Python Torrent Search and Download (TPB)

This python command line utility will search the pirate bay for a specific search string and pull out the torrent files and download them to your current directory.

by: Daniel Folkes

This is Licensed under GPLv3. Give Back.

tpb.jpg

    Download Source

  1. print “””Pirate Bay Torrent Downloader – Command Line Interface
  2. Program Written by: Daniel Folkes
  3. website: http://danfolkes.com
  4. email: danfolkes @t gmail dot c0m
  5.         
  6.         — Usage —
  7.     -a[n] = auto downloads first [n] results (default:10)
  8.     -o[/path/] = points to the folder where .torrent’s go
  9. “””
  10. import urllib2
  11. import sys
  12. autoprint = 0
  13. outgoing = “./”
  14. searchFor = []
  15. for arg in sys.argv[1:]:
  16.     print “|”+arg+”|”
  17.     if arg[:2] == “-a”:
  18.         autoprint = int(arg[2:len(arg)])
  19.     elif arg[:2] == “-o”:
  20.         outgoing = arg[2:len(arg)]
  21.     else:
  22.         searchFor.append(arg)
  23.     
  24. print “Searching The Pirate Bay for: ” + str(searchFor)
  25. url = ‘http://thepiratebay.org/search.php?q=’
  26. o = 0;
  27. for ar in searchFor:
  28.     if o != 0:
  29.         url+=”%20″    
  30.     url+=str(ar)
  31.     o +=1
  32. print “URL: ” + url
  33. req = urllib2.Request(url)
  34. response = urllib2.urlopen(req)
  35. the_page = response.read()
  36. results = 15
  37. i=0
  38. listing = []
  39. for line in the_page.split(“<td>”):
  40.     if i < results:
  41.         if line.count(“.torrent”):
  42.             strippedLine = line[9:line.find(“.torrent”)+8]
  43.             if autoprint == 0:
  44.                 print i, strippedLine[15:]
  45.             listing.append(strippedLine)
  46.             i+=1
  47. if autoprint==0:
  48.     download = raw_input(“Which ones to download?(sep by commas) :”).split(‘,’)
  49. else:
  50.     download = range(0,autoprint)
  51. print download
  52. for j in download:
  53. g = int(j)
  54. if (g < results) and (g >=0):
  55.         
  56.         #— Strip Characters Off
  57.         localPath = listing[g]
  58.         for c in “.:/%!@#$^&*()_-=+~`[]{}|;’,.”:
  59.          localPath = localPath.replace(c,””)
  60.         localPath = localPath[(len(localPath)/2):]
  61.         localPath += “.torrent”
  62.         if outgoing:
  63.             localPath = outgoing+localPath
  64.         #—
  65.         print “Downloading: “+ listing[g] + ” -> ” + str(localPath)
  66.         open(localPath, ‘wb’).write(urllib2.urlopen(listing[g]).read())
  67.         #import os             #if you want to use wget and have progress bar (remove line before)
  68.         #os.system(“wget “+listing[g])     #if you want to use wget and have progress bar
  69. print “…Download Complete”
Facebooktwittergoogle_pluspinterest

One thought on “Python Torrent Search and Download (TPB)”

Comments are closed.