All About Me
 Old Crap
 What's Going On
   Important News
 Pictures
   Kitty Porn

 Things I can't live without

> Cars
> Computers
> Food/Recipes
> Humor
> Music
> Privacy
Expand your vocabulary and feed a hungry person!

#!/usr/bin/env python
#
# cycle through download dir and show the torrents being grabbed
#
# additionally consult /share/incoming/.torrents.xml to print stats
#
from sys import *
from common import *
from xml.dom import minidom, Node
import string
import math
import time

## stolen from btlaunchmanycurses.py
def human_readable(n):
        n = long(n)
        unit = [' B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']
        i = 0
        if (n > 999):
                i = 1
                while i + 1 < len(unit) and (n >> 10) >= 999:
                        i += 1
                        n >>= 10
                n = float(n) / (1 << 10)
        if i > 0:
                size = '%.1f' % n + '%s' % unit[i]
        else:
                size = '%.0f' % n + '%s' % unit[i]
        return size

def findNodeName(parentNode, name):
    for childNode in parentNode.childNodes:
        if name == childNode.nodeName:
            content = []
            for textNode in childNode.childNodes:
                content.append( textNode.nodeValue )
            return string.join( content )
    return ''

verbose = False
tsize = 0

verbose = True

doc = minidom.parse( TORRENT_XML )

print "<html>"
print "<head>"
print "    <title>Downloads</title>"
print "</head>"
# do not cache this page!
print "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">"
print "<META HTTP-EQUIV=\"Expires\" CONTENT=\"-1\">"
print "<body>"
print ""

# last updated time
print "<b>Last Generated</b>: %s<br/><br/>" % time.ctime( time.time() )

print ""
print "<table border='1' cellspacing='0' cellpadding='2' width='100%'>"
print "<tr>"
print "    <th colspan='5' width='50%' align='center' bgcolor='#000000'><font color='#FFFFFF'>Name [Size]</font></th>"
print "    <th colspan='2' width='20%' align='center' bgcolor='#000000'><font color='#FFFFFF'>Up @ Rate</font></th>"
print "    <th colspan='2' width='20%' align='center' bgcolor='#000000'><font color='#FFFFFF'>Dn @ Rate</font></th>"
print " <th colspan='1' width='10%' align='center' bgcolor='#000000'><font color='#FFFFFF'>Ratio</font></th>"
print "</tr>"
print ""

totalBytesUp = 0
totalBytesDn = 0
totalRateUp    = 0
totalRateDn    = 0
totalIncoming = 0

for torrent in doc.documentElement.childNodes:

    if torrent.nodeName == 'torrent':
        torrentPath = findNodeName( torrent, 'fullpath' )

        name = findNodeName( torrent, 'name' )
        fsize = int( findNodeName( torrent, 'filesize' ) )
        totalIncoming += fsize
        hstBytesUp = int(findNodeName( torrent, 'totalUploadBytes'))
        hstBytesDn = int(findNodeName( torrent, 'totalDownloadBytes'))
        totalBytesUp += hstBytesUp
        totalBytesDn += hstBytesDn
        rateUp = float(findNodeName( torrent, 'uploadRate' ) )
        rateDn = float(findNodeName( torrent, 'downloadRate' ) )
        totalRateUp += rateUp
        totalRateDn += rateDn
        status = findNodeName( torrent, 'status' )
        eta = findNodeName( torrent, 'eta' )
        numPeers = int( findNodeName( torrent, 'peers' ) )
        numSeeds = int( findNodeName( torrent, 'seeds') )

        # 1:1 achieved?
        ratioOK = False
        if hstBytesUp > fsize and status == "seeding" and hstBytesUp > hstBytesDn:
            print "<tr bgcolor='green'>"
            ratioOK = True
        else:
            print "<tr>"

        print "<td colspan=\'5\' width=\'50%%\' align=\'left\'>%s [%s]" % (name, human_readable(fsize))

        if status != "seeding":
            
            if eta == "complete!":
                print "<br/><b>Status</b>: %s (%s)" % (status, findNodeName(torrent,'progress'))
            else:
                print "<br/><b>ETA</b>: %s" % eta
        print "</td>"

        print "<td colspan='2' width='20%%' align='center'>%s @ %s/s<br/>to %d peers</td>" % (human_readable(hstBytesUp), human_readable(rateUp), numPeers)
        print "<td colspan='2' width='20%%' align='center'>%s @ %s/s<br/>from %d seeds</td>" % (human_readable(hstBytesDn),human_readable(rateDn), numSeeds)

        if hstBytesDn > 0:
            print "<td colspan='1' width='10%%' align='center'>%.2f</td>" % (float(hstBytesUp) / float(hstBytesDn))
        else:
            print "<td colspan='1' width='10%%' align='center'> </td>"
        print "</tr>"
# totals
print "<tr>"
print " <td colspan='5' width='50%%' align='right'>= %s</td>" % human_readable(totalIncoming)
print " <td colspan='2' width='20%%' align='right'>= %s @ %s/s</td>" % (human_readable(totalBytesUp),human_readable(totalRateUp))
print " <td colspan='2' width='20%%' align='right'>= %s @ %s/s</td>" % (human_readable(totalBytesDn),human_readable(totalRateDn))
print " <td colspan='1' width='10%%' align='right'> </td>"
print "</tr>"

# /totals
print "</table>"
print "</body>"
print "</html>"


(© 1996-2008. Please don't link to images here; bandwidth costs money. Thanks.)