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
35 class SubDivXHTMLParser(HTMLParser.HTMLParser):
40 def __init__(self, down_uri):
41 HTMLParser.HTMLParser.__init__(self)
42 self.down_uri = down_uri
48 def handle_starttag(self, tag, attrs):
50 if tag == 'div' and attrs.get('id') == 'menu_detalle_buscador':
51 #self.cur = Subtitle()
53 self.subs.append(self.cur)
58 if attrs.get('id') == 'buscador_detalle':
60 elif attrs.get('id') == 'buscador_detalle_sub':
63 if attrs.get('class') == 'titulo_menu_izq':
65 elif attrs.get('href', '').startswith(self.down_uri):
66 self.cur['url'] = attrs['href']
70 def handle_endtag(self, tag):
76 def handle_data(self, data):
79 if self.attr is not None and data:
80 self.cur[self.attr] = data
82 #self.cur[self.attr] = self.cur.get(self.attr, '') + data.strip()
83 #setattr(self.cur, self.attr, data.strip())
84 elif data in ('Downloads:', 'Cds:', 'Comentarios:',
86 self.attr = data[:-1].lower()
87 elif data == 'Subido por:':
93 def get_subs(query_str):
94 query = SubDivXQuery(query_str)
96 url = urllib.urlopen(query.url)
98 parser = SubDivXHTMLParser(query.down_uri)
105 zip_exts = ('application/zip',)
106 rar_exts = ('application/rar', 'application/x-rar-compressed')
108 for sub in sorted(parser.subs, key=lambda s: int(s['downloads']), reverse=True):
110 - %(title)s (%(autor)s - %(fecha)s - %(downloads)s - %(comentarios)s)
114 fname, headers = urllib.urlretrieve(sub['url'])
115 if 'Content-Type' in headers:
116 if headers['Content-Type'] in zip_exts:
117 z = zipfile.ZipFile(fname, 'r')
119 for fn in z.namelist():
120 if fn.endswith('.srt') or fn.endswith('.sub'):
121 if '..' in fn or fn.startswith('/'):
122 print 'Dangerous file name:', fn
124 print 'Extracting', fn, '...'
126 elif headers['Content-Type'] in rar_exts:
127 if subprocess.call(['rar', 'x', fname]) != 0:
128 print 'Error unraring file %s' % fname
130 print 'Unrecognized file type:', headers['Content-Type']
132 print 'No Content-Type!'
135 for q in sys.argv[1:]: