Created
April 13, 2013 19:46
-
-
Save 013/5379769 to your computer and use it in GitHub Desktop.
Generate magnet links and a couple other things
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/python | |
import re, sys, bencode, binascii, urllib, hashlib | |
tracker = "http://swiftler.com:6969/scrape?info_hash=" | |
def usage(): | |
print """ | |
Usage: ./scrape [OPTION] [FILE/HASH] | |
-h Use a hash to get seeders, leechers and completed downloads | |
-t Use a torrent file to get seeders, leechers and completed downloads | |
-g Generate a magnet link from a torrent file | |
""" | |
exit() | |
def get_url(tracker, info_hash): | |
info_hash = binascii.a2b_hex(info_hash) | |
info_hash = urllib.quote(info_hash) | |
return tracker + info_hash | |
def get_info(url): | |
response = urllib.urlopen(url) | |
op = response.read() | |
match = re.match( r'd5.*completei(.*)e10:downloadedi(.*)e10:incompletei(.*)eeee', op, re.M|re.I) | |
if match: | |
return match.group(1), match.group(2), match.group(3) | |
#print "match.group() : ", match.group() | |
#print "Seeders: \t\t", match.group(1) | |
#print "Leechers:\t\t", match.group(2) | |
#print "Complete Downloads: \t", match.group(3) | |
else: | |
return "No match!" | |
def get_hash(mag): | |
match = re.match( r'magnet:\?xt=urn:btih:(.{40}).*', mag, re.M|re.I) | |
if match: | |
return match.group(1) | |
else: | |
return 1 | |
if __name__ == "__main__": | |
try: | |
opt = sys.argv[1] | |
except IndexError: | |
usage() | |
if opt == '-h': | |
try: | |
info_hash = sys.argv[2] | |
except IndexError: | |
exit("Missing info_hash") | |
elif opt == '-t': | |
try: | |
tr_file = open(sys.argv[2], "rb") | |
meta = bencode.bdecode(tr_file.read()) | |
info = meta['info'] | |
info_hash = hashlib.sha1(bencode.bencode(info)).hexdigest() | |
except IndexError: | |
exit("Missing torrent file") | |
elif opt == '-g': | |
try: | |
tr_file = open(sys.argv[2], "rb") | |
meta = bencode.bdecode(tr_file.read()) | |
info = meta['info'] | |
info_hash = hashlib.sha1(bencode.bencode(info)).hexdigest() | |
# Only generate a magnet link, then exit | |
print "magnet:?xt=urn:btih:" + info_hash + "&tr=udp%3A%2F%2Fswiftler.com%3A6969" | |
exit() | |
except IndexError: | |
exit("Missing torrent file") | |
else: | |
usage() | |
url = get_url(tracker, info_hash) | |
print get_info(url); | |
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/python | |
#sudo apt-get install python-mysqldb | |
import MySQLdb as mdb | |
import sys, time | |
import scrape | |
con = None | |
tracker = "http://swiftler.com:6969/scrape?info_hash=" | |
try: | |
con = mdb.connect('localhost', 'username', | |
'password', 'db') | |
cur = con.cursor() | |
cur.execute("SELECT * FROM magnets") | |
rows = cur.fetchall() | |
desc = cur.description | |
for row in rows: | |
try: | |
id_f = int(row[0]) | |
magnet_uri = row[3] | |
last_check = int(row[6]) | |
seeders = int(row[7]) | |
leechers = int(row[8]) | |
except TypeError: | |
print "Empty field, check database..." | |
#print "Magnet Link: %s" % magnet_uri | |
#print "Lastcheck: \t%d\nSeeders: \t%d\nLeechers: \t%d\n" % (last_check, seeders, leechers) | |
# If the torrent has never been checked, or it was check more than 1 hour ago | |
if last_check == 0 or last_check <= (int(time.time()) - 3600): | |
#print "Updating... " | |
info_hash = scrape.get_hash(magnet_uri) | |
scrape_url = scrape.get_url(tracker, info_hash) | |
#print scrape_url | |
seeds, complete, leech = scrape.get_info(scrape_url) | |
cur.execute("UPDATE magnets SET seeders = %d WHERE id = %d" % (int(seeds), id_f)) | |
cur.execute("UPDATE magnets SET leechers = %d WHERE id = %d" % (int(leech), id_f)) | |
cur.execute("UPDATE magnets SET last_check = %d WHERE id = %d" % (int(time.time()), id_f)) | |
#print "Number of rows updated: %d" % cur.rowcount | |
except mdb.Error, e: | |
print "Error %d: %s" % (e.args[0], e.args[1]) | |
sys.exit(1) | |
finally: | |
if con: | |
con.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment