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
46 self.in_script_style = False
48 def handle_starttag(self, tag, attrs):
50 if tag == 'div' and attrs.get('id') == 'menu_detalle_buscador':
52 self.subs.append(self.cur)
56 if tag == 'script' or tag == 'style':
57 self.in_script_style = True
60 if attrs.get('id') == 'buscador_detalle':
62 elif attrs.get('id') == 'buscador_detalle_sub':
65 if attrs.get('class') == 'titulo_menu_izq':
67 elif attrs.get('href', '').startswith(self.down_uri):
68 self.cur['url'] = attrs['href']
72 def handle_endtag(self, tag):
74 if tag == 'script' or tag == 'style':
75 self.in_script_style = False
81 def handle_data(self, data):
85 # Hack to handle comments in <script> <style> which don't end
86 # up in handle_comment(), so we just ignore the whole tags
87 if self.in_script_style:
89 if self.attr is not None and data:
90 self.cur[self.attr] = data
92 elif data in ('Downloads:', 'Cds:', 'Comentarios:',
94 self.attr = data[:-1].lower()
95 elif data == 'Subido por:':
101 def subdivx_get_subs(query_str):
105 query = SubDivXQuery(query_str, page_number)
106 url = urllib.urlopen(query.url)
107 parser = SubDivXHTMLParser(query.down_uri)
117 subs.extend(parser.subs)
120 return sorted(subs, key=lambda s: int(s['downloads']), reverse=True)
123 def get_subs(query_str):
124 zip_exts = ('application/zip',)
125 rar_exts = ('application/rar', 'application/x-rar-compressed')
127 for sub in subdivx_get_subs(query_str):
129 - %(title)s (%(autor)s - %(fecha)s - %(downloads)s - %(comentarios)s)
133 fname, headers = urllib.urlretrieve(sub['url'])
134 if 'Content-Type' in headers:
135 if headers['Content-Type'] in zip_exts:
136 z = zipfile.ZipFile(fname, 'r')
138 for fn in z.namelist():
139 if fn.endswith('.srt') or fn.endswith('.sub'):
140 if '..' in fn or fn.startswith('/'):
141 print 'Dangerous file name:', fn
143 print 'Extracting', fn, '...'
145 elif headers['Content-Type'] in rar_exts:
146 if subprocess.call(['rar', 'x', fname]) != 0:
147 print 'Error unraring file %s' % fname
149 print 'Unrecognized file type:', headers['Content-Type']
151 print 'No Content-Type!'
154 for q in sys.argv[1:]: