From 080741f36df4a311820945cfbafbae4ae763f291 Mon Sep 17 00:00:00 2001 From: Georgios Atheridis Date: Wed, 30 Mar 2022 19:55:44 +0300 Subject: [PATCH] added unit converter, translator, youtube api, raid --- skgyorugo/README.md | 10 ++-- skgyorugo/commands/t.py | 15 +++++ skgyorugo/commands/youtube.py | 20 +++++++ skgyorugo/main.py | 83 ++++++++++++++++---------- skgyorugo/scripts/translator.py | 6 ++ skgyorugo/scripts/unit_converter.py | 91 +++++++++++++++++++++++++++++ skgyorugo/tools/raid.py | 31 +++++----- skgyorugo/yt_api/__init__.py | 11 ++++ skgyorugo/yt_api/videos.py | 27 +++++++++ 9 files changed, 245 insertions(+), 49 deletions(-) create mode 100644 skgyorugo/commands/t.py create mode 100644 skgyorugo/commands/youtube.py create mode 100644 skgyorugo/scripts/translator.py create mode 100644 skgyorugo/scripts/unit_converter.py create mode 100644 skgyorugo/yt_api/__init__.py create mode 100644 skgyorugo/yt_api/videos.py diff --git a/skgyorugo/README.md b/skgyorugo/README.md index 1a75e96..dd1f015 100644 --- a/skgyorugo/README.md +++ b/skgyorugo/README.md @@ -1,21 +1,23 @@ ### TODO -- [ ] youtube api -- [ ] raid / host shoutout +- [x] youtube api +- [x] raid / host shoutout - [ ] latege (command and auto), could look through auto schedule - [ ] basic math polish notation -- [ ] translator (?t a replied message / database in memory of messages) +- [x] translator +- [ ] (?t a replied message / database in memory of messages) - [ ] followage command - [ ] commands in groups - [ ] quote adder / remover / getter - [ ] reminder (timer) -- [ ] inch to cm +- [x] inch to cm - [ ] license --- opensource --- info - [ ] streamer time - [ ] list maker (who's up for flex) - [ ] variable counter with cross-command functionality - [ ] specific word in message {message.0}, {message.1}, etc. - [ ] replied variable {reply.nick} {reply.message} {reply.message.0} +- [ ] sub sellout bot ## Basic Commands - [ ] int diff --git a/skgyorugo/commands/t.py b/skgyorugo/commands/t.py new file mode 100644 index 0000000..fc4e7f2 --- /dev/null +++ b/skgyorugo/commands/t.py @@ -0,0 +1,15 @@ +from aptbot.bot import Message, Commands, Bot +import tools.smart_privmsg +import scripts.translator + +PERMISSION = 99 +PREFIX = '?' +DESCRIPTION = "Translates a message from any language (supported by google translate) into English. How to use: ?t " +USER_COOLDOWN = 15 +GLOBAL_COOLDOWN = 5 + + +def main(bot: Bot, message: Message): + msg = ' '.join(message.value.split(' ')[1:]) + trans = scripts.translator.translate(msg) + tools.smart_privmsg.send(bot, message, trans) diff --git a/skgyorugo/commands/youtube.py b/skgyorugo/commands/youtube.py new file mode 100644 index 0000000..1829fa1 --- /dev/null +++ b/skgyorugo/commands/youtube.py @@ -0,0 +1,20 @@ +from aptbot.bot import Message, Commands, Bot +import yt_api.videos + +PERMISSION = 99 +PREFIX = '?' +DESCRIPTION = "Get my newest video! Just type ?video" +USER_COOLDOWN = 15 +GLOBAL_COOLDOWN = 15 + +CHANNEL_ID = "UCQ7C3NUKY6TSkURdUdVoYFw" + + +def main(bot: Bot, message: Message): + video = yt_api.videos.get_newest_video(CHANNEL_ID) + if video: + video_link = f"https://www.youtube.com/watch?v={video.video_id}" + msg = f"Watch my latest video \"{video.video_name}\" here -> {video_link}" + bot.send_privmsg(message.channel, msg) + else: + msg = f"Check out my youtube channel here -> https://www.youtube.com/channel/{CHANNEL_ID}" diff --git a/skgyorugo/main.py b/skgyorugo/main.py index 1685603..de62fb6 100644 --- a/skgyorugo/main.py +++ b/skgyorugo/main.py @@ -1,6 +1,3 @@ -import tools.raid -import tools.smart_privmsg -import tools.permissions from aptbot.bot import Bot, Message, Commands import os import importlib @@ -9,11 +6,18 @@ import sqlite3 from importlib import reload import traceback import ttv_api.users +import tools.raid +import tools.smart_privmsg +import tools.permissions import analyze_command +import scripts.unit_converter +import yt_api.videos reload(tools.raid) reload(tools.smart_privmsg) reload(tools.permissions) +reload(analyze_command) +reload(scripts.unit_converter) PATH = os.path.dirname(os.path.realpath(__file__)) COMMANDS_PATH = os.path.join(PATH, "commands") @@ -51,25 +55,33 @@ def create_database(): conn = sqlite3.connect(os.path.join(PATH, "database.db")) c = conn.cursor() try: - c.execute("""CREATE TABLE commands ( - command TEXT NOT NULL, - prefix TEXT NOT NULL, - permission INTEGER NOT NULL, - description TEXT, - user_cooldown INTEGER NOT NULL, - global_cooldown INTEGER NOT NULL, - last_used INTEGER NOT NULL, - PRIMARY KEY (command) - )""") + c.execute( + """ + CREATE TABLE commands ( + command TEXT NOT NULL, + prefix TEXT NOT NULL, + permission INTEGER NOT NULL, + description TEXT, + user_cooldown INTEGER NOT NULL, + global_cooldown INTEGER NOT NULL, + last_used INTEGER NOT NULL, + PRIMARY KEY (command) + ) + """ + ) except sqlite3.OperationalError: print("Table commands exists") try: - c.execute("""CREATE TABLE users ( - user_id text NOT NULL, - permission INTEGER NOT NULL, - PRIMARY KEY (user_id) - )""") + c.execute( + """ + CREATE TABLE users ( + user_id text NOT NULL, + permission INTEGER NOT NULL, + PRIMARY KEY (user_id) + ) + """ + ) except sqlite3.OperationalError as e: print(f"Table users exists: {e}") else: @@ -79,23 +91,31 @@ def create_database(): (aptbot_id[0].user_id, 0)) try: - c.execute("""CREATE TABLE cooldowns ( - user_id TEXT NOT NULL, - command TEXT NOT NULL, - user_cooldown INTEGER NOT NULL, - FOREIGN KEY(user_id) REFERENCES users(user_id) - FOREIGN KEY(command) REFERENCES commands(command) - PRIMARY KEY (user_id, command) - )""") + c.execute( + """ + CREATE TABLE cooldowns ( + user_id TEXT NOT NULL, + command TEXT NOT NULL, + user_cooldown INTEGER NOT NULL, + FOREIGN KEY(user_id) REFERENCES users(user_id) + FOREIGN KEY(command) REFERENCES commands(command) + PRIMARY KEY (user_id, command) + ) + """ + ) except sqlite3.OperationalError: print("Table cooldowns exists") try: - c.execute("""CREATE TABLE command_values ( - command TEXT NOT NULL, - value TEXT NOT NULL, - FOREIGN KEY(command) REFERENCES commands(command) - )""") + c.execute( + """ + CREATE TABLE command_values ( + command TEXT NOT NULL, + value TEXT NOT NULL, + FOREIGN KEY(command) REFERENCES commands(command) + ) + """ + ) except sqlite3.OperationalError: print("Table cooldowns exists") @@ -139,5 +159,6 @@ update_commands_in_database() def main(bot: Bot, message: Message): if message.command == Commands.PRIVMSG: analyze_command.do_command(bot, message, modules) + scripts.unit_converter.send_metric(bot, message) tools.raid.raid(bot, message) diff --git a/skgyorugo/scripts/translator.py b/skgyorugo/scripts/translator.py new file mode 100644 index 0000000..7447a51 --- /dev/null +++ b/skgyorugo/scripts/translator.py @@ -0,0 +1,6 @@ +from deep_translator import GoogleTranslator + + +def translate(text: str) -> str: + trans = GoogleTranslator(source="auto", target="en").translate(text) + return trans diff --git a/skgyorugo/scripts/unit_converter.py b/skgyorugo/scripts/unit_converter.py new file mode 100644 index 0000000..ffd6315 --- /dev/null +++ b/skgyorugo/scripts/unit_converter.py @@ -0,0 +1,91 @@ +from aptbot.bot import Bot, Message, Commands +import spacy +import re +import tools.smart_privmsg + +nlp = spacy.load("en_core_web_sm") + + +def send_metric(bot: Bot, message: Message): + text = "" + ft_inch, cm = _tometric(message.value) + for i in range(len(cm)): + if cm[i] > 230: + text += f"{ft_inch[i][0]:.1f}ft. and {ft_inch[i][1]:.1f}in. is {cm[i] / 100:.2f}m. | " + elif cm[i] > 0: + text += f"{ft_inch[i][0]:.1f}ft. and {ft_inch[i][1]:.1f}in. is {cm[i]:.1f}cm. | " + if text: + tools.smart_privmsg.send(bot, message, text) + + +def _tometric(text: str) -> tuple[list[tuple[int, int]], list[float]]: + ft_inch: list[tuple] = [] + feet = 0 + inches = 0 + text = text.replace("-", " ") + text = re.sub(r"([0-9]+(\.[0-9]+)?)", r"\1 ", text) + text = re.sub(r"\s{2,}", r" ", text) + doc = nlp(text) + feet_was_last = False + next_should_be_double = False + found_single = False + for w in doc: + # print(w.text, w.pos_) + if w.pos_ in {'AUX', 'VERB', 'ADP'}: + if feet_was_last and not next_should_be_double: + ft_inch.append((feet, 0.0)) + feet = 0 + inches = 0 + if w.like_num or w.pos_ == 'NUM': + # index_of_previous_token = w.i - 1 + # try: + # prev_token = doc[index_of_previous_token] + # print(prev_token.lemma_) + # if prev_token.lemma_ != "be": + # continue + # except: + # pass + # if "'" in w.text: + # feet_and_inches = w.text.split("'") + # try: + # feet = float(feet_and_inches[0]) + # inches = float(feet_and_inches[1]) + # except: + # pass + index_of_next_token = w.i + 1 + try: + next_token = doc[index_of_next_token] + if next_token.lemma_ in {"ft", "foot", "'"}: + if next_token.lemma_ == "'" and not next_should_be_double: + next_should_be_double = True + elif next_token.lemma_ == "'": + feet = 0 + inches = 0 + continue + if feet_was_last: + ft_inch.append((feet, 0.0)) + feet = float(w.text) + feet_was_last = True + elif next_token.lemma_ in {"inch", "\""}: + if next_token.lemma_ == "\"" and next_should_be_double: + inches = float(w.text) + next_should_be_double = False + elif next_token.lemma_ == "\"": + inches = 0 + elif next_should_be_double: + feet = 0 + inches = float(w.text) + else: + inches = float(w.text) + ft_inch.append((feet, inches)) + feet = 0 + inches = 0 + feet_was_last = False + except: + pass + if feet_was_last and not next_should_be_double: + ft_inch.append((feet, 0.0)) + cm: list[float] = [] + for unit in ft_inch: + cm.append((unit[0] * 12 + unit[1]) * 2.54) + return (ft_inch, cm) diff --git a/skgyorugo/tools/raid.py b/skgyorugo/tools/raid.py index f16eb16..104729b 100644 --- a/skgyorugo/tools/raid.py +++ b/skgyorugo/tools/raid.py @@ -1,18 +1,21 @@ +import ttv_api.channel from aptbot.bot import Bot, Message, Commands def raid(bot: Bot, message: Message): - if message.command == Commands.USERNOTICE: - if message.tags["msg-id"] == "raid": - raider_name = message.tags["msg-param-displayName"] - raider_login = message.tags["msg-param-login"] - raider_id = message.tags["user-id"] - raider_game = "" - if raider_id: - raider_channel_info = "channel info here" - viewers = message.tags["msg-param-viewerCount"] - viewers = f"{viewers} viewer" if viewers == "1" else f"{viewers} viewers" - msg_reply = f"POGGERS {raider_name} has raided {message.channel} with {viewers}!!! Why don\'t you check them out at https://twitch.tv/{raider_login}" - if raider_game: - msg_reply += f' they were just playing {raider_game}.' - bot.send_privmsg(message.channel, msg_reply) + if message.command == Commands.USERNOTICE and message.tags["msg-id"] == "raid": + raider_name = message.tags["msg-param-displayName"] + raider_login = message.tags["msg-param-login"] + raider_id = message.tags["user-id"] + raider_channel_info = ttv_api.channel.get_channels(raider_id) + raider_game = "" + if raider_channel_info: + raider_game = raider_channel_info[0].game_name + viewers = message.tags["msg-param-viewerCount"] + viewers = f"{viewers} viewer" if viewers == "1" else f"{viewers} viewers" + msg_reply = f"POGGERS {raider_name} has raided {message.channel} \ + with {viewers}!!! Why don't you check them out at: \ + https://twitch.tv/{raider_login}" + if raider_game: + msg_reply += f" they were just playing {raider_game}!" + bot.send_privmsg(message.channel, msg_reply) diff --git a/skgyorugo/yt_api/__init__.py b/skgyorugo/yt_api/__init__.py new file mode 100644 index 0000000..7c8626b --- /dev/null +++ b/skgyorugo/yt_api/__init__.py @@ -0,0 +1,11 @@ +import urllib3 +from dataclasses import dataclass +import os +import json +from typing import Optional + +URL = "https://www.googleapis.com/youtube/v3/search" + +API = os.getenv("YOUTUBE_API") +CLIENT_ID = os.getenv("YOUTUBE_CLIENT_ID") +CLIENT_SECRET = os.getenv("YOUTUBE_CLIENT_SECRET") diff --git a/skgyorugo/yt_api/videos.py b/skgyorugo/yt_api/videos.py new file mode 100644 index 0000000..c43954a --- /dev/null +++ b/skgyorugo/yt_api/videos.py @@ -0,0 +1,27 @@ +from yt_api import * + + +@dataclass +class Video: + video_name: str + video_id: str + + +def get_newest_video(channel_id: str) -> Optional[Video]: + get_video_snippets = "?part=snippet" + get_url = URL + get_video_snippets + \ + f"&channelId={channel_id}&order=date&type=video" + f"&key={API}" + + http = urllib3.PoolManager() + r = http.request( + "GET", + get_url, + ) + print(f"the r.status is {r.status}") + if r.status != 200: + return None + data = json.loads(r.data.decode("utf-8"))["items"][0] + video_id = data["id"]["videoId"] + video_title = data["snippet"]["title"] + + return Video(video_title, video_id) -- 2.30.2