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):
77 if self.attr is not None and data:
78 self.cur[self.attr] = data
80 elif data in ('Downloads:', 'Cds:', 'Comentarios:',
82 self.attr = data[:-1].lower()
83 elif data == 'Subido por:':
89 def subdivx_get_subs(query_str):
93 query = SubDivXQuery(query_str, page_number)
94 url = urllib.urlopen(query.url)
95 parser = SubDivXHTMLParser(query.down_uri)
105 subs.extend(parser.subs)
108 return sorted(subs, key=lambda s: int(s['downloads']), reverse=True)
111 def get_subs(query_str):
112 zip_exts = ('application/zip',)
113 rar_exts = ('application/rar', 'application/x-rar-compressed')
115 for sub in subdivx_get_subs(query_str):
117 - %(title)s (%(autor)s - %(fecha)s - %(downloads)s - %(comentarios)s)
121 fname, headers = urllib.urlretrieve(sub['url'])
122 if 'Content-Type' in headers:
123 if headers['Content-Type'] in zip_exts:
124 z = zipfile.ZipFile(fname, 'r')
126 for fn in z.namelist():
127 if fn.endswith('.srt') or fn.endswith('.sub'):
128 if '..' in fn or fn.startswith('/'):
129 print 'Dangerous file name:', fn
131 print 'Extracting', fn, '...'
133 elif headers['Content-Type'] in rar_exts:
134 if subprocess.call(['rar', 'x', fname]) != 0:
135 print 'Error unraring file %s' % fname
137 print 'Unrecognized file type:', headers['Content-Type']
139 print 'No Content-Type!'
142 for q in sys.argv[1:]: