Last active
August 29, 2015 13:58
-
-
Save winny-/10394308 to your computer and use it in GitHub Desktop.
Bump transmission file priorities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import transmissionrpc | |
import guessit | |
class TorrentBumper(object): | |
def __init__(self, host='localhost', port=9091): | |
self.client = transmissionrpc.Client(host, port=port) | |
@property | |
def torrents(self): | |
return [TorrentWrapper(t) for t in self.client.get_torrents()] | |
def bump_torrent(self, torrent, expedite=2): | |
files = torrent.files() | |
completed = [] | |
for fileid, file_details in dict(files).iteritems(): | |
do_not_prioritze = (torrent.file_is_completed(fileid) or | |
not torrent.file_is_wanted(fileid)) | |
if do_not_prioritze: | |
files.pop(fileid) | |
completed.append(fileid) | |
continue | |
try: | |
guess = guessit.guess_episode_info(file_details['name']) | |
file_details['episode'] = guess['episodeNumber'] | |
except KeyError: | |
# Bump unknown episode numbers to the end of the queue. | |
file_details['episode'] = 999999999 | |
ids_by_episode = sorted(files.keys(), | |
key=lambda fileid: files[fileid]['episode']) | |
highest_speed = ids_by_episode[:expedite] | |
rest = ids_by_episode[expedite:] | |
threshhold = len(rest) / 2 | |
normal_speed = rest[:threshhold] | |
low_speed = rest[threshhold:] | |
print('TID: {} ;; high {} | norm {} | low {} | completed {}' | |
.format(torrent.id, | |
len(highest_speed), | |
len(normal_speed), | |
len(low_speed), | |
len(completed))) | |
self.client.change_torrent(torrent.id, | |
priority_low=low_speed, | |
priority_normal=normal_speed+completed, | |
priority_high=highest_speed) | |
def bump_all_active(self): | |
[self.bump_torrent(torrent) for torrent in self.torrents | |
if torrent.status == 'downloading' or | |
torrent.status == 'download pending'] | |
class TorrentWrapper(object): | |
def __init__(self, torrent): | |
self.torrent = torrent | |
def file_is_wanted(self, fileid): | |
return bool(self.torrent.wanted[fileid]) | |
def file_is_completed(self, fileid): | |
file_ = self.torrent.files()[fileid] | |
return not file_['size'] - file_['completed'] | |
def __getattr__(self, attr): | |
if attr in self.__dict__: | |
return getattr(self, attr) | |
return getattr(self.torrent, attr) | |
def main(): | |
host, port = 'localhost', 9091 | |
try: | |
bumper = TorrentBumper(host, port) | |
except transmissionrpc.error.TransmissionError: | |
print('Could not connect to Transmission on {}:{}'.format(host, port)) | |
return | |
bumper.bump_all_active() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment