update-hib.py
author Fabien Ninoles <fabien@tzone.org>
Wed, 22 Jan 2014 19:37:59 -0500
changeset 10 d7e256c9aec9
parent 9 e3a2bb2bae8d
child 11 dc1b075c538a
permissions -rwxr-xr-x
Add torrents.log. Add support for new Stream section. Add more logging.
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.
8
98065a298da0 Update to handle audio type as "standard" (always download).
Fabien Ninoles <fabien@tzone.org>
parents: 7
diff changeset
    10
#
2
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
9
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    19
import bs4
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    20
from pprint import pprint
4
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    21
from itertools import chain, groupby
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    22
import logging
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
    23
import operator
0
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 Download:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    26
    subst = { "arc32"         : ("x86",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    27
              "arc64"         : ("x64",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    28
              "i386.deb"      : ("x86","deb"),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    29
              "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
    30
              "i686.rpm"      : ("x86", "rpm"),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    31
              ".i386.rpm"     : ("x86", "rpm"),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    32
              "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
    33
              ".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
    34
              "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
    35
              "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
    36
              ".tar.gz"       : ("tgz",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    37
              ".deb"          : ("deb",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    38
              ".rpm"          : ("rpm",),
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    39
              "32-bit"        : ("x86",),
5
b6a3b0987bfc Add ebook support.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
    40
              "64-bit"        : ("x64",),
b6a3b0987bfc Add ebook support.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
    41
              "(HD)"          : ("HD",),
6
0c6d2ed2cd7c Add download script production for http only links.
Fabien Ninoles <fabien@tzone.org>
parents: 5
diff changeset
    42
              "(MP3)"         : ("MP3",),
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    43
              }
9
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    44
    def __init__(self, dltype, soup):
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    45
        self.dltype = dltype
9
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    46
        ids = [attr for attr in soup["class"] if attr != "download"]
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    47
        button = soup.find(class_="flexbtn")
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    48
        desc = button.span.string
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    49
        ids.extend(desc.split(" "))
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    50
        self.id = " ".join(ids)
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    51
        def cleanup(attr):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    52
            attr = attr.strip()
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    53
            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
    54
                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
    55
                    yield s
9
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    56
        self.attrs = set(chain.from_iterable(cleanup(attr) for attr in ids))
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    57
        urls = button.a.attrs
10
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
    58
        logging.debug("URLS are %r", urls)
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    59
        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
    60
        self.web = urls["data-web"]
9
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    61
        details = soup.find(class_="dldetails").find(class_="dlsize")
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    62
        size = details.find(class_="mbs")
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    63
        md5 = details.find(class_="dlmd5")
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    64
        date = details.find(class_="dldate")
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    65
        self.size = size.string if size else "Unknown"
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    66
        self.md5 = md5.string if md5 else "Unknown"
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    67
        self.date = date.string if date else "Unknown"
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    68
    def format(self, prefix=""):
5
b6a3b0987bfc Add ebook support.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
    69
        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
    70
        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
    71
        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
    72
        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
    73
        res += prefix + "  <md5>" + self.md5 + "</md5>\n"
9
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    74
        res += prefix + "  <date>" + self.date + "</date>\n"
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    75
        res += prefix + "</download>"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    76
        return res
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    77
    def __repr__(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    78
        return self.format()
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    79
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    80
class Downloads:
9
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    81
    def __init__(self, soup):
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    82
        self.id = [class_ for class_ in soup["class"] if class_ != "downloads"][0]
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    83
        self.elements = []
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    84
        self.others = []
9
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    85
        self.addchilds(soup)
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    86
    def addchilds(self, soup):
10
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
    87
        logging.debug("Parsing soup for downloads %s", self.id)
9
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    88
        for child in soup.children:
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    89
            if type(child) is not bs4.element.Tag:
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    90
                continue
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    91
            classes = child["class"] if "class" in child.attrs else []
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    92
            if [True for attr in classes if attr in ("arc-toggle", "downloads")]:
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
    93
                self.addchilds(child)
9
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
    94
            elif "download" in classes:
10
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
    95
                desc = child.find(class_="flexbtn").span.string
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
    96
                if desc == "Stream":
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
    97
                    logging.info("Ignoring Stream URLs for %s", self.id)
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
    98
                else:
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
    99
                    self.elements.append(Download(self.id, child))
9
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
   100
            elif [True for attr in classes if attr in ("clearfix","label")]:
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   101
                pass
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   102
            else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   103
                self.others.append(child)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   104
    def __iter__(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   105
        return iter(self.elements)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   106
    def format(self, prefix = ""):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   107
        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
   108
        if self.elements:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   109
            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
   110
                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
   111
        if self.others:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   112
            res += prefix + "  <others>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   113
            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
   114
                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
   115
            res += prefix + "  </others>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   116
        res += prefix + "</downloads>"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   117
        return res
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   118
    def __repr__(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   119
        return self.format()
8
98065a298da0 Update to handle audio type as "standard" (always download).
Fabien Ninoles <fabien@tzone.org>
parents: 7
diff changeset
   120
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   121
class Game:
9
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
   122
    def __init__(self, soup):
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   123
        self.title = "unknown"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   124
        self.downloads = []
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   125
        self.others = []
10
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   126
        for child in soup.children:
9
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
   127
            if type(child) is not bs4.element.Tag:
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
   128
                continue
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
   129
            classes = child["class"] if "class" in child.attrs else []
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
   130
            if "gameinfo" in classes:
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
   131
                self.title = child.find(class_="title").a.string.strip()
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
   132
            elif "downloads" in classes:
10
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   133
                logging.debug("Collecting downloadables for %s", self.title)
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   134
                self.downloads.append(Downloads(child))
9
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
   135
            elif [True for attr in classes if attr in ["icn", "clearfix"]]:
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   136
                pass
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   137
            else:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   138
                self.others.append(child)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   139
    def __repr__(self):
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   140
        res  = "<game>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   141
        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
   142
        if self.downloads:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   143
            res += "  <downloads>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   144
            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
   145
                res += dl.format("    ") + "\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   146
            res += "  </downloads>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   147
        if self.others:
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   148
            res += "  <others>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   149
            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
   150
                res += o.format("    ") + "\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   151
            res += "  </others>\n"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   152
        res += "</game>"
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   153
        return res
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   154
9
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
   155
def parseGamesFromSoup(soup):
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
   156
    for row in soup.find_all(class_="row"):
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   157
        yield Game(row)
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   158
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   159
def parseGamesFromFile(filename):
9
e3a2bb2bae8d Add bs4 support, removing the need for tidy up the page first.
Fabien Ninoles <fabien@tzone.org>
parents: 8
diff changeset
   160
    for game in parseGamesFromSoup(bs4.BeautifulSoup(open(filename))):
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   161
        yield game
0
1e76c59aa3a6 Initial version: parse tidy file and select a suitable download url.
Fabien Ninoles <fabien@tzone.org>
parents:
diff changeset
   162
8
98065a298da0 Update to handle audio type as "standard" (always download).
Fabien Ninoles <fabien@tzone.org>
parents: 7
diff changeset
   163
class FileSelector:
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   164
    def scoreDownload(self, dl):
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   165
        if dl.dltype == "audio":
8
98065a298da0 Update to handle audio type as "standard" (always download).
Fabien Ninoles <fabien@tzone.org>
parents: 7
diff changeset
   166
            if not dl.attrs: # Empty set, so we simply take it.
98065a298da0 Update to handle audio type as "standard" (always download).
Fabien Ninoles <fabien@tzone.org>
parents: 7
diff changeset
   167
                return 1
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   168
            if "FLAC" in dl.attrs:
4
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   169
                return 1
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   170
            if "OGG" in dl.attrs:
8
98065a298da0 Update to handle audio type as "standard" (always download).
Fabien Ninoles <fabien@tzone.org>
parents: 7
diff changeset
   171
                return 1
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   172
            if "MP3" in dl.attrs:
8
98065a298da0 Update to handle audio type as "standard" (always download).
Fabien Ninoles <fabien@tzone.org>
parents: 7
diff changeset
   173
                return 1
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   174
            if "website" in dl.attrs:
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   175
                return -1
10
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   176
            if "AAC" in dl.attrs:
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   177
                return 1
8
98065a298da0 Update to handle audio type as "standard" (always download).
Fabien Ninoles <fabien@tzone.org>
parents: 7
diff changeset
   178
            raise Exception("Unknown audio type: %r" % (dl.attrs))
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   179
        if dl.dltype in ("mac","windows"):
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   180
            return -1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   181
        if dl.dltype == "linux":
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   182
            score = 1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   183
            if "x64" in dl.attrs:
4
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   184
                score += 2
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   185
            if "deb" in dl.attrs:
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   186
                score += 1
8
98065a298da0 Update to handle audio type as "standard" (always download).
Fabien Ninoles <fabien@tzone.org>
parents: 7
diff changeset
   187
            if "Stream" in dl.attrs:
98065a298da0 Update to handle audio type as "standard" (always download).
Fabien Ninoles <fabien@tzone.org>
parents: 7
diff changeset
   188
                score -= 1
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   189
            return score
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   190
        if dl.dltype == "android":
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   191
            return -1
5
b6a3b0987bfc Add ebook support.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
   192
        if dl.dltype == "ebook":
b6a3b0987bfc Add ebook support.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
   193
            if "MOBI" in dl.attrs:
b6a3b0987bfc Add ebook support.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
   194
                return -1
b6a3b0987bfc Add ebook support.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
   195
            if "HD" in dl.attrs:
b6a3b0987bfc Add ebook support.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
   196
                return 2
b6a3b0987bfc Add ebook support.
Fabien Ninoles <fabien@tzone.org>
parents: 4
diff changeset
   197
            return 1
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   198
        raise Exception("Unknown dls type: %r" % (dl,))
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   199
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   200
    def chooseDownloads(self, dls):
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   201
        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
   202
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   203
    def __call__(self, dls):
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   204
        return self.chooseDownloads(dls)
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   205
4
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   206
def selectHighestScore(scores):
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   207
    if scores:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   208
        get_first = operator.itemgetter(0)
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   209
        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
   210
        if score > 0:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   211
            return list(dl for s, dl in dls)
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   212
        else:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   213
            return []
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   214
    logging.debug("Empty scores list: %r", scores)
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   215
    return []
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   216
10
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   217
class tee:
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   218
    def __init__(self, main, *other):
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   219
        self.main = main
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   220
        self.other = other
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   221
    def write(self, s):
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   222
        self.main.write(s)
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   223
        for o in self.other:
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   224
            o.write(s)
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   225
2
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   226
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
   227
    selector = FileSelector()
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   228
    downloads = []
10
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   229
    import sys
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   230
    with open("torrents.log", "w") as l:
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   231
        for game in parseGamesFromFile(fn):
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   232
            logging.info("Parsing game %s (%d downloads)", game.title, len(game.downloads))
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   233
            for dls in game.downloads:
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   234
                scores = list(selector(dls))
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   235
                choosen = selectHighestScore(scores)
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   236
                for score, dl in scores:
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   237
                    print("[%s] %2d | %-30s | %-15s | %-30s | %-15s | %s " % (
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   238
                            "*" if dl in choosen else " ",
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   239
                            score,
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   240
                            game.title,
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   241
                            dls.id,
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   242
                            dl.date,
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   243
                            ", ".join(sorted(dl.attrs)),
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   244
                            dl.torrent),
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   245
                          file=l)
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   246
                    if dl in choosen:
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   247
                        downloads.append(dl)
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   248
                if not scores:
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   249
                    print("No download for %s" % (dls.id), file=l)
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   250
                print("-" * 80, file=l)
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   251
2
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   252
    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
   253
    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
   254
    import os
6
0c6d2ed2cd7c Add download script production for http only links.
Fabien Ninoles <fabien@tzone.org>
parents: 5
diff changeset
   255
    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
   256
    opener = urllib.request.build_opener()
4
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   257
    for dl in (dl for dl in downloads):
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   258
        if dl.torrent:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   259
            try:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   260
                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
   261
                if os.path.exists(fn):
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   262
                    logging.info("Skipping existing torrent %s", fn)
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   263
                else:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   264
                    logging.info("Saving %s as %s", dl.torrent, fn)
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   265
                    with opener.open(dl.torrent) as u:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   266
                        with open(fn,"wb") as f:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   267
                            f.write(u.read())
10
d7e256c9aec9 Add torrents.log.
Fabien Ninoles <fabien@tzone.org>
parents: 9
diff changeset
   268
                    logging.info("%s saved.", os.path.realpath(fn))
4
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   269
            except:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   270
                logging.exception("Error with download %r", dl)
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   271
        else:
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   272
            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
   273
            fn = os.path.basename(urllib.parse.urlsplit(dl.web).path)
8
98065a298da0 Update to handle audio type as "standard" (always download).
Fabien Ninoles <fabien@tzone.org>
parents: 7
diff changeset
   274
            urlfile.write("wget --progress=bar -c -O %s \"%s\"\n" % (fn,dl.web))
98065a298da0 Update to handle audio type as "standard" (always download).
Fabien Ninoles <fabien@tzone.org>
parents: 7
diff changeset
   275
1
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   276
fb1ab147b2dd Add downloading of torrent files.
Fabien Ninoles <fabien@tzone.org>
parents: 0
diff changeset
   277
2
3675dd7daf59 Take filename from command line arguments, add copyright and better readme text.
Fabien Ninoles <fabien@tzone.org>
parents: 1
diff changeset
   278
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
   279
    import sys
4
e102d2bb7a9e Update to download multiple version.
Fabien Ninoles <fabien@tzone.org>
parents: 2
diff changeset
   280
    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
   281
    main(sys.argv[1])