10 def __init__(self, to_search):
11 self.host = "www.subdivx.com"
12 self.page = "/index.php"
13 self.down_page = "/bajar.php"
23 return 'http://%s%s?%s' % (self.host, self.page,
24 urllib.urlencode(self.query))
27 return self.page + '?' + urllib.urlencode(self.query)
30 return 'http://' + self.host + self.down_page
33 class SubDivXHTMLParser(HTMLParser.HTMLParser):
38 def __init__(self, down_uri):
39 HTMLParser.HTMLParser.__init__(self)
40 self.down_uri = down_uri
46 def handle_starttag(self, tag, attrs):
48 if tag == 'div' and attrs.get('id') == 'menu_detalle_buscador':
50 self.subs.append(self.cur)
55 if attrs.get('id') == 'buscador_detalle':
57 elif attrs.get('id') == 'buscador_detalle_sub':
60 if attrs.get('class') == 'titulo_menu_izq':
62 elif attrs.get('href', '').startswith(self.down_uri):
63 self.cur['url'] = attrs['href']
67 def handle_endtag(self, tag):
73 def handle_data(self, data):
76 if self.attr is not None and data:
77 self.cur[self.attr] = data
79 elif data in ('Downloads:', 'Cds:', 'Comentarios:',
81 self.attr = data[:-1].lower()
82 elif data == 'Subido por:':
88 def get_subs(query_str):
89 query = SubDivXQuery(query_str)
91 url = urllib.urlopen(query.url)
93 parser = SubDivXHTMLParser(query.down_uri)
100 zip_exts = ('application/zip',)
101 rar_exts = ('application/rar', 'application/x-rar-compressed')
103 for sub in sorted(parser.subs, key=lambda s: int(s['downloads']), reverse=True):
105 - %(title)s (%(autor)s - %(fecha)s - %(downloads)s - %(comentarios)s)
109 fname, headers = urllib.urlretrieve(sub['url'])
110 if 'Content-Type' in headers:
111 if headers['Content-Type'] in zip_exts:
112 z = zipfile.ZipFile(fname, 'r')
114 for fn in z.namelist():
115 if fn.endswith('.srt') or fn.endswith('.sub'):
116 if '..' in fn or fn.startswith('/'):
117 print 'Dangerous file name:', fn
119 print 'Extracting', fn, '...'
121 elif headers['Content-Type'] in rar_exts:
122 if subprocess.call(['rar', 'x', fname]) != 0:
123 print 'Error unraring file %s' % fname
125 print 'Unrecognized file type:', headers['Content-Type']
127 print 'No Content-Type!'
130 for q in sys.argv[1:]: