diff -r 1e76c59aa3a6 -r fb1ab147b2dd update-hib.py --- a/update-hib.py Sun Aug 26 12:23:05 2012 -0400 +++ b/update-hib.py Sun Aug 26 22:03:23 2012 -0400 @@ -131,27 +131,6 @@ else: self.size = "Unknown" self.md5 = "Unknown" - def score(self): - if self.dltype == "audio": - if "FLAC" in self.attrs: - return 2 - if "MP3" in self.attrs: - return 1 - if "website" in self.attrs: - return -1 - raise Exception("Unknow audio type: %r" % (self.attrs)) - if self.dltype in ("mac","windows"): - return -1 - if self.dltype == "linux": - score = 1 - if "x64" in self.attrs: - score += 1 - if "deb" in self.attrs: - score += 1 - return score - if self.dltype == "android": - return 0 - raise Exception("Unknown dls type: %r" % (self,)) def format(self, prefix=""): res = prefix + '\n' res += prefix + " " + self.web + "\n" @@ -195,12 +174,6 @@ res += prefix + " \n" res += prefix + "" return res - def choose(self): - scores = list((dl.score(),dl) for dl in self if dl.score() >= 0) - scores.sort(key = lambda x: x[0], reverse = True) - for s, dl in scores: - return [dl] - return [] def __repr__(self): return self.format() @@ -234,22 +207,75 @@ res += "" return res -def parseGames(dom): +def parseGamesFromDom(dom): for row in findRows(dom): yield Game(row) -parser = BundleParser() -with open("tidy_bundle.html") as f: - for l in f: +def parseGamesFromFile(filename): + parser = BundleParser() + for l in open(filename): parser.feed(l) + for game in parseGamesFromDom(parser.dom): + yield game -for game in parseGames(parser.dom): +class FileSelector: + def scoreDownload(self, dl): + if dl.dltype == "audio": + if "FLAC" in dl.attrs: + return 2 + if "MP3" in dl.attrs: + return 1 + if "website" in dl.attrs: + return -1 + raise Exception("Unknow audio type: %r" % (dl.attrs)) + if dl.dltype in ("mac","windows"): + return -1 + if dl.dltype == "linux": + score = 1 + if "x64" in dl.attrs: + score += 1 + if "deb" in dl.attrs: + score += 1 + return score + if dl.dltype == "android": + return -1 + raise Exception("Unknown dls type: %r" % (dl,)) + + def chooseDownloads(self, dls): + return sorted(((self.scoreDownload(dl),dl) for dl in dls), key=lambda x: x[0], reverse=True) + + def __call__(self, dls): + return self.chooseDownloads(dls) + +selector = FileSelector() + +downloads = [] +for game in parseGamesFromFile("tidy_bundle.html"): for dls in game.downloads: - choosen = dls.choose() - for dl in dls: - print("%s | %-20s | %-10s | %-25s | %s " % ( + scores = selector(dls) + choosen = list(dl for score, dl in scores if score >= 0)[:1] + for score, dl in scores: + print("[%s] %2d | %-20s | %-10s | %-25s | %s " % ( "*" if dl in choosen else " ", + score, game.title, dls.id, ", ".join(sorted(dl.attrs)), dl.torrent)) + if dl in choosen: + downloads.append(dl) + if not scores: + print("No download for",dls.id) + print("-" * 80) + +import urllib.request +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()) +