mirror of
https://github.com/vikstrous/pirate-get
synced 2025-01-10 10:04:21 +01:00
Merge pull request #29 from alexpeak/printTorrentInfo
Add options to print torrent descriptions & files
This commit is contained in:
commit
594f8d73e6
159
pirate-get.py
159
pirate-get.py
@ -101,6 +101,7 @@ def main():
|
|||||||
try:
|
try:
|
||||||
sizes = []
|
sizes = []
|
||||||
uploaded = []
|
uploaded = []
|
||||||
|
identifiers = []
|
||||||
for page in xrange(pages):
|
for page in xrange(pages):
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -133,6 +134,7 @@ def main():
|
|||||||
# print res
|
# print res
|
||||||
sizes.extend([match.replace(" ", " ") for match in re.findall("(?<=Size )[0-9.]+\ \;[KMGT]*[i ]*B",res)])
|
sizes.extend([match.replace(" ", " ") for match in re.findall("(?<=Size )[0-9.]+\ \;[KMGT]*[i ]*B",res)])
|
||||||
uploaded.extend([match.replace(" ", " ") for match in re.findall("(?<=Uploaded ).+(?=\, Size)",res)])
|
uploaded.extend([match.replace(" ", " ") for match in re.findall("(?<=Uploaded ).+(?=\, Size)",res)])
|
||||||
|
identifiers.extend([match.replace(" ", " ") for match in re.findall("(?<=/torrent/)[0-9]+(?=/)",res)])
|
||||||
# pprint(sizes); print len(sizes)
|
# pprint(sizes); print len(sizes)
|
||||||
# pprint(uploaded); print len(uploaded)
|
# pprint(uploaded); print len(uploaded)
|
||||||
state = "seeds"
|
state = "seeds"
|
||||||
@ -154,7 +156,7 @@ def main():
|
|||||||
exit()
|
exit()
|
||||||
|
|
||||||
# return the sizes in a spearate list
|
# return the sizes in a spearate list
|
||||||
return res_l, sizes, uploaded
|
return res_l, sizes, uploaded, identifiers
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
@ -221,7 +223,7 @@ def main():
|
|||||||
for mirror in mirrors:
|
for mirror in mirrors:
|
||||||
try:
|
try:
|
||||||
print("Trying " + mirror)
|
print("Trying " + mirror)
|
||||||
mags, sizes, uploaded = remote(args, mirror)
|
mags, sizes, uploaded, identifiers = remote(args, mirror)
|
||||||
break
|
break
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
print(format(e))
|
print(format(e))
|
||||||
@ -231,31 +233,73 @@ def main():
|
|||||||
print("no results")
|
print("no results")
|
||||||
return
|
return
|
||||||
# enhanced print output with column titles
|
# enhanced print output with column titles
|
||||||
print("%5s %6s %6s %-5s %-11s %-11s %s" \
|
def print_search_results():
|
||||||
% ( "LINK", "SEED", "LEECH", "RATIO", "SIZE", "UPLOAD", "NAME"),
|
print("%5s %6s %6s %-5s %-11s %-11s %s" \
|
||||||
color="header")
|
% ( "LINK", "SEED", "LEECH", "RATIO", "SIZE", "UPLOAD", "NAME"),
|
||||||
cur_color = "zebra_0"
|
color="header")
|
||||||
for m in range(len(mags)):
|
cur_color = "zebra_0"
|
||||||
magnet = mags[m]
|
for m in range(len(mags)):
|
||||||
no_seeders = int(magnet[1])
|
magnet = mags[m]
|
||||||
no_leechers = int(magnet[2])
|
no_seeders = int(magnet[1])
|
||||||
name = re.search("dn=([^\&]*)", magnet[0])
|
no_leechers = int(magnet[2])
|
||||||
|
name = re.search("dn=([^\&]*)", magnet[0])
|
||||||
|
|
||||||
# compute the S/L ratio (Higher is better)
|
# compute the S/L ratio (Higher is better)
|
||||||
try:
|
try:
|
||||||
ratio = no_seeders/no_leechers
|
ratio = no_seeders/no_leechers
|
||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
ratio = -1
|
ratio = -1
|
||||||
|
|
||||||
# Alternate between colors
|
# Alternate between colors
|
||||||
cur_color = "zebra_0" if (cur_color == "zebra_1") else "zebra_1"
|
cur_color = "zebra_0" if (cur_color == "zebra_1") else "zebra_1"
|
||||||
|
|
||||||
torrent_name = urllib.unquote(name.group(1).encode('ascii')) \
|
torrent_name = urllib.unquote(name.group(1).encode('ascii')) \
|
||||||
.decode('utf-8').replace("+", " ")
|
.decode('utf-8').replace("+", " ")
|
||||||
# enhanced print output with justified columns
|
# enhanced print output with justified columns
|
||||||
print ("%5d %6d %6d %5.1f %-11s %-11s %s" % (
|
print ("%5d %6d %6d %5.1f %-11s %-11s %s" % (
|
||||||
m, no_seeders, no_leechers, ratio ,sizes[m],
|
m, no_seeders, no_leechers, ratio ,sizes[m],
|
||||||
uploaded[m], torrent_name), color=cur_color)
|
uploaded[m], torrent_name), color=cur_color)
|
||||||
|
def print_descriptions(chosen_links):
|
||||||
|
for link in chosen_links:
|
||||||
|
path = '/torrent/' + identifiers[int(link)] + '/'
|
||||||
|
request = urllib2.Request(mirror + path)
|
||||||
|
request.add_header('Accept-encoding', 'gzip')
|
||||||
|
f = urllib2.urlopen(request)
|
||||||
|
if f.info().get('Content-Encoding') == 'gzip':
|
||||||
|
buf = StringIO(f.read())
|
||||||
|
f = gzip.GzipFile(fileobj=buf)
|
||||||
|
res = f.read()
|
||||||
|
name = re.search("dn=([^\&]*)", mags[int(link)][0])
|
||||||
|
torrent_name = urllib.unquote(name.group(1).encode('ascii')) \
|
||||||
|
.decode('utf-8').replace("+", " ")
|
||||||
|
desc = re.search(r"<div class=\"nfo\">\s*<pre>(.+?)(?=</pre>)", res, re.DOTALL).group(1)
|
||||||
|
# Replace HTML links with markdown style versions
|
||||||
|
desc = re.sub(r"<a href=\"\s*([^\"]+?)\s*\"[^>]*>(\s*)([^<]+?)(\s*)</a>", r"\2[\3](\1)\4", desc)
|
||||||
|
print ('Description for "' + torrent_name + '":', color="zebra_1")
|
||||||
|
print (desc, color="zebra_0")
|
||||||
|
|
||||||
|
def print_fileLists(chosen_links):
|
||||||
|
for link in chosen_links:
|
||||||
|
path = '/ajax_details_filelist.php'
|
||||||
|
query = '?id=' + identifiers[int(link)]
|
||||||
|
request = urllib2.Request(mirror + path + query)
|
||||||
|
request.add_header('Accept-encoding', 'gzip')
|
||||||
|
f = urllib2.urlopen(request)
|
||||||
|
if f.info().get('Content-Encoding') == 'gzip':
|
||||||
|
buf = StringIO(f.read())
|
||||||
|
f = gzip.GzipFile(fileobj=buf)
|
||||||
|
res = f.read().replace(" ", " ")
|
||||||
|
files = re.findall(r"<td align=\"left\">\s*([^<]+?)\s*</td><td align=\"right\">\s*([^<]+?)\s*</tr>", res)
|
||||||
|
name = re.search("dn=([^\&]*)", mags[int(link)][0])
|
||||||
|
torrent_name = urllib.unquote(name.group(1).encode('ascii')) \
|
||||||
|
.decode('utf-8').replace("+", " ")
|
||||||
|
print ('Files in "' + torrent_name + '":', color="zebra_1")
|
||||||
|
cur_color = "zebra_0"
|
||||||
|
for f in files:
|
||||||
|
print ("%-11s %s" % (f[1], f[0]), color=cur_color)
|
||||||
|
cur_color = "zebra_0" if (cur_color == "zebra_1") else "zebra_1"
|
||||||
|
|
||||||
|
print_search_results()
|
||||||
|
|
||||||
if args.first:
|
if args.first:
|
||||||
print("Choosing first result");
|
print("Choosing first result");
|
||||||
@ -264,22 +308,57 @@ def main():
|
|||||||
print("Downloading all results");
|
print("Downloading all results");
|
||||||
choices = range(0, len(mags))
|
choices = range(0, len(mags))
|
||||||
else:
|
else:
|
||||||
try:
|
# New input loop to support different link options
|
||||||
l = raw_input("Select link(s): ")
|
while True:
|
||||||
except KeyboardInterrupt :
|
try:
|
||||||
print("\nCancelled.")
|
l = raw_input("Select link(s) (Type 'h' for more options): ")
|
||||||
exit()
|
except KeyboardInterrupt :
|
||||||
|
print("\nCancelled.")
|
||||||
|
exit()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Very permissive handling
|
# Very permissive handling
|
||||||
# Substitute multiple consecutive spaces or commas for single comma
|
# Check for any occurances or d, f, or p
|
||||||
l = re.sub("[ ,]+", ",", l)
|
cmd_code_match = re.search(r'([hdfp])', l, flags=re.IGNORECASE)
|
||||||
# Remove anything that isn't an integer or comma.
|
if cmd_code_match:
|
||||||
l = re.sub("[^0-9,]", "", l)
|
code = cmd_code_match.group(0).lower()
|
||||||
# Turn into list
|
else:
|
||||||
choices = l.split(",")
|
code = None
|
||||||
except Exception:
|
# Clean up command codes
|
||||||
choices = ()
|
l = re.sub(r"^[hdfp, ]*|[hdfp, ]*$", "", l)
|
||||||
|
# Substitute multiple consecutive spaces or commas for single comma
|
||||||
|
l = re.sub("[ ,]+", ",", l)
|
||||||
|
# Remove anything that isn't an integer or comma.
|
||||||
|
l = re.sub("[^0-9,]", "", l)
|
||||||
|
# Turn into list
|
||||||
|
choices = l.split(",")
|
||||||
|
# Act on option, if supplied
|
||||||
|
if code == 'h':
|
||||||
|
print("Options:")
|
||||||
|
print("<links>: Download selected torrents")
|
||||||
|
print("[d<links>]: Get descriptions")
|
||||||
|
print("[f<links>]: Get files")
|
||||||
|
print("[p] Print search results")
|
||||||
|
continue
|
||||||
|
elif code == 'd':
|
||||||
|
print_descriptions(choices)
|
||||||
|
continue
|
||||||
|
elif code == 'f':
|
||||||
|
print_fileLists(choices)
|
||||||
|
continue
|
||||||
|
elif code == 'p':
|
||||||
|
print_search_results()
|
||||||
|
continue
|
||||||
|
elif not l:
|
||||||
|
print('No links entered!')
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
except Exception, e:
|
||||||
|
print('Exception:')
|
||||||
|
print(str(e))
|
||||||
|
choices = ()
|
||||||
|
break;
|
||||||
|
|
||||||
if config.get('SaveToFile', 'enabled'):
|
if config.get('SaveToFile', 'enabled'):
|
||||||
# Save to file is enabled
|
# Save to file is enabled
|
||||||
@ -303,7 +382,7 @@ def main():
|
|||||||
os.system("""transmission-remote --add "%s" """ % (url))
|
os.system("""transmission-remote --add "%s" """ % (url))
|
||||||
os.system("transmission-remote -l")
|
os.system("transmission-remote -l")
|
||||||
elif args.command:
|
elif args.command:
|
||||||
os.system(args.command % (url))
|
os.system(args.command % (url))
|
||||||
else:
|
else:
|
||||||
webbrowser.open(url)
|
webbrowser.open(url)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user