Update to download multiple version.
authorFabien Ninoles <fabien@tzone.org>
Sat, 22 Sep 2012 13:45:10 -0400
changeset 4 e102d2bb7a9e
parent 3 478647351440
child 5 b6a3b0987bfc
Update to download multiple version.
update-hib.py
--- a/update-hib.py	Sun Aug 26 22:27:54 2012 -0400
+++ b/update-hib.py	Sat Sep 22 13:45:10 2012 -0400
@@ -20,7 +20,9 @@
 from html.parser import HTMLParser
 from pprint import pprint
 import xml.dom
-from itertools import chain
+from itertools import chain, groupby
+import logging
+import operator
 
 class Node:
     def __init__(self, **args):
@@ -239,9 +241,11 @@
     def scoreDownload(self, dl):
         if dl.dltype == "audio":
             if "FLAC" in dl.attrs:
-                return 2
+                return 1
+            if "OGG" in dl.attrs:
+                return 1 
             if "MP3" in dl.attrs:
-                return 1
+                return 1 
             if "website" in dl.attrs:
                 return -1
             raise Exception("Unknow audio type: %r" % (dl.attrs))
@@ -250,7 +254,7 @@
         if dl.dltype == "linux":
             score = 1
             if "x64" in dl.attrs:
-                score += 1
+                score += 2
             if "deb" in dl.attrs:
                 score += 1
             return score
@@ -264,13 +268,24 @@
     def __call__(self, dls):
         return self.chooseDownloads(dls)
 
+def selectHighestScore(scores):
+    if scores:
+        get_first = operator.itemgetter(0)
+        score, dls = next(groupby(sorted(scores, key = get_first, reverse=True), get_first))
+        if score > 0:
+            return list(dl for s, dl in dls)
+        else:
+            return []
+    logging.debug("Empty scores list: %r", scores)
+    return []
+
 def main(fn):
     selector = FileSelector()
     downloads = []
     for game in parseGamesFromFile(fn):
         for dls in game.downloads:
-            scores = selector(dls)
-            choosen = list(dl for score, dl in scores if score >= 0)[:1]
+            scores = list(selector(dls))
+            choosen = selectHighestScore(scores)
             for score, dl in scores:
                 print("[%s] %2d | %-20s | %-10s | %-25s | %s " % (
                         "*" if dl in choosen else " ",
@@ -289,14 +304,24 @@
     import urllib.parse
     import os
     opener = urllib.request.build_opener()
-    for dl in downloads:
-        fn = os.path.basename(urllib.parse.urlsplit(dl.torrent).path)
-        print("Saving",dl.torrent,"as",fn)
-        with opener.open(dl.torrent) as u:
-            with open(fn,"wb") as f:
-                f.write(u.read())
+    for dl in (dl for dl in downloads):
+        if dl.torrent:
+            try:
+                fn = os.path.basename(urllib.parse.urlsplit(dl.torrent).path)
+                if os.path.exists(fn):
+                    logging.info("Skipping existing torrent %s", fn)
+                else:
+                    logging.info("Saving %s as %s", dl.torrent, fn)
+                    with opener.open(dl.torrent) as u:
+                        with open(fn,"wb") as f:
+                            f.write(u.read())
+            except:
+                logging.exception("Error with download %r", dl)
+        else:
+            logging.info("No torrent, url is %s", dl.web)
 
 
 if __name__ == '__main__':
     import sys
+    logging.getLogger().setLevel(logging.INFO)
     main(sys.argv[1])