Created
June 21, 2013 06:15
-
-
Save skope/5829235 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
import socket | |
import sys | |
import re | |
import time | |
import urllib2 | |
from pymongo import MongoClient | |
from lxml.html import parse | |
from datetime import datetime | |
# Parse nick from line | |
def get_nick(nick): | |
return re.search('(?<=\:)(.*?)(?=\!)', nick).group(0) | |
# Parse host from line | |
def get_host(host): | |
return re.search('(?<=\!)(.*?)(?=\ )', host).group(0) | |
# Parse message from line | |
def get_msg(msg): | |
return re.search('(.*?:){2}(.*)', msg).group(2) | |
# Parse channel from line | |
def get_channel(channel): | |
return re.search('(?<=\PRIVMSG )(.*?)(?=\ )', channel).group(0) | |
# Print current timestamp | |
def timestamp(): | |
return datetime.fromtimestamp(time.time()).strftime("%H:%M:%S") | |
# Return URL and check if it works | |
def verify_url(url): | |
try: | |
urllib2.urlopen(url) | |
return True | |
except IOError: | |
return False | |
# Parse title from url | |
def parse_title(url): | |
try: | |
return parse(urllib2.urlopen(url)).find(".//title").text.rstrip() | |
except IOError: | |
return False | |
def say(chan, msg): | |
try: | |
irc.send("PRIVMSG " + chan + " :" + msg.encode('utf-8') + "\n") | |
except: | |
return False | |
# Main configuration | |
config = { | |
"host": "localhost", | |
"port": 6667, | |
"channels": ["#bot"], | |
"nick": "bot" | |
} | |
# Set up mongo | |
client = MongoClient("localhost") | |
db = client.psykebot | |
dbLinks = db.links | |
# Initialize IRC socket | |
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
print "[" + timestamp() + "] Connecting to " + config["host"] | |
# Connect to server | |
irc.connect((config["host"], config["port"])) | |
irc.send("USER " + config["nick"] + " " + config["nick"] + " " + config["nick"] + ": Psykedelia\n") | |
irc.send("NICK " + config["nick"] + "\n") | |
# Join to channels | |
for c in config["channels"]: | |
irc.send("JOIN " + c + "\n") | |
print "[" + timestamp() + "] Connected to " + c | |
while 1: | |
text = irc.recv(2040) | |
if text.find("PING") != -1: | |
irc.send("PONG " + text.split()[1] + "\n") | |
if text.find("PRIVMSG #") != -1: | |
line = { | |
"username": get_nick(text), | |
"channel": get_channel(text) | |
} | |
msg = get_msg(text) | |
if re.search('^\!old\ ', msg): | |
url = msg.split(" ")[1] | |
old = dbLinks.find_one({ "url": url }) | |
output = url + " was posted " + old["timestamp"].strftime("%d.%m.%Y %H:%M:%S") + " by " + old["username"] | |
#output = "%s was posted %s by %s" % (url, old["timestamp"].strftime("%d.%m.%Y %H:%M:%S"), old["username"]) | |
say(line["channel"], output) | |
print "[" + timestamp() + "] " + line["channel"] + " <" + line["username"] + "> " + msg | |
if re.search('(http[s]?://|ftp://)(.*?)(?=(\ |$))', get_msg(text)): | |
url = re.search('(http[s]?://|ftp://)(.*?)(?=(\ |$))', msg).group(0) | |
if verify_url(url): | |
if not re.search('^\!old\ ', msg): | |
title = parse_title(url) | |
if dbLinks.find({ "url": url.rstrip() }).count() > 0: | |
say(line["channel"], "(old) " + title) | |
else: | |
say(line["channel"], title) | |
link = [{ | |
"username": line["username"], | |
"channel": line["channel"], | |
"title": title, | |
"url": url.rstrip(), | |
"timestamp": datetime.now() | |
}] | |
dbLinks.insert(link) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment