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 self.in_script_style = False
49 def handle_starttag(self, tag, attrs):
51 if tag == 'div' and attrs.get('id') == 'menu_detalle_buscador':
53 self.subs.append(self.cur)
57 if tag == 'script' or tag == 'style':
58 self.in_script_style = True
61 if attrs.get('id') == 'buscador_detalle':
63 elif attrs.get('id') == 'buscador_detalle_sub':
66 if attrs.get('class') == 'titulo_menu_izq':
68 elif attrs.get('href', '').startswith(self.down_uri):
69 self.cur['url'] = attrs['href']
73 def handle_endtag(self, tag):
75 if tag == 'script' or tag == 'style':
76 self.in_script_style = False
82 def handle_data(self, data):
86 # Hack to handle comments in <script> <style> which don't end
87 # up in handle_comment(), so we just ignore the whole tags
88 if self.in_script_style:
90 if self.attr is not None and data:
91 self.cur[self.attr] = data
93 elif data in ('Downloads:', 'Cds:', 'Comentarios:',
95 self.attr = data[:-1].lower()
96 elif data == 'Subido por:':
102 def subdivx_get_subs(query_str):
106 query = SubDivXQuery(query_str, page_number)
107 url = urllib.urlopen(query.url)
108 parser = SubDivXHTMLParser(query.down_uri)
118 subs.extend(parser.subs)
121 return sorted(subs, key=lambda s: int(s['downloads']), reverse=True)
124 def get_subs(query_str):
125 zip_exts = ('application/zip',)
126 rar_exts = ('application/rar', 'application/x-rar-compressed')
128 for sub in subdivx_get_subs(query_str):
130 - %(titulo)s (%(autor)s - %(fecha)s - %(downloads)s - %(comentarios)s)
134 fname, headers = urllib.urlretrieve(sub['url'])
135 if 'Content-Type' in headers:
136 if headers['Content-Type'] in zip_exts:
137 z = zipfile.ZipFile(fname, 'r')
139 for fn in z.namelist():
140 if fn.endswith('.srt') or fn.endswith('.sub'):
141 if '..' in fn or fn.startswith('/'):
142 print 'Dangerous file name:', fn
144 print 'Extracting', fn, '...'
146 elif headers['Content-Type'] in rar_exts:
147 if subprocess.call(['rar', 'x', fname]) != 0:
148 print 'Error unraring file %s' % fname
150 print 'Unrecognized file type:', headers['Content-Type']
152 print 'No Content-Type!'
155 for q in sys.argv[1:]: