diff options
Diffstat (limited to 'feeder/dump.py')
| -rwxr-xr-x | feeder/dump.py | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/feeder/dump.py b/feeder/dump.py new file mode 100755 index 0000000..fef178a --- /dev/null +++ b/feeder/dump.py @@ -0,0 +1,191 @@ +#!/usr/bin/python + +import urllib +import urllib2 +import sha +import sys +import time +import string +import os +import os.path +import random +import simplejson as json + +urlencode = urllib.urlencode +urlopen = urllib2.urlopen +Request = urllib2.Request + +def now (): + return int(time.mktime(time.localtime())) + +USER_NICK = 'pepper' +USER_PASSWORD = 'amnesia' +ROOM_NAME = 'dumpfm' + +API_FIRST = 'http://dump.fm/' +API_LOGIN = 'http://dump.fm/login' +API_POLL = 'http://dump.fm/refresh' +API_SAY = 'http://dump.fm/msg' +API_FAVE = 'http://dump.fm/cmd/tag/add' +API_SEARCH = 'http://dump.fm/cmd/search' + +SEARCH_DIR = 'searches' + +class Dump: + def __init__ (self, nick=USER_NICK, password=USER_PASSWORD, room=ROOM_NAME): + self.nick = nick + self.password = password + self.room = room + self.hash = sha.new(self.nick + '$' + self.password + '$dumpfm').hexdigest() + + self.bots = ["pepper", "DaytimeTelevision"] + self.cookie = "" + self.timestamp = 0 + self.firstTime = True + self.favers = {} + self.pending_faves = {} + + self.login() + def get (self, url, params=None): + if params is not None: + url += "?%s" + return self.request(url % urlencode(params), None) + else: + return self.request(url, None) + def post (self, url, params): + data = urlencode(params) + return self.request(url, data) + def request (self, url, data): + headers = { + 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)', + 'Referer': 'http://dump.fm/chat', + 'Cookie': self.cookie, + 'Accept': '*/*', + } + try: + req = Request(url, data, headers) + response = urlopen(req) + except IOError, e: + if hasattr(e, 'code'): + print '%s - ERROR %s' % (url, e.code) + return None + else: + return response + def login (self): + # self.cookie = 'compojure-session=95343788-a029-4935-ab7a-9402e8509a4a; ' + # self.cookie += 'login-token=v1%25djdonjuan%251306303289221%254c3a4f6c6950ee72ff002ab0cdf27c2ce762e5ce' + response = self.get(API_FIRST) + headers = str(response.info()).split("\n") + for h in headers: + partz = h.split(": ") + if partz[0] == "Set-Cookie": + if "compojure-session" in partz[1]: + self.cookie += partz[1].split(";")[0] + break + response.read() + + params = {'nick': self.nick, 'ts': '', 'hash': self.hash, 'rememberme': 'yes'} + response = self.post(API_LOGIN, params) + headers = str(response.info()).split("\n") + for h in headers: + partz = h.split(": ") + if partz[0] == "Set-Cookie": + if "login-token" in partz[1]: + token = partz[1].split(";")[0] + self.cookie += "; " + token + break + response.read() + # print self.cookie + def poll (self): + # ?_=1305699034230&room=dumpfm&since=1305699032374 + params = {'_': self.timestamp + 1776, 'room': self.room, 'since': self.timestamp} + response = self.get(API_POLL, params) + if response is None: + return + try: + data = json.loads(response.read()) + except json.decoder.JSONDecodeError: + return + self.timestamp = data['timestamp'] + if self.firstTime: + self.firstTime = False + return + if len(data['messages']): + for msg in data['messages']: + if msg['nick'] == self.nick: + print self.nick, ">", "new dump" + elif 'asdf.us' in msg['content']: + print msg['nick'], "redumped from asdf" + elif self.nick in msg['content']: + print msg['nick'], ">", msg['content'] + # if 'asdf.us' in msg['content'] and msg['nick'] != self.nick: + # self.set_fave_timeout(msg, "for redumping") + if msg['nick'] in self.favers and self.favers[msg['nick']] > 0 and "http" in msg['content']: + self.set_fave_timeout(msg, "back") + self.favers[msg['nick']] -= 1 + if len(data['favs']): + for fav in data['favs']: + print "+", fav['from'], "just faved you!" + if fav['from'] in self.favers: + self.favers[fav['from']] += 1 + else: + self.favers[fav['from']] = 1 + self.delay_fave() + # print "timestamp =", str(self.timestamp) + def set_fave_timeout (self, msg, reason): + if msg['nick'] in self.bots: + return + print 'faved', msg['nick'], reason + self.pending_faves[msg['msg_id']] = random.randint(3,12) + + def delay_fave (self): + to_remove = [] + for id in self.pending_faves.keys(): + if self.pending_faves[id] == 0: + to_remove.append(id) + self.fave(id) + else: + self.pending_faves[id] -= 1 + for id in to_remove: + del(self.pending_faves[id]) + + def say (self, msg): + print ">", msg + self.post(API_SAY, {'room': self.room, 'content': msg}) + def fave (self, msgid): + self.post(API_FAVE, {'tag': 'favorite', 'message_id': msgid}) + def search (self, term): + if not term: + return + url = API_SEARCH +'/'+ term + response = self.get(url) + urls = json.loads(response.read()) + + out = open(SEARCH_DIR+"/"+term+".txt", "w") + for row in urls: + url = "" + if "url" not in row: + continue + if row['url'][0] == "/": + url = "http://dump.fm/images" + row['url'] + else: + url = "http://" + row['url'] + pos = url.lower().find(term) + prev = url[pos-1] + next = url[pos+len(term)] + if prev not in string.punctuation or next not in string.punctuation: + continue + out.write(url+"\n") + out.close() + + def poll_forever (self): + while 1: + self.poll() + time.sleep(1) + + +if __name__ == '__main__': + dump = Dump () + # dump.login() + dump.poll_forever() + |
