home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / bin / btdownloadheadless.bittorrent < prev    next >
Encoding:
Text File  |  2007-02-19  |  4.7 KB  |  166 lines

  1. #! /usr/bin/python
  2.  
  3. # Written by Bram Cohen
  4. # see LICENSE.txt for license information
  5.  
  6. from BitTorrent.download import download
  7. from threading import Event
  8. from os.path import abspath
  9. from sys import argv, stdout
  10. from cStringIO import StringIO
  11. from time import time
  12.  
  13. def hours(n):
  14.     if n == -1:
  15.         return '<unknown>'
  16.     if n == 0:
  17.         return 'complete!'
  18.     n = long(n)
  19.     h, r = divmod(n, 60 * 60)
  20.     m, sec = divmod(r, 60)
  21.     if h > 1000000:
  22.         return '<unknown>'
  23.     if h > 0:
  24.         return '%d hour %02d min %02d sec' % (h, m, sec)
  25.     else:
  26.         return '%d min %02d sec' % (m, sec)
  27.  
  28. class HeadlessDisplayer:
  29.     def __init__(self):
  30.         self.done = False
  31.         self.file = ''
  32.         self.percentDone = ''
  33.         self.timeEst = ''
  34.         self.downloadTo = ''
  35.         self.downRate = ''
  36.         self.upRate = ''
  37.         self.downTotal = ''
  38.         self.upTotal = ''
  39.         self.errors = []
  40.         self.last_update_time = 0
  41.  
  42.     def finished(self):
  43.         self.done = True
  44.         self.percentDone = '100'
  45.         self.timeEst = 'Download Succeeded!'
  46.         self.downRate = ''
  47.         self.display({})
  48.  
  49.     def failed(self):
  50.         self.done = True
  51.         self.percentDone = '0'
  52.         self.timeEst = 'Download Failed!'
  53.         self.downRate = ''
  54.         self.display({})
  55.  
  56.     def error(self, errormsg):
  57.         self.errors.append(errormsg)
  58.         if len(self.errors) > 5: 
  59.             self.errors = self.errors[-5:]
  60.         self.display({})
  61.  
  62.     def display(self, dict):
  63.         if self.last_update_time + 0.1 > time() and dict.get('fractionDone') not in (0.0, 1.0) and not dict.has_key('activity'):
  64.             return
  65.         self.last_update_time = time()
  66.         if dict.has_key('spew'):
  67.             print_spew(dict['spew'])
  68.         if dict.has_key('fractionDone'):
  69.             self.percentDone = str(float(int(dict['fractionDone'] * 1000)) / 10)
  70.         if dict.has_key('timeEst'):
  71.             self.timeEst = hours(dict['timeEst'])
  72.         if dict.has_key('activity') and not self.done:
  73.             self.timeEst = dict['activity']
  74.         if dict.has_key('downRate'):
  75.             self.downRate = '%.2f K/s' % (float(dict['downRate']) / (1 << 10))
  76.         if dict.has_key('upRate'):
  77.             self.upRate = '%.2f K/s' % (float(dict['upRate']) / (1 << 10))
  78.         if dict.has_key('upTotal'):
  79.             self.upTotal = '%.1f M' % (dict['upTotal'])
  80.         if dict.has_key('downTotal'):
  81.             self.downTotal = '%.1f M' % (dict['downTotal'])
  82.         print '\n\n'
  83.         for err in self.errors:
  84.             print 'ERROR: ' + err + '\n'
  85.         print 'saving:        ', self.file
  86.         print 'percent done:  ', self.percentDone
  87.         print 'time left:     ', self.timeEst
  88.         print 'download to:   ', self.downloadTo
  89.         if self.downRate != '':
  90.             print 'download rate: ', self.downRate
  91.         if self.upRate != '':
  92.             print 'upload rate:   ', self.upRate
  93.         if self.downTotal != '':
  94.             print 'download total:', self.downTotal
  95.         if self.upTotal != '':
  96.             print 'upload total:  ', self.upTotal
  97.         stdout.flush()
  98.  
  99.     def chooseFile(self, default, size, saveas, dir):
  100.         self.file = '%s (%.1f M)' % (default, float(size) / (1 << 20))
  101.         if saveas != '':
  102.             default = saveas
  103.         self.downloadTo = abspath(default)
  104.         return default
  105.  
  106.     def newpath(self, path):
  107.         self.downloadTo = path
  108.  
  109. def print_spew(spew):
  110.     s = StringIO()
  111.     s.write('\n\n\n')
  112.     for c in spew:
  113.         s.write('%20s ' % c['ip'])
  114.         if c['initiation'] == 'local':
  115.             s.write('l')
  116.         else:
  117.             s.write('r')
  118.         rate, interested, choked = c['upload']
  119.         s.write(' %10s ' % str(int(rate)))
  120.         if c['is_optimistic_unchoke']:
  121.             s.write('*')
  122.         else:
  123.             s.write(' ')
  124.         if interested:
  125.             s.write('i')
  126.         else:
  127.             s.write(' ')
  128.         if choked:
  129.             s.write('c')
  130.         else:
  131.             s.write(' ')
  132.  
  133.         rate, interested, choked, snubbed = c['download']
  134.         s.write(' %10s ' % str(int(rate)))
  135.         if interested:
  136.             s.write('i')
  137.         else:
  138.             s.write(' ')
  139.         if choked:
  140.             s.write('c')
  141.         else:
  142.             s.write(' ')
  143.         if snubbed:
  144.             s.write('s')
  145.         else:
  146.             s.write(' ')
  147.         s.write('\n')
  148.     print s.getvalue()
  149.  
  150. def run(params):
  151.     try:
  152.         import curses
  153.         curses.initscr()
  154.         cols = curses.COLS
  155.         curses.endwin()
  156.     except:
  157.         cols = 80
  158.  
  159.     h = HeadlessDisplayer()
  160.     download(params, h.chooseFile, h.display, h.finished, h.error, Event(), cols, h.newpath)
  161.     if not h.done:
  162.         h.failed()
  163.  
  164. if __name__ == '__main__':
  165.     run(argv[1:])
  166.