Skip to content

Instantly share code, notes, and snippets.

@umrysh
Created December 21, 2015 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save umrysh/e2523d5dafe374d30d1e to your computer and use it in GitHub Desktop.
Save umrysh/e2523d5dafe374d30d1e to your computer and use it in GitHub Desktop.
Create a list of people downloading Star Wars: The Force Awakens
import libtorrent as lt
import time
import pickle
import MySQLdb as mdb
#import sqlite3 as lite
#import BeautifulSoup as bs
import bs4 as bs
import pickle
import urllib2
import sys
import datetime
import os
import GeoIP
con = None
cur = None
params = { 'save_path': './'}
num_tries = 3
pickle_file = 'top100.bin'
base_url = 'http://thepiratebay.gd'
def ip_location(ip_address):
location = ["",""]
#Receive an and return Country, City and Latitude/Longitude
gi = GeoIP.open("./GeoIPCity.dat",GeoIP.GEOIP_STANDARD)
gir = gi.record_by_addr(ip_address)
if gir != None:
location = [gir['country_name'], gir['city']]
return location
def peer_info(handle):
#Take the torrent handle and return peer information
return 0
def create_magnet_url(torrent_hash):
link = "magnet:?xt=urn:btih:%s&tr=udp%%3A%%2F%%2Ftracker.openbittorrent.com%%3A80&tr=udp%%3A%%2F%%2Ftracker.publicbt.com%%3A80" % torrent_hash
return link
def delete_session(handle,ses):
ses.remove_torrent(handle,1)
def create_session():
ses = lt.session()
return ses
def create_handle(ses,link,params):
handle = lt.add_magnet_uri(ses, link, params)
return handle
def get_metadata(handle):
print('downloading metadata...')
handle.metadata()
print('got metadata, starting torrent download...')
def insert_peer_details(handle,name):
#Get peer information
pe = handle.get_peer_info()
for p in handle.get_peer_info():
cur.execute('select dateunix from IP_%s where addr = "%s" and torrent="%s"' % (p.ip[0].split(".")[0],p.ip[0],name))
numrows = len(cur.fetchall())
if(numrows==0):
location = ip_location(p.ip[0])
print("IP: %s, Port: %s, City: %s, Country: %s" % (p.ip[0],p.ip[1],location[1],location[0]))
cur.execute('INSERT INTO IP_%s(addr,torrent,dateunix,city,country) VALUES ("%s","%s","%s","%s","%s")' % (p.ip[0].split(".")[0],p.ip[0],name,datetime.datetime.now(),location[1],location[0]))
con.commit()
def parseNav(html):
navLinks = [base_url+link['href'] for link in html.findAll("a")]
return navLinks
def getDetails(html):
html = str(html)
html = ''.join([i if ord(i) < 128 else ' ' for i in html])
soup = bs.BeautifulSoup(html,"lxml")
links = [str(tag['href']) for tag in soup.findAll("a")] #Get all links in a list
t = links[2].split('/')
torrentName = t[-1]
torrenrId = t[2]
details = {}
details["name"] = torrentName
details["detaiLink"] = links[2]
details['downloadLink'] = links[3]
details['id'] = torrenrId
return details
########################################################
def start(soup,userPage=False):
global nav
menu = [] #Execution starts here
for tag in soup.findAll("table",{'id':'searchResult'}):
tr = tag.findAll("tr")
if userPage:
nav = tr.pop()
nav = parseNav(nav)
menu = [getDetails(tr[i])for i in range(1,len(tr))]
for i in range(1,len(tr)):
menu.append(getDetails(tr[i]))
x = getDetails(tr[i])
return menu
def saveNav(nav,fileName="nav.bin"):
pickle.dump(nav, open(fileName,"wb"))
def main():
global con
global cur
# Open mysql
try:
con = mdb.connect(host="127.0.0.1", port=3306,user="user", passwd="password",db="torrent")
except mdb.Error, e:
print("Could not connect to MySQL database.\nError %d: %s" % (e.args[0],e.args[1]))
sys.exit(1)
cur = con.cursor()
#con = lite.connect("TorrentDB.db")
with con:
#con.row_factory = lite.Row
#cur = con.cursor()
# Create all the tables
print("[*]Creating Tables")
for count in range (0,256):
#print("CREATE TABLE IP_%s (addr varchar(15), torrent varchar (255), dateunix varchar(17), INDEX (addr)) ENGINE=InnoDB;" % (count)
#print("CREATE TABLE IP_%s (addr varchar(15), torrent varchar (255), dateunix varchar(17)" % (count)
cur.execute("CREATE TABLE IF NOT EXISTS IP_%s (addr varchar(15), torrent varchar (255), dateunix varchar(17),city varchar(50),country varchar(50))" % (count))
cur.execute("CREATE INDEX IP_%s_addr ON IP_%s (addr)"% (count,count))
con.commit()
print("[*]Finished Creating Tables")
while(True):
try:
mode = int(sys.argv[1])
url = sys.argv[2]
output = sys.argv[3]
output = output.lower()
except Exception as e:
mode = 1
url = "https://thepiratebay.gd/search/star%20wars%20the%20force%20awakens/0/99/200"
output = "true"
print("[*]Started")
print("[*]Sending Request")
req = urllib2.Request(url)
req.add_header('User-Agent',"Firefox 3")
print("[*]Reading Response")
html = str(urllib2.urlopen(req).read())
soup = bs.BeautifulSoup(html, "lxml")
if mode == 1:
print("[*]Parsing Top 100 From url: %s" % url)
x = start(soup)
if output == "true":
print("\n[*}Printing top 100")
for i in x:
print(i)
print("\n\n\n")
print("[*]Saving Top 100 as top100.bin")
saveNav(x, "top100.bin")
elif mode == 0: #and 'user' in url:
print("[*}Parsing page for user url %s" % url)
x = start(soup,True)
print("[*]Done Parsing")
print("[*]Saving Nav Panel")
if output == "true" and mode == 0:
print("[*]Printing Response")
print("=====================")
print(x)
print("\n\n============================")
print("[*]NavPanel")
print("\n\n=================================")
print(nav)
saveNav(nav)
print("\n\n[*]Done")
else:print("Sleep with fishes")
## Parse the file
f = pickle.load(open(pickle_file))
for line in f:
tries = 0
# Create a session, get a handle, grab a hash and get peers
link = line['downloadLink']
print(line['name'])
ses = create_session()
handle = create_handle(ses,link,params)
while tries < num_tries:
print("Discovering Peers...")
handle.has_metadata()
time.sleep(30) #Give metadata 30s to timeout
if (not handle.has_metadata()):
tries = tries + 1
else:
insert_peer_details(handle,line['name'])
tries = 5
delete_session(handle,ses)
if __name__ == '__main__':
main()
import MySQLdb as mdb
import sys,csv
from datetime import datetime
def main():
global con
global cur
# Open mysql
try:
con = mdb.connect(host="127.0.0.1", port=3306,user="user", passwd="password",db="torrent")
except mdb.Error, e:
print("Could not connect to MySQL database.\nError %d: %s" % (e.args[0],e.args[1]))
sys.exit(1)
cur = con.cursor()
with con:
# Open file for writing
f = csv.writer(open("/home/user/Dropbox/StarWarsIPs/StarWarsDownloaders.csv", 'wb'), delimiter=',',quotechar='"', quoting=csv.QUOTE_MINIMAL)
f.writerow(["IP Address","Time","Torrent Name","City","Country"])
for count in range (0,256):
cur.execute('SELECT addr,dateunix,torrent,city,country from IP_%s' % count)
rows = cur.fetchall()
for row in rows:
f.writerow(row)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment