mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
bin/ircd: add titlebot and tweetbot
This commit is contained in:
@@ -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 .`
|
||||
36
bin/ircd/script/bots/irc.py
Normal file
36
bin/ircd/script/bots/irc.py
Normal file
@@ -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
|
||||
42
bin/ircd/script/bots/titlebot.py
Normal file
42
bin/ircd/script/bots/titlebot.py
Normal file
@@ -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}")
|
||||
58
bin/ircd/script/bots/tweetifier.py
Normal file
58
bin/ircd/script/bots/tweetifier.py
Normal file
@@ -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}")
|
||||
Reference in New Issue
Block a user