]> git.llucax.com Git - software/subdivxget.git/blob - subdivxget
1aecc66b904811165ec5b5f1bfd44798cd551d42
[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 not self.parsing:
76                         return
77                 data = data.strip()
78                 if self.attr is not None and data:
79                         self.cur[self.attr] = data
80                         self.attr = None
81                 elif data in ('Downloads:', 'Cds:', 'Comentarios:',
82                                 'Formato:'):
83                         self.attr = data[:-1].lower()
84                 elif data == 'Subido por:':
85                         self.attr = 'autor'
86                 elif data == 'el':
87                         self.attr = 'fecha'
88
89
90 def subdivx_get_subs(query_str):
91         page_number = 1
92         subs = []
93         while True:
94                 query = SubDivXQuery(query_str, page_number)
95                 url = urllib.urlopen(query.url)
96                 parser = SubDivXHTMLParser(query.down_uri)
97
98                 for line in url:
99                         parser.feed(line)
100
101                 url.close()
102
103                 if not parser.subs:
104                         break
105
106                 subs.extend(parser.subs)
107                 page_number += 1
108
109         return sorted(subs, key=lambda s: int(s['downloads']), reverse=True)
110
111
112 def get_subs(query_str):
113         zip_exts = ('application/zip',)
114         rar_exts = ('application/rar', 'application/x-rar-compressed')
115
116         for sub in subdivx_get_subs(query_str):
117                 print '''\
118         - %(title)s (%(autor)s - %(fecha)s - %(downloads)s - %(comentarios)s)
119           %(desc)s
120                 DOWNLOADING ...
121         ''' % sub
122                 fname, headers = urllib.urlretrieve(sub['url'])
123                 if 'Content-Type' in headers:
124                         if headers['Content-Type'] in zip_exts:
125                                 z = zipfile.ZipFile(fname, 'r')
126                                 z.printdir()
127                                 for fn in z.namelist():
128                                         if fn.endswith('.srt') or fn.endswith('.sub'):
129                                                 if '..' in fn or fn.startswith('/'):
130                                                         print 'Dangerous file name:', fn
131                                                         continue
132                                                 print 'Extracting', fn, '...'
133                                                 z.extract(fn)
134                         elif headers['Content-Type'] in rar_exts:
135                                 if subprocess.call(['rar', 'x', fname]) != 0:
136                                         print 'Error unraring file %s' % fname
137                         else:
138                                 print 'Unrecognized file type:', headers['Content-Type']
139                 else:
140                         print 'No Content-Type!'
141
142
143 for q in sys.argv[1:]:
144         get_subs(q)
145