update-hib.py
changeset 2 3675dd7daf59
parent 1 fb1ab147b2dd
child 4 e102d2bb7a9e
equal deleted inserted replaced
1:fb1ab147b2dd 2:3675dd7daf59
     1 #!/usr/bin/python3
     1 #!/usr/bin/python3
       
     2 #
       
     3 # Update HIB - Scrapper for the HumbleBundle library page.
       
     4 # Copyright (C) 2012, Fabien Ninoles <- fabien - AT - tzone . org ->
       
     5 #
       
     6 # This program is free software: you can redistribute it and/or modify
       
     7 # it under the terms of the GNU General Public License as published by
       
     8 # the Free Software Foundation, either version 3 of the License, or
       
     9 # (at your option) any later version.
       
    10 # 
       
    11 # This program is distributed in the hope that it will be useful,
       
    12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14 # GNU General Public License for more details.
       
    15 #
       
    16 # You should have received a copy of the GNU General Public License
       
    17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    18 
     2 
    19 
     3 from html.parser import HTMLParser
    20 from html.parser import HTMLParser
     4 from pprint import pprint
    21 from pprint import pprint
     5 import xml.dom
    22 import xml.dom
     6 from itertools import chain
    23 from itertools import chain
   245         return sorted(((self.scoreDownload(dl),dl) for dl in dls), key=lambda x: x[0], reverse=True)
   262         return sorted(((self.scoreDownload(dl),dl) for dl in dls), key=lambda x: x[0], reverse=True)
   246 
   263 
   247     def __call__(self, dls):
   264     def __call__(self, dls):
   248         return self.chooseDownloads(dls)
   265         return self.chooseDownloads(dls)
   249 
   266 
   250 selector = FileSelector()
   267 def main(fn):
   251 
   268     selector = FileSelector()
   252 downloads = []
   269     downloads = []
   253 for game in parseGamesFromFile("tidy_bundle.html"):
   270     for game in parseGamesFromFile(fn):
   254     for dls in game.downloads:
   271         for dls in game.downloads:
   255         scores = selector(dls)
   272             scores = selector(dls)
   256         choosen = list(dl for score, dl in scores if score >= 0)[:1]
   273             choosen = list(dl for score, dl in scores if score >= 0)[:1]
   257         for score, dl in scores:
   274             for score, dl in scores:
   258             print("[%s] %2d | %-20s | %-10s | %-25s | %s " % (
   275                 print("[%s] %2d | %-20s | %-10s | %-25s | %s " % (
   259                     "*" if dl in choosen else " ",
   276                         "*" if dl in choosen else " ",
   260                     score,
   277                         score,
   261                     game.title, 
   278                         game.title, 
   262                     dls.id,
   279                         dls.id,
   263                     ", ".join(sorted(dl.attrs)),
   280                         ", ".join(sorted(dl.attrs)),
   264                     dl.torrent))
   281                         dl.torrent))
   265             if dl in choosen:
   282                 if dl in choosen:
   266                 downloads.append(dl)
   283                     downloads.append(dl)
   267         if not scores:
   284             if not scores:
   268             print("No download for",dls.id)
   285                 print("No download for",dls.id)
   269         print("-" * 80)
   286             print("-" * 80)
   270 
   287 
   271 import urllib.request
   288     import urllib.request
   272 import urllib.parse
   289     import urllib.parse
   273 import os
   290     import os
   274 opener = urllib.request.build_opener()
   291     opener = urllib.request.build_opener()
   275 for dl in downloads:
   292     for dl in downloads:
   276     fn = os.path.basename(urllib.parse.urlsplit(dl.torrent).path)
   293         fn = os.path.basename(urllib.parse.urlsplit(dl.torrent).path)
   277     print("Saving",dl.torrent,"as",fn)
   294         print("Saving",dl.torrent,"as",fn)
   278     with opener.open(dl.torrent) as u:
   295         with opener.open(dl.torrent) as u:
   279         with open(fn,"wb") as f:
   296             with open(fn,"wb") as f:
   280             f.write(u.read())
   297                 f.write(u.read())
   281 
   298 
       
   299 
       
   300 if __name__ == '__main__':
       
   301     import sys
       
   302     main(sys.argv[1])