summaryrefslogtreecommitdiffstats
path: root/notcqst.py
blob: b5cc1b71ce9869006d996f57369a53c62209a9f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import socket
import random
from yt_dlp import YoutubeDL


def check_youtube():
    if "https://youtube.com" in text_str or "https://www.youtube.com" in text_str:
        split_str = text_str.split('##tech-tangents :')
        vid_url = split_str[1].strip()
        with YoutubeDL() as ydl:
            info_dict = ydl.extract_info(vid_url, download=False)
            video_url = info_dict.get("url", None)
            video_id = info_dict.get("id", None)
            video_title = info_dict.get('title', None)
            channel_name = info_dict.get('channel', None)
            irc.send((bytes("PRIVMSG " + channel + " :" + channel_name + ": " + video_title + "\r\n", 'utf-8')))
            return 1
    else:
        return 0


def command_choose(split_spaces):
    print(split_spaces)
    arr_hack = len(split_spaces)
    arr_hack -= 2
    choices = []
    for x in range(arr_hack):
        choices.append(split_spaces[x+2])
        print(choices)
    decision = random.choice(choices)
    irc.send((bytes("PRIVMSG " + channel + " :" + decision + "\r\n", 'utf-8')))


def command_hello():
    irc.send((bytes("PRIVMSG " + channel + " :" + "Hello, " + "\r\n", 'utf-8')))


def unknown_command():
    irc.send((bytes("PRIVMSG " + channel + " :" + "Sorry I don't understand." + "\r\n", 'utf-8')))


def command_flag():
    if ":n!" in text_str:
        split_text = text_str.split(':n!')
        split_spaces = split_text[1].split(' ')
        command = split_spaces[1].strip()
        match command:
            case "choose": command_choose(split_spaces)
            case "hello": command_hello()
            case _: unknown_command()


def parse_cmd():
    command_flag()
    check_youtube()


server = "irc.libera.chat"
channel = "##tech-tangents"
botnick = "notcqst"

irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

user_str = ("USER " + botnick + " " + botnick + " " + botnick + " :" + botnick + "\n")
nick_str = ("NICK " + botnick + "\n")
channel_string = ("JOIN " + channel + "\n")
send_to_channel_str = ("PRIVMSG " + channel + " :Hello " + "\r\n")

print("connecting to: " + server)

irc.connect((server, 6667))

irc.send(bytes(user_str, 'utf-8'))
irc.send(bytes(nick_str, 'utf-8'))
irc.send(bytes(channel_string, 'utf-8'))
irc.send(bytes(send_to_channel_str, 'utf-8'))


while 1:
    text = irc.recv(2040)
    print(text)
    text_str = text.decode("utf-8")
    parse_cmd()