-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy.py
89 lines (72 loc) · 2.51 KB
/
proxy.py
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
#!/usr/bin/env python
###############################################################################
# Copyright (c) 2012, Floor Terra <[email protected]>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
###############################################################################
import random
import urlparse
import urllib
import urllib2
import twitter
import time
ACCOUNT_NAME = "@floorter" # Running as this user
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_TOKEN_KEY = ""
ACCESS_TOKEN_SECRET = ""
def shorten_magnet(m):
if not m.startswith("magnet:?"):
return False
param = m[8:]
q = "magnet:?"
keep = []
keepone = []
for p in urlparse.parse_qsl(param):
if p[0] == "tr":
keepone.append(p)
elif p[0] == "dn":
keep.append(("dn", "short"))
elif p[0] == "xt":
q += p[0] + "=" + p[1] + "&"
else:
keep.append(p)
keep.append(random.choice(keepone))
q += urllib.urlencode(keep)
return q
def search(q):
url = "http://thepiratebay.se/search/%s/0/7/0" % urllib.quote(q)
page = urllib.urlopen(url).read()
start = page.find("magnet:")
end = page.find("\"", start)
return page[start:end]
api = twitter.Api(
consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
access_token_key=ACCESS_TOKEN_KEY,
access_token_secret=ACCESS_TOKEN_SECRET
)
while True:
f = open("last.id")
last = int(f.read())
f.close()
mentions = api.GetMentions(since_id=last)
for m in mentions:
magnet = search(m.text.replace(ACCOUNT_NAME, ""))
update_str = "@%s %s" % (m.user.screen_name, shorten_magnet(magnet))
update = api.PostUpdate(update_str, in_reply_to_status_id=m.id)
f = open("last.id", "w")
f.write("%d\n" % (m.id,))
f.close()
print update.text
time.sleep(30)