update-hib.py
author Fabien Ninoles <fabien@tzone.org>
Wed, 12 Dec 2012 22:48:14 -0500
changeset 7 9aadddc2ba90
parent 6 0c6d2ed2cd7c
child 8 98065a298da0
permissions -rwxr-xr-x
Fix some bad arguments.
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
4
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    23
from itertools import chain, groupby
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    24
import logging
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    25
import operator
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    26
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    27
class Node:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    28
    def __init__(self, **args):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    29
        self.childs = []
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    30
        self.attrs = {}
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    31
        for arg in args:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    32
            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
    33
        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
    34
            self.tag = self.name
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    35
            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
    36
            del self.attrs["class"]
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    37
        else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    38
            self.tag = self.name
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    39
    def format(self, prefix = ""):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    40
        res = prefix + "<" + self.name
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    41
        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
    42
            if self.attrs[attr]:
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 + '="' + self.attrs[attr] + '"'
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    44
            else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    45
                res += "\n" + prefix + "  " + attr
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    46
        if self.name == "data":
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    47
            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
    48
        elif self.childs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    49
            res += ">"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    50
            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
    51
                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
    52
            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
    53
        else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    54
            res += "/>"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    55
        return res
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    56
    def find(self, prefix):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    57
        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
    58
            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
    59
                yield child
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    60
    def __getattr__(self, name):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    61
        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
    62
            if child.name == name:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    63
                setattr(self, name, child)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    64
                return child
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    65
        raise AttributeError(name)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    66
    def __repr__(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    67
        return self.format()
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    68
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    69
class BundleParser(HTMLParser):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    70
    def __init__(self, **args):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    71
        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
    72
        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
    73
                        childs = [],
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    74
                        parent = None)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    75
        self.current = self.dom
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    76
        self.depth = 1
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    77
    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
    78
        # print("+" * self.depth,tag)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    79
        new = Node(name = tag,
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    80
                   attrs = dict(attrs),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    81
                   childs = [],
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    82
                   parent = self.current)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    83
        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
    84
        self.current = new
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    85
        self.depth += 1
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    86
    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
    87
        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
    88
            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
    89
            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
    90
            self.depth-=1
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    91
            # 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
    92
            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
    93
        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
    94
        self.depth-=1
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    95
        # print("-" * self.depth,tag)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    96
        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
    97
    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
    98
        if data.strip():
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    99
            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
   100
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   101
def findRows(dom):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   102
    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
   103
        try:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   104
            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
   105
                yield child
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   106
            else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   107
                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
   108
                    yield row
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   109
        except KeyError:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   110
            pass
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   111
            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
   112
                yield row
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   113
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   114
class Download:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   115
    subst = { "arc32"         : ("x86",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   116
              "arc64"         : ("x64",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   117
              "i386.deb"      : ("x86","deb"),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   118
              "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
   119
              "i686.rpm"      : ("x86", "rpm"),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   120
              ".i386.rpm"     : ("x86", "rpm"),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   121
              "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
   122
              ".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
   123
              "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
   124
              "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
   125
              ".tar.gz"       : ("tgz",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   126
              ".deb"          : ("deb",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   127
              ".rpm"          : ("rpm",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   128
              "32-bit"        : ("x86",),
5
b6a3b0987bfc Add ebook support.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
   129
              "64-bit"        : ("x64",),
b6a3b0987bfc Add ebook support.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
   130
              "(HD)"          : ("HD",),
6
0c6d2ed2cd7c Add download script production for http only links.
Fabien Ninoles <fabien@tzone.org>
parents: 5
diff changeset
   131
              "(MP3)"         : ("MP3",),
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   132
              }
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   133
    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
   134
        self.dltype = dltype
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   135
        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
   136
        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
   137
        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
   138
        self.id += " " + desc
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   139
        def cleanup(attr):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   140
            attr = attr.strip()
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   141
            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
   142
                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
   143
                    yield s
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   144
        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
   145
        urls = button.a.attrs
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   146
        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
   147
        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
   148
        details = dom.dldetails.dlsize
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   149
        if details.childs:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   150
            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
   151
            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
   152
        else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   153
            self.size = "Unknown"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   154
            self.md5 = "Unknown"
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=""):
5
b6a3b0987bfc Add ebook support.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
   156
        res = prefix + '<download type="' + self.dltype + '" id="' + self.id + '">\n'
0
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 __repr__(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   199
        return self.format()
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   200
        
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   201
class Game:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   202
    def __init__(self, dom):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   203
        self.title = "unknown"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   204
        self.downloads = []
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   205
        self.others = []
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   206
        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
   207
            if child.name == "gameinfo":
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   208
                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
   209
            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
   210
                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
   211
            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
   212
                pass
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   213
            else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   214
                self.others.append(child)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   215
    def __repr__(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   216
        res  = "<game>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   217
        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
   218
        if self.downloads:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   219
            res += "  <downloads>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   220
            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
   221
                res += dl.format("    ") + "\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   222
            res += "  </downloads>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   223
        if self.others:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   224
            res += "  <others>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   225
            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
   226
                res += o.format("    ") + "\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   227
            res += "  </others>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   228
        res += "</game>"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   229
        return res
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 parseGamesFromDom(dom):
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   232
    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
   233
        yield Game(row)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   234
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   235
def parseGamesFromFile(filename):
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   236
    parser = BundleParser()
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   237
    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
   238
        parser.feed(l)
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   239
    for game in parseGamesFromDom(parser.dom):
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   240
        yield game
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   241
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   242
class FileSelector:    
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   243
    def scoreDownload(self, dl):
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   244
        if dl.dltype == "audio":
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   245
            if "FLAC" in dl.attrs:
4
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   246
                return 1
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   247
            if "OGG" in dl.attrs:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   248
                return 1 
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   249
            if "MP3" in dl.attrs:
4
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   250
                return 1 
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   251
            if "website" in dl.attrs:
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   252
                return -1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   253
            raise Exception("Unknow audio type: %r" % (dl.attrs))
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   254
        if dl.dltype in ("mac","windows"):
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   255
            return -1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   256
        if dl.dltype == "linux":
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   257
            score = 1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   258
            if "x64" in dl.attrs:
4
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   259
                score += 2
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   260
            if "deb" in dl.attrs:
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   261
                score += 1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   262
            return score
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   263
        if dl.dltype == "android":
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   264
            return -1
5
b6a3b0987bfc Add ebook support.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
   265
        if dl.dltype == "ebook":
b6a3b0987bfc Add ebook support.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
   266
            if "MOBI" in dl.attrs:
b6a3b0987bfc Add ebook support.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
   267
                return -1
b6a3b0987bfc Add ebook support.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
   268
            if "HD" in dl.attrs:
b6a3b0987bfc Add ebook support.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
   269
                return 2
b6a3b0987bfc Add ebook support.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
   270
            return 1
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   271
        raise Exception("Unknown dls type: %r" % (dl,))
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   272
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   273
    def chooseDownloads(self, dls):
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   274
        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
   275
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   276
    def __call__(self, dls):
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   277
        return self.chooseDownloads(dls)
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   278
4
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   279
def selectHighestScore(scores):
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   280
    if scores:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   281
        get_first = operator.itemgetter(0)
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   282
        score, dls = next(groupby(sorted(scores, key = get_first, reverse=True), get_first))
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   283
        if score > 0:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   284
            return list(dl for s, dl in dls)
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   285
        else:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   286
            return []
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   287
    logging.debug("Empty scores list: %r", scores)
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   288
    return []
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   289
2
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   290
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
   291
    selector = FileSelector()
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   292
    downloads = []
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   293
    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
   294
        for dls in game.downloads:
4
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   295
            scores = list(selector(dls))
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   296
            choosen = selectHighestScore(scores)
2
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   297
            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
   298
                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
   299
                        "*" 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
   300
                        score,
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   301
                        game.title, 
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   302
                        dls.id,
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   303
                        ", ".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
   304
                        dl.torrent))
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   305
                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
   306
                    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
   307
            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
   308
                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
   309
            print("-" * 80)
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   310
2
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   311
    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
   312
    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
   313
    import os
6
0c6d2ed2cd7c Add download script production for http only links.
Fabien Ninoles <fabien@tzone.org>
parents: 5
diff changeset
   314
    urlfile = open('http-download.sh','w')
2
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   315
    opener = urllib.request.build_opener()
4
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   316
    for dl in (dl for dl in downloads):
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   317
        if dl.torrent:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   318
            try:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   319
                fn = os.path.basename(urllib.parse.urlsplit(dl.torrent).path)
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   320
                if os.path.exists(fn):
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   321
                    logging.info("Skipping existing torrent %s", fn)
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   322
                else:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   323
                    logging.info("Saving %s as %s", dl.torrent, fn)
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   324
                    with opener.open(dl.torrent) as u:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   325
                        with open(fn,"wb") as f:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   326
                            f.write(u.read())
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   327
            except:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   328
                logging.exception("Error with download %r", dl)
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   329
        else:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   330
            logging.info("No torrent, url is %s", dl.web)
6
0c6d2ed2cd7c Add download script production for http only links.
Fabien Ninoles <fabien@tzone.org>
parents: 5
diff changeset
   331
            fn = os.path.basename(urllib.parse.urlsplit(dl.web).path)
7
9aadddc2ba90 Fix some bad arguments.
Fabien Ninoles <fabien@tzone.org>
parents: 6
diff changeset
   332
            urlfile.write("wget --progress=bar -c -O ../packages/%s \"%s\"\n" % (fn,dl.web))
6
0c6d2ed2cd7c Add download script production for http only links.
Fabien Ninoles <fabien@tzone.org>
parents: 5
diff changeset
   333
            
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   334
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   335
2
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   336
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
   337
    import sys
4
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   338
    logging.getLogger().setLevel(logging.INFO)
2
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   339
    main(sys.argv[1])