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!

#
# common methods
#
import os
import time

from pythonutils.pathutils import Lock, LockError, LockFile
from sys import *
from os.path import *
from sha import *
from BitTornado.bencode import *
from shutil import *
import re
import datetime

## constants
PERCENT_KEEP_FREE = .30
INCOMING_TORRENT_DIR = '/test/monitored'
COMPLETED_TORRENT_DIR = '/test/monitored.done'

AUTOSTOPD_DIR=os.path.join( INCOMING_TORRENT_DIR, '.autostopd')
TORRENT_XML=os.path.join(INCOMING_TORRENT_DIR, '.torrents.xml')
MASTER_HASH_LIST = os.path.join( INCOMING_TORRENT_DIR,'.seen_hashes' )
ALLOWED_TRACKER_LIST = os.path.join( INCOMING_TORRENT_DIR, '.allowed_trackers')
USER_DL_DIR=os.path.join( '/home/torrentuser/torrents', str(os.getuid()) )
EXPIRED_TORRENT_DIR='/share/expired'
MAX_SLEEP_TIME = 20
FILE_DELIMITER = ':'
## /constants

# ======================================================================
def isTrackerAllowed(torrentTracker):
    rv = False

    f = open( ALLOWED_TRACKER_LIST, 'r' )
    for tracker in f.readlines():
        if tracker.startswith( '#'):
            continue

        tracker = tracker[:-1]
        if torrentTracker.rfind( tracker) > -1:
            rv = True
            break
    f.close();

    return rv

# ======================================================================
def isFileOwnerCurrentUser(fn):
    return os.stat(fn).st_uid == os.getuid()

# ======================================================================
def printSleepingStatus(timeToSleep):
    i=0

    while i != timeToSleep:
                    if i>1:
                                    stdout.write( "\b" )
                                if i>=10:
                                                stdout.write( "\b" )

                    stdout.write( "%d" % i )
                    stdout.flush()
                    i += 1
                    time.sleep( 1 );
    stdout.write( "\n" );
    stdout.flush();

# ======================================================================
# check for a hash in the master hashes file to see if a file has already
# been downloaded
def checkDownloadStatus(info):
    found = False

    try:    
        h = sha( bencode( info ) ).hexdigest()

        # open master hashes
        hashList = LockFile( MASTER_HASH_LIST, mode='r', timeout=5, step=0.1 )
        hashes = hashList.readlines()
        hashList.close()

        for hash in hashes:
            # does line == hash?
            splitLine = hash.split( FILE_DELIMITER )
            lineHash = splitLine[0]

            if lineHash == h:
                found = True
    except IOError, (errno, strerror):
        print "I/O error(%s): %s" % (errno, strerror)
        found = False
    except:
        print "EXception caught!: %s " % exc_info()[0]
        found = False

    return found

# ======================================================================
# record a hash in our master hashlist file
def recordDownloadedTorrent(info):
    try:    
        info_hash = sha( bencode( info    ) ).hexdigest()

        hashStr = "%s%s%s%s%d" % (info_hash, FILE_DELIMITER,
            datetime.datetime.now(), FILE_DELIMITER,
            os.getuid())

        ## lock file
        lock = Lock( MASTER_HASH_LIST, timeout=5, step=0.1 )
        lock.lock()
        hashList = open( MASTER_HASH_LIST, "a" )
        hashList.write( hashStr )
        hashList.write( '\n' )
        hashList.close()
        lock.unlock()
    except:
        raise

# ======================================================================
# replace crazy chars with _ to make scp life easier and scriptable
def escapeFilename(s):
                return re.sub("[^A-Za-z0-9\.]", "_", s)

# ======================================================================
## get full path for a file in a torrent
def fullFilePathFromTorrent(file_info):
    torrentFile = ''
    for item in file_info['path']:
        if torrentFile != '':
            torrentFile += "/"
        torrentFile += item
    return torrentFile

# ======================================================================
def nameFromTorrent(fn):
    info = infoFromTorrent(fn)

    if info == '':
        return None
    else:
        return info['name']

## get metainfo from a given torrent
def infoFromTorrent(fn):
    try:
        metainfo_file = open(fn, 'rb')
        metainfo = bdecode(metainfo_file.read())
        metainfo_file.close()
        info = metainfo['info']
        return info
    except:
        return ''

# ======================================================================
## find a file in a torrent
def findTorrent(s):
    try:
        f = open(s, 'rb')
        f.close()
        return s
    except:
        # cant open that file, assume its not there
        for root, dir, files in os.walk( INCOMING_TORRENT_DIR ):
            for file in files:
                if file.find('.torrent', 0 ) != -1:
                    fn = os.path.join(INCOMING_TORRENT_DIR, file)
                    info = infoFromTorrent(fn)
                    info_hash = sha( bencode( info ) ).hexdigest()

                    if info['name'] == s:
                        print "Located file %s in torrent %s" % (s, info_hash)
                        return fn
                    else:
                        if info.has_key('files'):
                            for torrentFileInfo in info['files']:
                                torrentFile = fullFilePathFromTorrent( torrentFileInfo )
                                if torrentFile == s:
                                    print "Located file %s in torrent %s" % (s, info_hash)
                                    return fn
    return ''


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