update-hib.py
author Fabien Ninoles <fabien@tzone.org>
Sun, 26 Aug 2012 22:27:54 -0400
changeset 3 478647351440
parent 2 3675dd7daf59
child 4 e102d2bb7a9e
permissions -rwxr-xr-x
Add README.txt
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
2
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
     2
#
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
     3
# Update HIB - Scrapper for the HumbleBundle library page.
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
     4
# Copyright (C) 2012, Fabien Ninoles <- fabien - AT - tzone . org ->
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
     5
#
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
     6
# This program is free software: you can redistribute it and/or modify
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
     7
# it under the terms of the GNU General Public License as published by
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
     8
# the Free Software Foundation, either version 3 of the License, or
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
     9
# (at your option) any later version.
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
    10
# 
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
    11
# This program is distributed in the hope that it will be useful,
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
    12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
    13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
    14
# GNU General Public License for more details.
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
    15
#
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
    16
# You should have received a copy of the GNU General Public License
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
    17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
    18
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    19
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    20
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
    21
from pprint import pprint
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    22
import xml.dom
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    23
from itertools import chain
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    24
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    25
class Node:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    26
    def __init__(self, **args):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    27
        self.childs = []
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    28
        self.attrs = {}
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    29
        for arg in args:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    30
            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
    31
        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
    32
            self.tag = self.name
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    33
            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
    34
            del self.attrs["class"]
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    35
        else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    36
            self.tag = self.name
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    37
    def format(self, prefix = ""):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    38
        res = prefix + "<" + self.name
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    39
        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
    40
            if self.attrs[attr]:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    41
                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
    42
            else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    43
                res += "\n" + prefix + "  " + attr
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    44
        if self.name == "data":
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    45
            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
    46
        elif self.childs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    47
            res += ">"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    48
            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
    49
                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
    50
            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
    51
        else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    52
            res += "/>"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    53
        return res
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    54
    def find(self, prefix):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    55
        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
    56
            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
    57
                yield child
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    58
    def __getattr__(self, name):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    59
        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
    60
            if child.name == name:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    61
                setattr(self, name, child)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    62
                return child
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    63
        raise AttributeError(name)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    64
    def __repr__(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    65
        return self.format()
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    66
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    67
class BundleParser(HTMLParser):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    68
    def __init__(self, **args):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    69
        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
    70
        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
    71
                        childs = [],
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    72
                        parent = None)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    73
        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
        self.depth = 1
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    75
    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
    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
        new = Node(name = tag,
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    78
                   attrs = dict(attrs),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    79
                   childs = [],
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    80
                   parent = self.current)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    81
        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
    82
        self.current = new
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    83
        self.depth += 1
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    84
    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
    85
        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
    86
            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
    87
            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
    88
            self.depth-=1
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    89
            # 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
    90
            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
    91
        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
    92
        self.depth-=1
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    93
        # print("-" * self.depth,tag)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    94
        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
    95
    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
    96
        if data.strip():
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    97
            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
    98
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    99
def findRows(dom):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   100
    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
   101
        try:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   102
            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
   103
                yield child
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   104
            else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   105
                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
   106
                    yield row
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   107
        except KeyError:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   108
            pass
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   109
            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
   110
                yield row
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
class Download:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   113
    subst = { "arc32"         : ("x86",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   114
              "arc64"         : ("x64",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   115
              "i386.deb"      : ("x86","deb"),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   116
              "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
   117
              "i686.rpm"      : ("x86", "rpm"),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   118
              ".i386.rpm"     : ("x86", "rpm"),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   119
              "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
   120
              ".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
   121
              "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
   122
              "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
   123
              ".tar.gz"       : ("tgz",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   124
              ".deb"          : ("deb",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   125
              ".rpm"          : ("rpm",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   126
              "32-bit"        : ("x86",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   127
              "64-bit"        : ("x64",),              
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   128
              }
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   129
    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
   130
        self.dltype = dltype
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   131
        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
   132
        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
   133
        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
   134
        self.id += " " + desc
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   135
        def cleanup(attr):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   136
            attr = attr.strip()
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   137
            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
   138
                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
   139
                    yield s
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   140
        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
   141
        urls = button.a.attrs
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   142
        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
   143
        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
   144
        details = dom.dldetails.dlsize
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   145
        if details.childs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   146
            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
   147
            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
   148
        else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   149
            self.size = "Unknown"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   150
            self.md5 = "Unknown"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   151
    def format(self, prefix=""):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   152
        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
   153
        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
   154
        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
   155
        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
   156
        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
   157
        res += prefix + "</download>"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   158
        return res
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   159
    def __repr__(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   160
        return self.format()
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   161
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   162
class Downloads:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   163
    def __init__(self, dom):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   164
        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
   165
        self.elements = []
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   166
        self.others = []
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   167
        self.addchilds(dom)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   168
    def addchilds(self, dom):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   169
        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
   170
            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
   171
                self.addchilds(child)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   172
            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
   173
                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
   174
            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
   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 in ("clearfix","label"):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   177
                pass
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   178
            else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   179
                self.others.append(child)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   180
    def __iter__(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   181
        return iter(self.elements)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   182
    def format(self, prefix = ""):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   183
        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
   184
        if self.elements:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   185
            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
   186
                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
   187
        if self.others:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   188
            res += prefix + "  <others>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   189
            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
   190
                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
   191
            res += prefix + "  </others>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   192
        res += prefix + "</downloads>"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   193
        return res
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   194
    def __repr__(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   195
        return self.format()
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   196
        
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   197
class Game:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   198
    def __init__(self, dom):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   199
        self.title = "unknown"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   200
        self.downloads = []
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   201
        self.others = []
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   202
        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
   203
            if child.name == "gameinfo":
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   204
                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
   205
            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
   206
                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
   207
            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
   208
                pass
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   209
            else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   210
                self.others.append(child)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   211
    def __repr__(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   212
        res  = "<game>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   213
        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
   214
        if self.downloads:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   215
            res += "  <downloads>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   216
            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
   217
                res += dl.format("    ") + "\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   218
            res += "  </downloads>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   219
        if self.others:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   220
            res += "  <others>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   221
            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
   222
                res += o.format("    ") + "\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   223
            res += "  </others>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   224
        res += "</game>"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   225
        return res
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   226
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   227
def parseGamesFromDom(dom):
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   228
    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
   229
        yield Game(row)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   230
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   231
def parseGamesFromFile(filename):
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   232
    parser = BundleParser()
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   233
    for l in open(filename):
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   234
        parser.feed(l)
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   235
    for game in parseGamesFromDom(parser.dom):
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   236
        yield game
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   237
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   238
class FileSelector:    
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   239
    def scoreDownload(self, dl):
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   240
        if dl.dltype == "audio":
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   241
            if "FLAC" in dl.attrs:
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   242
                return 2
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   243
            if "MP3" in dl.attrs:
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   244
                return 1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   245
            if "website" in dl.attrs:
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   246
                return -1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   247
            raise Exception("Unknow audio type: %r" % (dl.attrs))
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   248
        if dl.dltype in ("mac","windows"):
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   249
            return -1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   250
        if dl.dltype == "linux":
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   251
            score = 1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   252
            if "x64" in dl.attrs:
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   253
                score += 1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   254
            if "deb" in dl.attrs:
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   255
                score += 1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   256
            return score
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   257
        if dl.dltype == "android":
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   258
            return -1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   259
        raise Exception("Unknown dls type: %r" % (dl,))
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   260
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   261
    def chooseDownloads(self, dls):
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   262
        return sorted(((self.scoreDownload(dl),dl) for dl in dls), key=lambda x: x[0], reverse=True)
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   263
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   264
    def __call__(self, dls):
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   265
        return self.chooseDownloads(dls)
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   266
2
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   267
def main(fn):
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   268
    selector = FileSelector()
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   269
    downloads = []
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   270
    for game in parseGamesFromFile(fn):
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   271
        for dls in game.downloads:
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   272
            scores = selector(dls)
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   273
            choosen = list(dl for score, dl in scores if score >= 0)[:1]
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   274
            for score, dl in scores:
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   275
                print("[%s] %2d | %-20s | %-10s | %-25s | %s " % (
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   276
                        "*" if dl in choosen else " ",
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   277
                        score,
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   278
                        game.title, 
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   279
                        dls.id,
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   280
                        ", ".join(sorted(dl.attrs)),
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   281
                        dl.torrent))
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   282
                if dl in choosen:
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   283
                    downloads.append(dl)
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   284
            if not scores:
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   285
                print("No download for",dls.id)
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   286
            print("-" * 80)
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   287
2
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   288
    import urllib.request
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   289
    import urllib.parse
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   290
    import os
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   291
    opener = urllib.request.build_opener()
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   292
    for dl in downloads:
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   293
        fn = os.path.basename(urllib.parse.urlsplit(dl.torrent).path)
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   294
        print("Saving",dl.torrent,"as",fn)
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   295
        with opener.open(dl.torrent) as u:
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   296
            with open(fn,"wb") as f:
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   297
                f.write(u.read())
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   298
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   299
2
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   300
if __name__ == '__main__':
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   301
    import sys
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   302
    main(sys.argv[1])