]> git.llucax.com Git - software/subdivxget.git/blob - subdivxget
5f67edbfeb8852a47a2cbfcf89b1e83c522dc8d1
[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
47         def handle_starttag(self, tag, attrs):
48                 attrs = dict(attrs)
49                 if tag == 'div' and attrs.get('id') == 'menu_detalle_buscador':
50                         self.cur = dict()
51                         self.subs.append(self.cur)
52                         self.parsing = True
53                 if not self.parsing:
54                         return
55                 if tag == 'div':
56                         if attrs.get('id') == 'buscador_detalle':
57                                 self.parsing = True
58                         elif attrs.get('id') == 'buscador_detalle_sub':
59                                 self.attr = 'desc'
60                 elif tag == 'a':
61                         if attrs.get('class') == 'titulo_menu_izq':
62                                 self.attr = 'title'
63                         elif attrs.get('href', '').startswith(self.down_uri):
64                                 self.cur['url'] = attrs['href']
65                 if self.parsing:
66                         self.depth += 1
67
68         def handle_endtag(self, tag):
69                 if self.parsing:
70                         self.depth -= 1
71                 if self.depth == 0:
72                         self.parsing = False
73
74         def handle_data(self, data):
75                 if self.parsing:
76                         data = data.strip()
77                         if self.attr is not None and data:
78                                 self.cur[self.attr] = data
79                                 self.attr = None
80                         elif data in ('Downloads:', 'Cds:', 'Comentarios:',
81                                         'Formato:'):
82                                 self.attr = data[:-1].lower()
83                         elif data == 'Subido por:':
84                                 self.attr = 'autor'
85                         elif data == 'el':
86                                 self.attr = 'fecha'
87
88
89 def subdivx_get_subs(query_str):
90         page_number = 1
91         subs = []
92         while True:
93                 query = SubDivXQuery(query_str, page_number)
94                 url = urllib.urlopen(query.url)
95                 parser = SubDivXHTMLParser(query.down_uri)
96
97                 for line in url:
98                         parser.feed(line)
99
100                 url.close()
101
102                 if not parser.subs:
103                         break
104
105                 subs.extend(parser.subs)
106                 page_number += 1
107
108         return sorted(subs, key=lambda s: int(s['downloads']), reverse=True)
109
110
111 def get_subs(query_str):
112         zip_exts = ('application/zip',)
113         rar_exts = ('application/rar', 'application/x-rar-compressed')
114
115         for sub in subdivx_get_subs(query_str):
116                 print '''\
117         - %(title)s (%(autor)s - %(fecha)s - %(downloads)s - %(comentarios)s)
118           %(desc)s
119                 DOWNLOADING ...
120         ''' % sub
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')
125                                 z.printdir()
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
130                                                         continue
131                                                 print 'Extracting', fn, '...'
132                                                 z.extract(fn)
133                         elif headers['Content-Type'] in rar_exts:
134                                 if subprocess.call(['rar', 'x', fname]) != 0:
135                                         print 'Error unraring file %s' % fname
136                         else:
137                                 print 'Unrecognized file type:', headers['Content-Type']
138                 else:
139                         print 'No Content-Type!'
140
141
142 for q in sys.argv[1:]:
143         get_subs(q)
144