added argparsing

master
Lance Allen 9 years ago
parent 89f7233796
commit 1148d0e67b

@ -0,0 +1,3 @@
#!/usr/bin/env python

@ -17,6 +17,7 @@ __author__ = 'LANCE - https://github.com/lalanza808'
# Built-in libraries # Built-in libraries
from os import path from os import path
import argparse
# 3rd party libraries # 3rd party libraries
import requests import requests
@ -34,24 +35,36 @@ choice = ""
# Current/working PirateBay URL # Current/working PirateBay URL
tpb = "https://thepiratebay.se" tpb = "https://thepiratebay.se"
# IP of the machine running transmission. Probably localhost # Torrent server IP; can be any machine running transmission-daemon
transmissionServer = '' # with a firewall inbound allowed to TCP/9091 (transmissionrpc)
rpcserver = 'localhost'
# Squelch HTTPS insecure warnings # Squelch HTTPS insecure warnings
requests.packages.urllib3.disable_warnings() requests.packages.urllib3.disable_warnings()
##################################################
# Parsing and Arguments
parser = argparse.ArgumentParser(description='Scrape The Pirate Bay for torrents.')
parser.add_argument('--search', '-s', dest='searcharg', help='The string to search for on TPB', required=False)
parser.add_argument('--top', '-t', dest='top', action='store_true', help='Automatically grab the torrent with most seeds', required=False)
args = parser.parse_args()
################################################## ##################################################
# Functions # Functions
def checkTransmission(): def checkTransmission():
""" """
Checks to see if transmission-daemon is running on transmissionServer Checks to see if transmission-daemon is running on rpcserver
and and initiates the function to ask user for input and and initiates the function to ask user for input
""" """
try: try:
transmissionrpc.Client(transmissionServer, port=9091) transmissionrpc.Client(rpcserver, port=9091)
getSearchURL() getSearchURL()
except KeyboardInterrupt: except KeyboardInterrupt:
print "\n\nLater bro." print "\n\nLater bro."
@ -67,7 +80,10 @@ def getSearchURL():
Formats string into proper url Formats string into proper url
Gets HTML source of search page for use in the next function Gets HTML source of search page for use in the next function
""" """
searchString = raw_input("[+] What would you like to search?\n>>> ") if args.searcharg:
searchString = args.searcharg
else:
searchString = raw_input("[+] What would you like to search?\n>>> ")
searchURL = "{}/search/{}/0/7/0".format(tpb, searchString) #/0/7/0 tells TPB to sort descending by seeds searchURL = "{}/search/{}/0/7/0".format(tpb, searchString) #/0/7/0 tells TPB to sort descending by seeds
@ -85,24 +101,31 @@ def analyzeURL(source):
print "\n" print "\n"
global links, results global links, results
#Update the links array with the returned torrents
pageSoup = bs4.BeautifulSoup(source) #Create Beautiful Soup object pageSoup = bs4.BeautifulSoup(source) #Create Beautiful Soup object
for link in pageSoup.find_all('a'): #Find all anchor elements in page source for link in pageSoup.find_all('a'): #Find all anchor elements in page source
if link.get('href').startswith('/torrent'): #Filter items that don't start with /torrent if link.get('href').startswith('/torrent'): #Filter items that don't start with /torrent
links.append(link.get('href')) #Set the initial results to array 'links' links.append(link.get('href')) #Set the initial results to array 'links'
for number,link in enumerate(links): #Enumerate the array so the numbers start at 0 #If -t is supplied, bypass this section of code and go on to download the top torrent
results.update({number:link}) #Append results to results dictionary if args.top and links:
print "({}) {}".format(number, path.basename(link)) downloadTorrent(links[0])
else:
if results: #If dict is not empty, continue with script for number,link in enumerate(links): #Enumerate the array so the numbers start at 0
print "\n(98) Search again" results.update({number:link}) #Append results to results dictionary
print "(99) Exit" print "({}) {}".format(number, path.basename(link))
chooseTorrent()
else: #If dict is empty (no results from search) re-run script if results: #If dict is not empty, continue with script
print "\nNo results found. Try again." print "\n(98) Search again"
results = {} print "(99) Exit"
links = [] chooseTorrent()
getSearchURL() #Loop back to script start else: #If dict is empty (no results from search) re-run script
print "\nNo results found. Try again."
results = {}
links = []
args.searcharg = ''
getSearchURL() #Loop back to script start
def chooseTorrent(): def chooseTorrent():
@ -117,6 +140,7 @@ def chooseTorrent():
print "\nStarting over" print "\nStarting over"
results = {} results = {}
links = [] links = []
args.searcharg = ''
getSearchURL() #Loop back to start getSearchURL() #Loop back to start
elif selection == 99: elif selection == 99:
print "\nBye.\n" print "\nBye.\n"
@ -151,9 +175,11 @@ def downloadTorrent(torrent):
print "\n[+] Adding magnet link for torrent:\n\n{}".format(torrent) print "\n[+] Adding magnet link for torrent:\n\n{}".format(torrent)
transmissionrpc.Client(transmissionServer).add_torrent(magnetLink) transmissionrpc.Client(rpcserver).add_torrent(magnetLink)
print "\n[.] Done!\n"
print "\n[.] Done!\n" exit(0)
if __name__ == "__main__": if __name__ == "__main__":
checkTransmission() checkTransmission()

@ -49,4 +49,4 @@ Add a cron job to run //removeFinishedTorrents.py// every 5 minutes. Hook it int
* Refactor code; not a fan of the spaghetti code functions * Refactor code; not a fan of the spaghetti code functions
* Comment script better; for personal reasons. I hate being confused 6 months later * Comment script better; for personal reasons. I hate being confused 6 months later
* Maybe add a setup script. Maybe * Pushbullet read/download new torrents

Loading…
Cancel
Save