summaryrefslogtreecommitdiff
path: root/feeder/dump.py
blob: fef178a353d400ed33a7b6e5c63664180ce4f6dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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()