diff --git a/pirate.py b/pirate.py index d332776..0463b88 100755 --- a/pirate.py +++ b/pirate.py @@ -4,12 +4,12 @@ """ The Pirate Bay scraper - -Uses 2 external libraries for scraping HTML elements from ThePirateBay. +Uses 3 external libraries for scraping HTML elements from ThePirateBay and interacting with transmission-daemon. Asks user for a search selection, offers a list of choices, and grabs the magent link for the selection. """ -__author__ = 'LANCE' +__author__ = 'LANCE - https://github.com/lalanza808' # Built-in libraries @@ -23,15 +23,15 @@ from time import sleep # 3rd party libraries import requests import bs4 - +import transmissionrpc results = {} links = [] choice = "" tpb = "https://thepiratebay.se" -# Squelch SSL warnings -urllib3.disable_warnings() +# Squelch HTTPS insecure warnings +requests.packages.urllib3.disable_warnings() def getSearchURL(): """ @@ -39,12 +39,15 @@ def getSearchURL(): Formats string into proper url """ try: - searchString = raw_input("[+] What would you like to search?\n>") + searchString = raw_input("[+] What would you like to search?\n> ") except KeyboardInterrupt: print "\n\nLater bro." exit(0) + searchURL = "{}/search/{}/0/7/0".format(tpb, searchString) #/0/7/0 tells TPB to sort descending by seeds + pageSource = requests.get(searchURL, verify=False).text #Use requests lib to fetch page source for bs4 parsing + analyzeURL(pageSource) #Run analyzeURL function, passing page source @@ -84,7 +87,7 @@ def chooseTorrent(): """ global links, results try: - selection = int(raw_input("\n*** Enter the digit of the torrent to download.\n>")) + selection = int(raw_input("\n*** Enter the digit of the torrent to download.\n> ")) if selection == 98: print "\nStarting over" results = {} @@ -99,6 +102,7 @@ def chooseTorrent(): else: #If anything other than 98, 99, or valid key number entered, loop back to selection input print "\nNot a valid number" chooseTorrent() + except ValueError: print "\nThat is not a digit." chooseTorrent() @@ -106,37 +110,29 @@ def chooseTorrent(): def downloadTorrent(torrent): """ - Grabs the first magnet link and initiates the download + Grabs the first magnet link and initiates the download using the transmissionrpc python library """ # TPB no longer uses torrents as subdomain. Changing script to direct add magnet links #torrentName = search("/torrent/(.*)", torrent) #Strip out first portion of string (/torrent/) #torrentURL = "https://torrents.thepiratebay.se/{}.torrent".format(torrentName.group(1)) #TPB uses subdomain 'torrents' to host .torrent files magnetLinks = [] + torrentPage = requests.get("{}/{}".format(tpb, torrent), verify=False) torrentPageSoup = bs4.BeautifulSoup(torrentPage.content) + for link in torrentPageSoup.find_all('a'): if str(link.get('href')).startswith('magnet:?xt'): magnetLinks.append(link.get('href')) - torrentURL = magnetLinks[0] - print "\n*** Adding magnet link:\n\n{}".format(torrentURL) + + magnetLink = magnetLinks[0] + + print "\n*** Adding magnet link:\n\n{}".format(magnetLink) + + checkTransmission(magnetLink) #urlretrieve(torrentURL, path.basename(torrentURL)) #Save torrent file as same name - checkOS(path.basename(torrentURL)) #Check host operating system for proper torrent client + #checkOS(path.basename(torrentURL)) #Check host operating system for proper torrent client - -def checkOS(torrentDownload): - """ - Checks host operating system and determines how to start the torrent transfer - """ - if operatingSystem() == "Windows": #Windows is finished at this point - openCode = system("start {}".format(torrentDownload)) #Simply open it, default torrent client opens - if openCode == 0: - exit(0) #Clean exit - else: - print "\nYou need a torrent client installed.\n" - exit(1) - else: - checkTransmission(torrentDownload) #For linux systems running transmission-cli def checkTransmission(torrentDownload): """ @@ -145,7 +141,9 @@ def checkTransmission(torrentDownload): whichCode = system("which transmission-remote") print "\n" if whichCode == 0: - system("transmission-remote localhost:9091 -a {}".format(torrentDownload)) + t = transmissionrpc.Client('localhost', port=9091) + t.add_torrent("{}".format(torrentDownload)) + #system("transmission-remote localhost:9091 -a {}".format(torrentDownload)) if __name__ == "__main__":