From 89674ccddaa3fd4a9a4e11ce70a0d1b201ba6404 Mon Sep 17 00:00:00 2001 From: Dastan-glitch Date: Sun, 4 Dec 2022 03:23:21 +0300 Subject: [PATCH] bin/ircd: add titlebot and tweetbot --- bin/ircd/script/README.md | 12 ++++ bin/ircd/script/bots/irc.py | 36 ++++++++++++ bin/ircd/script/{ => bots/meetbot}/meetbot.py | 0 .../script/{ => bots/meetbot}/meetbot_cfg.py | 0 bin/ircd/script/bots/titlebot.py | 42 ++++++++++++++ bin/ircd/script/bots/tweetifier.py | 58 +++++++++++++++++++ 6 files changed, 148 insertions(+) create mode 100644 bin/ircd/script/bots/irc.py rename bin/ircd/script/{ => bots/meetbot}/meetbot.py (100%) rename bin/ircd/script/{ => bots/meetbot}/meetbot_cfg.py (100%) create mode 100644 bin/ircd/script/bots/titlebot.py create mode 100644 bin/ircd/script/bots/tweetifier.py diff --git a/bin/ircd/script/README.md b/bin/ircd/script/README.md index 508ef0a9b..b35a30093 100644 --- a/bin/ircd/script/README.md +++ b/bin/ircd/script/README.md @@ -16,3 +16,15 @@ is done through `meetbot_cfg.py`. * Edit `meetbot_cfg.py` for your needs. * Navigate terminal to the folder where `meetbot.py` is. * Run the bot: `$ python meetbot.py` + +## `titlebot` +`titlebot.py` is a bot used to print the title of a website provided by +a link in a `PRIVMSG`. + +## `tweetifier` +`tweetifier` is yet another bot that recognizes Twitter links, fetch +the tweet text and print it out in irc. + +## Requirment + `git clone https://github.com/Dastan-glitch/tweety.git` + `cd tweety && pip install .` \ No newline at end of file diff --git a/bin/ircd/script/bots/irc.py b/bin/ircd/script/bots/irc.py new file mode 100644 index 000000000..8ed6ea34d --- /dev/null +++ b/bin/ircd/script/bots/irc.py @@ -0,0 +1,36 @@ +import socket + +class IRC: + irc = socket.socket() + + def __init__(self): + # Define the socket + self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + + def send(self, channel, msg): + # Transfer data + self.irc.send(bytes("PRIVMSG " + channel + " :" + msg + "\n", "UTF-8")) + + def connect(self, server, port, channels, botnick): + # Connect to the server + print("Connecting to: " + server) + self.irc.connect((server, port)) + + # Perform user authentication + self.irc.send(bytes("USER " + botnick + " " + botnick +" " + botnick + " :python\n", "UTF-8")) + self.irc.send(bytes("NICK " + botnick + "\n", "UTF-8")) + # time.sleep(5) + + # join the channel + for chan in channels: + self.irc.send(bytes("JOIN " + chan + "\n", "UTF-8")) + + def get_response(self): + # time.sleep(1) + # Get the response + resp = self.irc.recv(2040).decode("UTF-8") + + if resp.find('PING') != -1: + self.irc.send(bytes('PONG ' + resp.split().decode("UTF-8") [1] + '\r\n', "UTF-8")) + + return resp diff --git a/bin/ircd/script/meetbot.py b/bin/ircd/script/bots/meetbot/meetbot.py similarity index 100% rename from bin/ircd/script/meetbot.py rename to bin/ircd/script/bots/meetbot/meetbot.py diff --git a/bin/ircd/script/meetbot_cfg.py b/bin/ircd/script/bots/meetbot/meetbot_cfg.py similarity index 100% rename from bin/ircd/script/meetbot_cfg.py rename to bin/ircd/script/bots/meetbot/meetbot_cfg.py diff --git a/bin/ircd/script/bots/titlebot.py b/bin/ircd/script/bots/titlebot.py new file mode 100644 index 000000000..dcd9cfcda --- /dev/null +++ b/bin/ircd/script/bots/titlebot.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- + +import re +import irc +import requests +from bs4 import BeautifulSoup + +## IRC Config +server = "127.0.0.1" +port = 11070 +channels = ["#test", "#test1"] +botnick = "website-title" +ircc = irc.IRC() +ircc.connect(server, port, channels, botnick) + +while True: + text = ircc.get_response() + text_list = text.split(' ') + if text_list[1] == "PRIVMSG": + channel = text_list[2] + msg = ' '.join(text_list[3:]) + url = re.findall(r'(https?://[^\s]+)', msg) + + for i in url: + reqs = requests.get(i) + soup = BeautifulSoup(reqs.text, 'html.parser') + + for title in soup.find_all('title'): + title_text = title.get_text() + if not len(title_text) > 0: + print("Error: Title not found!") + continue + title_text = title_text.split('\n') + title_msg = [] + # remove empty lines from tweet body + for line in title_text: + if not line.strip(): + continue + title_msg.append(line) + title_msg = " ".join(title_msg) + print(f"Title: {title_msg}") + ircc.send(channel, f"Title: {title_msg}") diff --git a/bin/ircd/script/bots/tweetifier.py b/bin/ircd/script/bots/tweetifier.py new file mode 100644 index 000000000..2e2d0fe8c --- /dev/null +++ b/bin/ircd/script/bots/tweetifier.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- + +import re +import irc +from tweety.bot import Twitter +from urllib.parse import urlparse + +## IRC Config +server = "127.0.0.1" +port = 11069 +channels = ["#test", "#test1"] +botnick = "tweetifier" +ircc = irc.IRC() +ircc.connect(server, port, channels, botnick) + +while True: + text = ircc.get_response() + if not len(text) > 0: + continue + print(text) + text_list = text.split(' ') + if text_list[1] == "PRIVMSG": + channel = text_list[2] + msg = ' '.join(text_list[3:]) + url = re.findall(r'(https?://[^\s]+)', msg) + + for i in url: + parsed_url = urlparse(i) + if str(parsed_url.path).endswith("/"): + tweetId = str(parsed_url.path)[:-1].split("/")[-1] + else: + tweetId = str(parsed_url.path).split("/")[-1] + print(f"tweet id: {tweetId}") + if not (parsed_url.netloc.lower() in ['twitter.com','t.co'] and parsed_url.scheme == 'https'): + continue + app = Twitter() + try: + tweet_text = app.tweet_detail(tweetId) + except: + print("Error: The Identifier provided of the tweet is either invalid or the tweet is private") + continue + + author_name = tweet_text.author.name + screen_name = tweet_text.author.screen_name + + tt = tweet_text.text.split('\n') + tweet_msg = [] + # remove empty lines from tweet body + for line in tt: + if not line.strip(): + continue + tweet_msg.append(line) + tweetify = str(' '.join(tweet_msg)) + if tweetify.startswith("@"): + tweetify = f"Replying to {tweetify}" + print(tweetify) + + ircc.send(channel, f"{author_name}(@{screen_name}): {tweetify}")