10 def __init__(self, to_search, page_number):
11 self.host = "www.subdivx.com"
12 self.page = "/index.php"
13 self.down_page = "/bajar.php"
24 return 'http://%s%s?%s' % (self.host, self.page,
25 urllib.urlencode(self.query))
28 return self.page + '?' + urllib.urlencode(self.query)
31 return 'http://' + self.host + self.down_page
34 class SubDivXHTMLParser(HTMLParser.HTMLParser):
39 def __init__(self, down_uri):
40 HTMLParser.HTMLParser.__init__(self)
41 self.down_uri = down_uri
47 def handle_starttag(self, tag, attrs):
49 if tag == 'div' and attrs.get('id') == 'menu_detalle_buscador':
51 self.subs.append(self.cur)
56 if attrs.get('id') == 'buscador_detalle':
58 elif attrs.get('id') == 'buscador_detalle_sub':
61 if attrs.get('class') == 'titulo_menu_izq':
63 elif attrs.get('href', '').startswith(self.down_uri):
64 self.cur['url'] = attrs['href']
68 def handle_endtag(self, tag):
74 def handle_data(self, data):
78 if self.attr is not None and data:
79 self.cur[self.attr] = data
81 elif data in ('Downloads:', 'Cds:', 'Comentarios:',
83 self.attr = data[:-1].lower()
84 elif data == 'Subido por:':
90 def subdivx_get_subs(query_str):
94 query = SubDivXQuery(query_str, page_number)
95 url = urllib.urlopen(query.url)
96 parser = SubDivXHTMLParser(query.down_uri)
106 subs.extend(parser.subs)
109 return sorted(subs, key=lambda s: int(s['downloads']), reverse=True)
112 def get_subs(query_str):
113 zip_exts = ('application/zip',)
114 rar_exts = ('application/rar', 'application/x-rar-compressed')
116 for sub in subdivx_get_subs(query_str):
118 - %(title)s (%(autor)s - %(fecha)s - %(downloads)s - %(comentarios)s)
122 fname, headers = urllib.urlretrieve(sub['url'])
123 if 'Content-Type' in headers:
124 if headers['Content-Type'] in zip_exts:
125 z = zipfile.ZipFile(fname, 'r')
127 for fn in z.namelist():
128 if fn.endswith('.srt') or fn.endswith('.sub'):
129 if '..' in fn or fn.startswith('/'):
130 print 'Dangerous file name:', fn
132 print 'Extracting', fn, '...'
134 elif headers['Content-Type'] in rar_exts:
135 if subprocess.call(['rar', 'x', fname]) != 0:
136 print 'Error unraring file %s' % fname
138 print 'Unrecognized file type:', headers['Content-Type']
140 print 'No Content-Type!'
143 for q in sys.argv[1:]: