This forum has been archived. All content is frozen. Please use KDE Discuss instead.

kross torrent sorter script

Tags: None
(comma "," separated)
Rioting_Pacifist
Registered Member
Posts
27
Karma
0

kross torrent sorter script

Fri Aug 14, 2009 5:01 pm
Thanks largely to George for helping me understand kross, ive managed to put together a basic script to auto sort torrents. unfortunately it has a huge memory leak, so do not use it:

Code: Select all
#!/usr/biclan/env kross

# License:
# 1) You have the right to redistribute (un)modified versions of this script aslong as the sourcecode required to run it is always avalible to users
# 2) You have the right to relicense this script aslong as the new license enforces (1)

# This script was written before i fully understood the KTorrent API so do not assume ih(inforhash) and tor (torrent) are always correctly names
import KTorrent
import Kross
import KTScriptingPlugin
from os.path import expanduser, basename
gName = "sorter-mediatype-r: "
gVer  = "0.1dev"

class JuanGroup:
   def __init__(self,name):
      self.name = str(name)
      self.subgroups = []
      self.members = []
   
   def isMember(self,tor):
      if tor.infoHash() in self.members:
         return True
      else:
         for grp in self.subgroups:
            if grp.isMember(tor): return True
      return False
   
   def addSubGroup(self,grp):
      self.subgroups.append(grp)
   
   def add(self,ih):
      self.members.append(ih)
      
   def remove(self,ih):
      self.members.remove(ih)

def searchStrings(name,sstring):
   for s in gSearchStrings[sstring]:
      if s in name: return True
   return False

def isVid(f):
   return searchStrings(basename(f),'vid')

def torrentAdded(ih):
   tor = KTorrent.torrent(ih)
   if searchStrings(tor.name(),'Film'): gMediaGroup["Film"].add(ih)
   elif searchStrings(tor.name(),'Discography'): gMediaGroup["Discography"].add(ih)
   elif searchStrings(tor.name(),'Series'): gMediaGroup["Series"].add(ih)
   elif tor.numFiles() == 0:
      if searchStrings(tor.name(),'hd') or searchStrings(tor.name(),'vid'): gMediaGroup["Video"].add(ih)
      elif searchStrings(tor.name(),'Audio'): gMediaGroup["Audio"].add(ih)
   else:
      vids = audio = 0
      for i in range(tor.numFiles()):
         if tor.isMultiMediaFile(i):
            if isVid(tor.filePathOnDisk(i)): vids +=1
            else: audio +=1
      
      if (vids > audio):
         if vids > 4: gMediaGroup["Series"].add(ih)
         else: gMediaGroup["Video"].add(ih)
      elif audio > 0:
         if audio < 4: gMediaGroup["Single"].add(ih)
         elif audio < 32:gMediaGroup["Album"].add(ih)
         else: gMediaGroup["Discography"].add(ih)

def fillSearchStrings(fname):
   sstrings={}
   f = open(expanduser(fname), 'r')
   for line in f:
      l = line.split(':',1)
      sstrings[l[0]] = l[1].split(',')
   f.close()
   return sstrings

def startup():
   KTorrent.log(gName+"starting ("+gVer+")")
   for tgrp,grps in ("Audio",["Single","Album","Discography"]),("Video",["Film","Series"]):
      gMediaGroup[tgrp]=JuanGroup(tgrp)
      KTScriptingPlugin.addGroup(tgrp ,"network-server","/all/MediaGroups/"+tgrp,gMediaGroup[tgrp])         #fix image
      for grp in grps:
         gMediaGroup[grp]=JuanGroup(grp)
         gMediaGroup[tgrp].addSubGroup(gMediaGroup[grp])
         KTScriptingPlugin.addGroup(grp,"network-server","/all/MediaGroups/"+tgrp+"/"+grp,gMediaGroup[grp])
   for ih in KTorrent.torrents():
      torrentAdded(ih)
   KTorrent.log(gName+"starting...done")

def unload():
   KTorrent.log(gName+"shutting down ("+gVer+")")
   for grp in gMediaGroup.values():
      KTScriptingPlugin.removeGroup(grp.name)
      del grp
   gSearchStrings.clear
   gMediaGroup.clear
   KTorrent.log(gName+"shutdown complete")

gMediaGroup={}                           #Global definitions, redo as not global?
gSearchStrings = fillSearchStrings('~/.kde/share/config/ktorrentsorter/strings') #load search strings once to save on disc access latter
startup()

~/.kde/share/config/ktorrentsorter/string is
Code: Select all
Film:axxo,kingdom release
Series:season,series,HDTV,SDTV,tvrip
Discography:discography,discografia
Audio:kbps,mp3,flac,cd,ogg
hd:720p,mkv,x264,bluray,1024p,1080,[HD,(HD
vid:.avi,.mkv,.wmv,.mp4


the memory starts haemorrhaging when you actually click on a group, to me this suggests that the problem is in the isMember function,
unfortunately i don't see what I'm doing wrong, is there a problem with the Kross interface?

I know i can work around this (redo subgroups to do the work on addition), but it doesn't seam like a nice way of doing it and I'm away from a sober state for the rest of the week

p.s is there a good community for python noobs to get feedback on scripts (e.g what im doing wrong) as most of my mistakes are probably not KTorrent/Kross specific?
George
Moderator
Posts
5421
Karma
1

Sat Aug 15, 2009 8:26 am
I see the memory leak to, a 100 KB every couple of seconds.

Looking at the code I don't see anything wrong, in isMember nothing is added to a list or a dictionary or something else. So I started the tracker grouping script, and that showed the same behavior.

It must be in KT itself or in Kross. But I don't see anything obvious in the scripting plugin which could leak memory.

Update: it is definitely where KT calls the script's isMember call (through kross) which is leaking memory. It's got to be in kross.
George
Moderator
Posts
5421
Karma
1

Sat Aug 15, 2009 8:55 am
I have made a bug report :

http://bugs.kde.org/show_bug.cgi?id=203922
Rioting_Pacifist
Registered Member
Posts
27
Karma
0

Re: kross torrent sorter script

Thu Aug 20, 2009 2:34 pm
thanks, I'm still working on the script, unfortunately as i don't know c (yet) i'll have to just keep an eye on the report for now


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot]