update-hib.py
changeset 13 7567c5e4db45
parent 12 9d5880ecdb82
child 14 f7112a0f9df7
equal deleted inserted replaced
12:9d5880ecdb82 13:7567c5e4db45
   228     def write(self, s):
   228     def write(self, s):
   229         self.main.write(s)
   229         self.main.write(s)
   230         for o in self.other:
   230         for o in self.other:
   231             o.write(s)
   231             o.write(s)
   232 
   232 
   233 def main(fn):
   233 def main(fn, cachedir):
   234     selector = FileSelector()
   234     selector = FileSelector()
   235     downloads = []
   235     downloads = []
   236     import sys
   236     import sys
   237     import os
   237     import os
   238     import urllib.parse
   238     import urllib.parse
   260                 print("-" * 80, file=l)
   260                 print("-" * 80, file=l)
   261 
   261 
   262     import urllib.request
   262     import urllib.request
   263     urlfile = open('http-download.sh','w')
   263     urlfile = open('http-download.sh','w')
   264     opener = urllib.request.build_opener()
   264     opener = urllib.request.build_opener()
       
   265     cache = set(os.listdir(cachedir))
   265     for dl in (dl for dl in downloads):
   266     for dl in (dl for dl in downloads):
   266         if dl.torrent:
   267         if dl.torrent:
   267             try:
   268             try:
   268                 fn = os.path.basename(urllib.parse.urlsplit(dl.torrent).path)
   269                 fn = os.path.basename(urllib.parse.urlsplit(dl.torrent).path)
   269                 if os.path.exists(fn):
   270                 if os.path.exists(fn):
   270                     logging.info("Skipping existing torrent %s", fn)
   271                     logging.info("Skipping existing torrent %s", fn)
       
   272                 elif fn in cache:
       
   273                     logging.info("Copying %s as %s from cache", dl.torrent, fn)
       
   274                     os.link(os.path.join(cachedir, fn), fn)
   271                 else:
   275                 else:
   272                     logging.info("Saving %s as %s", dl.torrent, fn)
   276                     logging.info("Saving %s as %s", dl.torrent, fn)
   273                     with opener.open(dl.torrent) as u:
   277                     with opener.open(dl.torrent) as u:
   274                         with open(fn,"wb") as f:
   278                         with open(fn,"wb") as f:
   275                             f.write(u.read())
   279                             f.write(u.read())
   284 
   288 
   285 
   289 
   286 if __name__ == '__main__':
   290 if __name__ == '__main__':
   287     import sys
   291     import sys
   288     logging.getLogger().setLevel(logging.INFO)
   292     logging.getLogger().setLevel(logging.INFO)
   289     main(sys.argv[1])
   293     main(sys.argv[1], sys.argv[2])