]> git.llucax.com Git - software/subdivxget.git/blobdiff - subdivxget
Improve field boundary detection
[software/subdivxget.git] / subdivxget
index 5f67edbfeb8852a47a2cbfcf89b1e83c522dc8d1..4ebcca5a71abf2bd94aa04b9c93b1003af36f240 100755 (executable)
@@ -43,6 +43,9 @@ class SubDivXHTMLParser(HTMLParser.HTMLParser):
                self.parsing = False
                self.subs = []
                self.attr = None
+               self.attr_depth = 0
+               self.cur = None
+               self.in_script_style = False
 
        def handle_starttag(self, tag, attrs):
                attrs = dict(attrs)
@@ -52,38 +55,67 @@ class SubDivXHTMLParser(HTMLParser.HTMLParser):
                        self.parsing = True
                if not self.parsing:
                        return
+               if tag == 'script' or tag == 'style':
+                       self.in_script_style = True
+                       return
                if tag == 'div':
                        if attrs.get('id') == 'buscador_detalle':
                                self.parsing = True
                        elif attrs.get('id') == 'buscador_detalle_sub':
                                self.attr = 'desc'
+                               self.attr_depth = self.depth + 1
+                               self.cur[self.attr] = ''
                elif tag == 'a':
                        if attrs.get('class') == 'titulo_menu_izq':
-                               self.attr = 'title'
+                               self.attr = 'titulo'
+                               self.attr_depth = self.depth + 1
+                               self.cur[self.attr] = ''
                        elif attrs.get('href', '').startswith(self.down_uri):
                                self.cur['url'] = attrs['href']
-               if self.parsing:
+               # br are usually not closed, so ignore them in depth calculation
+               if self.parsing and tag != 'br':
                        self.depth += 1
 
        def handle_endtag(self, tag):
                if self.parsing:
-                       self.depth -= 1
+                       if tag == 'script' or tag == 'style':
+                               self.in_script_style = False
+                               return
+                       if self.depth == self.attr_depth:
+                               self.attr = None
+                               self.attr_depth = 0
+                       # see comment in handle_starttag()
+                       if tag != 'br':
+                               self.depth -= 1
                if self.depth == 0:
                        self.parsing = False
 
        def handle_data(self, data):
-               if self.parsing:
-                       data = data.strip()
-                       if self.attr is not None and data:
-                               self.cur[self.attr] = data
+               if not self.parsing:
+                       return
+               data = data.strip()
+               # Hack to handle comments in <script> <style> which don't end
+               # up in handle_comment(), so we just ignore the whole tags
+               if self.in_script_style:
+                       return
+               if self.attr is not None and data:
+                       self.cur[self.attr] += ' ' + data
+                       if self.attr_depth == 0:
+                               self.cur[self.attr] = self.cur[self.attr].strip()
                                self.attr = None
-                       elif data in ('Downloads:', 'Cds:', 'Comentarios:',
-                                       'Formato:'):
-                               self.attr = data[:-1].lower()
-                       elif data == 'Subido por:':
-                               self.attr = 'autor'
-                       elif data == 'el':
-                               self.attr = 'fecha'
+                               self.attr_depth = 0
+               elif data in ('Downloads:', 'Cds:', 'Comentarios:', 'Formato:'):
+                       self.attr = data[:-1].lower()
+                       self.attr_depth = 0
+                       self.cur[self.attr] = ''
+               elif data == 'Subido por:':
+                       self.attr = 'autor'
+                       self.attr_depth = 0
+                       self.cur[self.attr] = ''
+               elif data == 'el':
+                       self.attr = 'fecha'
+                       self.attr_depth = 0
+                       self.cur[self.attr] = ''
 
 
 def subdivx_get_subs(query_str):
@@ -114,7 +146,7 @@ def get_subs(query_str):
 
        for sub in subdivx_get_subs(query_str):
                print '''\
-       - %(title)s (%(autor)s - %(fecha)s - %(downloads)s - %(comentarios)s)
+       - %(titulo)s (%(autor)s - %(fecha)s - %(downloads)s - %(comentarios)s)
          %(desc)s
                DOWNLOADING ...
        ''' % sub