update-hib.py
author Fabien Ninoles <fabien@tzone.org>
Sun, 26 Aug 2012 12:23:05 -0400
changeset 0 1e76c59aa3a6
child 1 fb1ab147b2dd
permissions -rwxr-xr-x
Initial version: parse tidy file and select a suitable download url.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     1
#!/usr/bin/python3
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     2
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     3
from html.parser import HTMLParser
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     4
from pprint import pprint
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     5
import xml.dom
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     6
from itertools import chain
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     7
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     8
class Node:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
     9
    def __init__(self, **args):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    10
        self.childs = []
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    11
        self.attrs = {}
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    12
        for arg in args:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    13
            setattr(self, arg, args[arg])
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    14
        if self.name == "div" and "class" in self.attrs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    15
            self.tag = self.name
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    16
            self.name = self.attrs["class"]
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    17
            del self.attrs["class"]
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    18
        else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    19
            self.tag = self.name
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    20
    def format(self, prefix = ""):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    21
        res = prefix + "<" + self.name
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    22
        for attr in self.attrs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    23
            if self.attrs[attr]:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    24
                res += "\n" + prefix + "  " + attr + '="' + self.attrs[attr] + '"'
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    25
            else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    26
                res += "\n" + prefix + "  " + attr
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    27
        if self.name == "data":
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    28
            res += ">" + self.data + "</" + self.name + ">"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    29
        elif self.childs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    30
            res += ">"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    31
            for child in self.childs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    32
                res += "\n" + child.format(prefix + "  ")
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    33
            res += "\n" + prefix + "</" + self.name + ">"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    34
        else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    35
            res += "/>"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    36
        return res
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    37
    def find(self, prefix):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    38
        for child in self.childs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    39
            if child.name.startswith(prefix):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    40
                yield child
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    41
    def __getattr__(self, name):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    42
        for child in self.childs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    43
            if child.name == name:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    44
                setattr(self, name, child)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    45
                return child
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    46
        raise AttributeError(name)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    47
    def __repr__(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    48
        return self.format()
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    49
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    50
class BundleParser(HTMLParser):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    51
    def __init__(self, **args):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    52
        super(BundleParser, self).__init__(**args)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    53
        self.dom = Node(name = "root",
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    54
                        childs = [],
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    55
                        parent = None)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    56
        self.current = self.dom
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    57
        self.depth = 1
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    58
    def handle_starttag(self, tag, attrs):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    59
        # print("+" * self.depth,tag)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    60
        new = Node(name = tag,
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    61
                   attrs = dict(attrs),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    62
                   childs = [],
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    63
                   parent = self.current)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    64
        self.current.childs.append(new)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    65
        self.current = new
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    66
        self.depth += 1
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    67
    def handle_endtag(self, tag):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    68
        while tag != self.current.tag:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    69
            print("*** Skipping", self.current.tag,"; looking for",tag)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    70
            self.current = self.current.parent
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    71
            self.depth-=1
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    72
            # print("-" * self.depth,self.current.tag)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    73
            assert(self.current != self.dom)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    74
        assert(self.current.tag == tag)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    75
        self.depth-=1
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    76
        # print("-" * self.depth,tag)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    77
        self.current = self.current.parent
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    78
    def handle_data(self, data):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    79
        if data.strip():
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    80
            self.current.childs.append(Node(name = "data", data = data, childs = []))
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    81
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    82
def findRows(dom):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    83
    for child in dom.childs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    84
        try:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    85
            if child.name[:4] == "row ":
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    86
                yield child
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    87
            else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    88
                for row in findRows(child):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    89
                    yield row
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    90
        except KeyError:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    91
            pass
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    92
            for row in findRows(child):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    93
                yield row
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    94
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    95
class Download:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    96
    subst = { "arc32"         : ("x86",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    97
              "arc64"         : ("x64",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    98
              "i386.deb"      : ("x86","deb"),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    99
              "x86_64.deb"    : ("x64", "deb"),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   100
              "i686.rpm"      : ("x86", "rpm"),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   101
              ".i386.rpm"     : ("x86", "rpm"),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   102
              "x86_64.rpm"    : ("x64", "rpm"),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   103
              ".x86_64.rpm"   : ("x64", "rpm"),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   104
              "i386.tar.gz"   : ("x86", "tgz"),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   105
              "x86_64.tar.gz" : ("x64", "tgz"),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   106
              ".tar.gz"       : ("tgz",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   107
              ".deb"          : ("deb",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   108
              ".rpm"          : ("rpm",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   109
              "32-bit"        : ("x86",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   110
              "64-bit"        : ("x64",),              
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   111
              }
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   112
    def __init__(self, dltype, dom):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   113
        self.dltype = dltype
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   114
        self.id = dom.name[len("download "):]
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   115
        button = list(dom.find("flexbtn "))[0]
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   116
        desc = button.span.data.data
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   117
        self.id += " " + desc
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   118
        def cleanup(attr):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   119
            attr = attr.strip()
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   120
            if attr not in ("Download","small",""):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   121
                for s in self.subst.get(attr,(attr,)):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   122
                    yield s
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   123
        self.attrs = set(chain.from_iterable(cleanup(attr) for attr in self.id.split(" ")))
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   124
        urls = button.a.attrs
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   125
        self.torrent = urls["data-bt"]
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   126
        self.web = urls["data-web"]
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   127
        details = dom.dldetails.dlsize
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   128
        if details.childs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   129
            self.size = details.span.data.data
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   130
            self.md5 = details.a.attrs["href"]
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   131
        else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   132
            self.size = "Unknown"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   133
            self.md5 = "Unknown"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   134
    def score(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   135
        if self.dltype == "audio":
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   136
            if "FLAC" in self.attrs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   137
                return 2
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   138
            if "MP3" in self.attrs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   139
                return 1
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   140
            if "website" in self.attrs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   141
                return -1
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   142
            raise Exception("Unknow audio type: %r" % (self.attrs))
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   143
        if self.dltype in ("mac","windows"):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   144
            return -1
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   145
        if self.dltype == "linux":
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   146
            score = 1
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   147
            if "x64" in self.attrs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   148
                score += 1
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   149
            if "deb" in self.attrs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   150
                score += 1
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   151
            return score
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   152
        if self.dltype == "android":
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   153
            return 0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   154
        raise Exception("Unknown dls type: %r" % (self,))
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   155
    def format(self, prefix=""):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   156
        res = prefix + '<download id="' + self.id + '">\n'
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   157
        res += prefix + "  <web>" + self.web + "</web>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   158
        res += prefix + "  <torrent>" + self.torrent + "</torrent>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   159
        res += prefix + "  <size>" + self.size + "</size>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   160
        res += prefix + "  <md5>" + self.md5 + "</md5>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   161
        res += prefix + "</download>"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   162
        return res
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   163
    def __repr__(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   164
        return self.format()
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   165
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   166
class Downloads:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   167
    def __init__(self, dom):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   168
        self.id = dom.name[len("downloads "):].split(" ")[0]
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   169
        self.elements = []
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   170
        self.others = []
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   171
        self.addchilds(dom)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   172
    def addchilds(self, dom):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   173
        for child in dom.childs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   174
            if child.name.startswith("downloads"):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   175
                self.addchilds(child)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   176
            elif child.name.startswith("download"):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   177
                self.elements.append(Download(self.id, child))
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   178
            elif child.name == "arc-toggle":
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   179
                self.addchilds(child)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   180
            elif child.name in ("clearfix","label"):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   181
                pass
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   182
            else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   183
                self.others.append(child)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   184
    def __iter__(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   185
        return iter(self.elements)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   186
    def format(self, prefix = ""):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   187
        res = prefix + '<downloads id="' + self.id + '">\n'
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   188
        if self.elements:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   189
            for el in self.elements:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   190
                res += el.format(prefix + "  ") + "\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   191
        if self.others:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   192
            res += prefix + "  <others>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   193
            for o in self.others:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   194
                res += o.format(prefix + "    ") + "\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   195
            res += prefix + "  </others>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   196
        res += prefix + "</downloads>"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   197
        return res
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   198
    def choose(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   199
        scores = list((dl.score(),dl) for dl in self if dl.score() >= 0)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   200
        scores.sort(key = lambda x: x[0], reverse = True)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   201
        for s, dl in scores:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   202
            return [dl]
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   203
        return []
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   204
    def __repr__(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   205
        return self.format()
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   206
        
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   207
class Game:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   208
    def __init__(self, dom):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   209
        self.title = "unknown"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   210
        self.downloads = []
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   211
        self.others = []
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   212
        for child in dom.childs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   213
            if child.name == "gameinfo":
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   214
                self.title = dom.gameinfo.title.a.data.data.strip()
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   215
            elif child.name.startswith("downloads "):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   216
                self.downloads.append(Downloads(child))
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   217
            elif child.name in ["icn", "clearfix"]:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   218
                pass
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   219
            else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   220
                self.others.append(child)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   221
    def __repr__(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   222
        res  = "<game>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   223
        res += "  <title>" + self.title + "</title>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   224
        if self.downloads:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   225
            res += "  <downloads>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   226
            for dl in self.downloads:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   227
                res += dl.format("    ") + "\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   228
            res += "  </downloads>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   229
        if self.others:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   230
            res += "  <others>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   231
            for o in self.others:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   232
                res += o.format("    ") + "\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   233
            res += "  </others>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   234
        res += "</game>"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   235
        return res
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   236
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   237
def parseGames(dom):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   238
    for row in findRows(dom):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   239
        yield Game(row)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   240
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   241
parser = BundleParser()
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   242
with open("tidy_bundle.html") as f:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   243
    for l in f:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   244
        parser.feed(l)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   245
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   246
for game in parseGames(parser.dom):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   247
    for dls in game.downloads:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   248
        choosen = dls.choose()
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   249
        for dl in dls:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   250
            print("%s | %-20s | %-10s | %-25s | %s " % (
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   251
                    "*" if dl in choosen else " ",
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   252
                    game.title, 
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   253
                    dls.id,
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   254
                    ", ".join(sorted(dl.attrs)),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   255
                    dl.torrent))