]> git.llucax.com Git - software/subdivxget.git/blob - subdivxget
Ignore script and style tags
[software/subdivxget.git] / subdivxget
1 #!/usr/bin/env python
2
3 import sys
4 import urllib
5 import zipfile
6 import subprocess
7 import HTMLParser
8
9 class SubDivXQuery:
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"
14                 self.query = dict(
15                         buscar = to_search,
16                         pg = page_number,
17                         accion = 5,
18                         masdesc = '',
19                         subtitulos = 1,
20                         realiza_b = 1,
21                 )
22         @property
23         def url(self):
24                 return 'http://%s%s?%s' % (self.host, self.page,
25                                 urllib.urlencode(self.query))
26         @property
27         def page_uri(self):
28                 return self.page + '?' + urllib.urlencode(self.query)
29         @property
30         def down_uri(self):
31                 return 'http://' + self.host + self.down_page
32
33
34 class SubDivXHTMLParser(HTMLParser.HTMLParser):
35
36         IDLE = 1
37         HEADER = 2
38
39         def __init__(self, down_uri):
40                 HTMLParser.HTMLParser.__init__(self)
41                 self.down_uri = down_uri
42                 self.depth = 0
43                 self.parsing = False
44                 self.subs = []
45                 self.attr = None
46                 self.in_script_style = False
47
48         def handle_starttag(self, tag, attrs):
49                 attrs = dict(attrs)
50                 if tag == 'div' and attrs.get('id') == 'menu_detalle_buscador':
51                         self.cur = dict()
52                         self.subs.append(self.cur)
53                         self.parsing = True
54                 if not self.parsing:
55                         return
56                 if tag == 'script' or tag == 'style':
57                         self.in_script_style = True
58                         return
59                 if tag == 'div':
60                         if attrs.get('id') == 'buscador_detalle':
61                                 self.parsing = True
62                         elif attrs.get('id') == 'buscador_detalle_sub':
63                                 self.attr = 'desc'
64                 elif tag == 'a':
65                         if attrs.get('class') == 'titulo_menu_izq':
66                                 self.attr = 'title'
67                         elif attrs.get('href', '').startswith(self.down_uri):
68                                 self.cur['url'] = attrs['href']
69                 if self.parsing:
70                         self.depth += 1
71
72         def handle_endtag(self, tag):
73                 if self.parsing:
74                         if tag == 'script' or tag == 'style':
75                                 self.in_script_style = False
76                                 return
77                         self.depth -= 1
78                 if self.depth == 0:
79                         self.parsing = False
80
81         def handle_data(self, data):
82                 if not self.parsing:
83                         return
84                 data = data.strip()
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:
88                         return
89                 if self.attr is not None and data:
90                         self.cur[self.attr] = data
91                         self.attr = None
92                 elif data in ('Downloads:', 'Cds:', 'Comentarios:',
93                                 'Formato:'):
94                         self.attr = data[:-1].lower()
95                 elif data == 'Subido por:':
96                         self.attr = 'autor'
97                 elif data == 'el':
98                         self.attr = 'fecha'
99
100
101 def subdivx_get_subs(query_str):
102         page_number = 1
103         subs = []
104         while True:
105                 query = SubDivXQuery(query_str, page_number)
106                 url = urllib.urlopen(query.url)
107                 parser = SubDivXHTMLParser(query.down_uri)
108
109                 for line in url:
110                         parser.feed(line)
111
112                 url.close()
113
114                 if not parser.subs:
115                         break
116
117                 subs.extend(parser.subs)
118                 page_number += 1
119
120         return sorted(subs, key=lambda s: int(s['downloads']), reverse=True)
121
122
123 def get_subs(query_str):
124         zip_exts = ('application/zip',)
125         rar_exts = ('application/rar', 'application/x-rar-compressed')
126
127         for sub in subdivx_get_subs(query_str):
128                 print '''\
129         - %(title)s (%(autor)s - %(fecha)s - %(downloads)s - %(comentarios)s)
130           %(desc)s
131                 DOWNLOADING ...
132         ''' % sub
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')
137                                 z.printdir()
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
142                                                         continue
143                                                 print 'Extracting', fn, '...'
144                                                 z.extract(fn)
145                         elif headers['Content-Type'] in rar_exts:
146                                 if subprocess.call(['rar', 'x', fname]) != 0:
147                                         print 'Error unraring file %s' % fname
148                         else:
149                                 print 'Unrecognized file type:', headers['Content-Type']
150                 else:
151                         print 'No Content-Type!'
152
153
154 for q in sys.argv[1:]:
155         get_subs(q)
156