Skip to content

Instantly share code, notes, and snippets.

@myano
Last active December 27, 2016 12:55
Show Gist options
  • Save myano/2025135 to your computer and use it in GitHub Desktop.
Save myano/2025135 to your computer and use it in GitHub Desktop.
Python script to check for latest episodes of television shows from tpb. Please use responsibility. Code not the cleanest nor most efficient. Improvements to come.
#!/usr/bin/env python
"""
AAAAAA.py
This script, when placed in a folder with other episodes, will find the next
episode number and query thepiratebay for the infohash of the most popular
match.
This script is only used to find shows that are freely available.
I personally enjoy Pioneer One and a bunch more films provided freely over at
http://vodo.net/
"""
import os
import re
import sys
import time
import urllib
from StringIO import StringIO
class Web:
class Grab(urllib.URLopener):
def __init__(self, *args):
self.version = 'Mozilla/5.0 (%s)' % (str(time.time()))
urllib.URLopener.__init__(self, *args)
def http_error_default(self, url, fp, errcode, errmsg, headers):
return urllib.addinfourl(fp, [headers, errcode], "http:" + url)
urllib._urlopener = Grab()
def get(self, uri):
u = urllib.urlopen(uri)
bytes = u.read()
u.close()
return bytes
web = Web()
def main():
## Find the latest episode
current_dir = os.listdir(os.getcwd())
current_dir.sort()
latest = current_dir[-1]
r_num = re.compile(r'(?ims)(s\d\de\d\d)')
matches = r_num.findall(latest)
## increase the episode number by 1
if not matches:
print "Could not find TV shows matching the pattern, s00e00.\n"
sys.exit()
k = matches[0]
n = int(k[-2:])
n += 1
n = str(n)
n = n.zfill(2)
epp = k[:-2] + str(n)
## find out what TV show we are looking for
## assumes pattern of Name.of.show.s44e44.lksdjfls.dkjfls.jkdlf.torrent
r_meh = re.compile(r'(?ims)(.*)s\d\de\d\d')
title = r_meh.findall(latest)
title = title[0]
pre_title = title
chars = {
('.', '_'): '%20',
(',', "'", '-', '"', '!', '@', '$', '#'): '',
}
for char_list in chars:
for char in char_list:
title.replace(char, chars[char_list])
tvshow = title
search = tvshow + unicode(epp)
## Search thepiratebay's website for the most popular torrent matching
## our search first try to obtain 720p copy, if none found obtain
## regular copy
r_torrents = re.compile(r'(?ims)(http[^\s]+\.torrent)')
def gettorrent(suche=""):
if suche:
suche = " " + suche
url = "https://thepiratebay.sx/search/%s%s/0/7/0" % (search, suche)
page = web.get(url)
re_links = re.compile('(?ims)<a href="(\/torrent\/\d+\/\S+)"')
torrent_links = re_links.findall(page)
## find the most popular torrent with the least amount of files
torrent_str = ""
for each in torrent_links:
url2 = "https://thepiratebay.sx" + each
page2 = web.get(url2)
re_files = re.compile('(?ims)return false\;\">(\S+)<\/a><\/dd>')
num_of_files = re_files.findall(page2)
if num_of_files:
num = int(num_of_files[0])
if num < 5:
re_magnets = re.compile('(magnet:\?xt\S+)&tr=')
results = re_magnets.findall(page2)
return results[0]
torrents = gettorrent("720p")
if not torrents:
torrents = gettorrent()
if not torrents:
print "Sorry, but there are no new shows available at this time."
sys.exit()
popular = torrents
tr = popular.find("&tr")
popular = popular[:tr]
show_name = pre_title + unicode(epp)
name_of_file = "newfiles-%s.magnet" % (show_name)
f = open(name_of_file, "w")
f.write(popular)
f.close()
## move it to the appropriate directory, the directory part can be changed
os.rename(name_of_file, "/media/tb2/torrents/newtor/" + name_of_file)
os.system("touch %s.avi" % (show_name))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment