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

how to add a torrent to a group using kross

Tags: None
(comma "," separated)
Rioting_Pacifist
Registered Member
Posts
27
Karma
0
hwo do you add a torrent to a group, i've looked around but I don't know much so have missed any obvious documentation

Reading tracker_groups.py
Code: Select all
#!/usr/bin/env kross                                 
# -*- coding: utf-8 -*-                               
import KTorrent                                       
import KTScriptingPlugin                             
import Kross                                         

class TrackerGroup:
        def __init__(self,url):
                self.url = url
                self.ref_count = 0

        def isMember(self,torrent):
                return self.url in torrent.trackers()

        def ref(self):
                self.ref_count += 1

        def unref(self):
                if self.ref_count > 0:
                        self.ref_count -= 1

        def canBeRemoved(self):
                return self.ref_count == 0


class TrackerGroupsScript:
        def __init__(self):
                self.tracker_map = {} # initialize the dictionary
                self.id = 0                                     
                KTorrent.connect("torrentAdded(const QString &)",self.torrentAdded)
                KTorrent.connect("torrentRemoved(const QString &)",self.torrentRemoved)
                tors = KTorrent.torrents()                                             
                # go over each torrent and collect all the trackers                   
                for t in tors:                                                         
                        self.torrentAdded(t)                                           

        def addTracker(self,tracker):
                g = TrackerGroup(tracker)
                g.ref()
                self.tracker_map[tracker] = g
                KTorrent.log("Adding group for tracker " + tracker)
                KTScriptingPlugin.addGroup(tracker,"network-server","/all/trackers/" + str(self.id),g)
                self.id += 1

        def removeTracker(self,tracker):
                g = self.tracker_map[tracker]
                g.unref()
                if g.canBeRemoved():
                        KTorrent.log("Removing group for tracker " + tracker)
                        KTScriptingPlugin.removeGroup(g.url)
                        del self.tracker_map[tracker]

        def torrentAdded(self,info_hash):
                tor = KTorrent.torrent(info_hash)
                trackers = tor.trackers()
                for tracker in trackers:
                        if not tracker in self.tracker_map:
                                self.addTracker(tracker)
                        else:
                                self.tracker_map[tracker].ref()

        def torrentRemoved(self,info_hash):
                tor = KTorrent.torrent(info_hash)
                trackers = tor.trackers()
                for tracker in trackers:
                        if tracker in self.tracker_map:
                                self.removeTracker(tracker)

        def unload(self):
                trackers = self.tracker_map.keys()
                for tracker in trackers:
                        g = self.tracker_map[tracker]
                        KTorrent.log("Removing group for tracker " + tracker)
                        KTScriptingPlugin.removeGroup(g.url)
                        del self.tracker_map[tracker]

s = TrackerGroupsScript()

# Called when script is unloaded
def unload():
        global s
        s.unload()
        del s

It seams that calling self.tracker_map[tracker].ref()
where tracker is KTorrent.torrent(info_hash).trackers(i)
is doing the work but i don't get how?

thx
George
Moderator
Posts
5421
Karma
1

Mon Jun 08, 2009 4:51 pm
The isMember function of TrackerGroup is called to determine if a torrent is a member of a group :

http://ktorrent.org/wiki/index.php/Writing_Scripts#The_KTScriptingPlugin_Module
Rioting_Pacifist
Registered Member
Posts
27
Karma
0

Wed Jun 17, 2009 7:41 pm
Sorry I still don't get it, I'm still learning python and havent really used classes yet, but how would i put a torrent into a group. would i just call it with true?
Ktorrent.group(groupname).ismember(True)
or would i have to define a the Class (a link to explain classes to a noob would be nice) for my group 1st?
so far the relevant code looks like
Code: Select all
def torrentAdded(ih):
   tor = KTorrent.torrent(ih)
   KTorrent.log("torrentAdded=%s" % tor.name())
   tortype,hd = get_type(tor)
   if hd:
      #make tor a member of tortype+"/HD"
      #make tor a member of "HD/"+tortype
   #make tor a member of tortype

where tortype is just a string with video/film/album/discography/series/etc
George
Moderator
Posts
5421
Karma
1

Wed Jun 17, 2009 11:33 pm
RiotingPacifist wrote:Sorry I still don't get it, I'm still learning python and havent really used classes yet, but how would i put a torrent into a group. would i just call it with true?
Ktorrent.group(groupname).ismember(True)
or would i have to define a the Class (a link to explain classes to a noob would be nice) for my group 1st?
so far the relevant code looks like
Code: Select all
def torrentAdded(ih):
   tor = KTorrent.torrent(ih)
   KTorrent.log("torrentAdded=%s" % tor.name())
   tortype,hd = get_type(tor)
   if hd:
      #make tor a member of tortype+"/HD"
      #make tor a member of "HD/"+tortype
   #make tor a member of tortype

where tortype is just a string with video/film/album/discography/series/etc


You don't call isMember, KT calls isMember. All you need to do is implement isMember.
Rioting_Pacifist
Registered Member
Posts
27
Karma
0

Sat Jul 18, 2009 11:26 pm
So far i've managed this
Code: Select all
#!/usr/biclan/env kross
# -*- coding: utf-8 -*-
import KTorrent
import Kross
import KTScriptingPlugin

gName = "sorter-basic: "
gVer  = "0.1dev"

class MediaGroup:
   def __init__(self,name):
      self.name = str(name)
      self.members = list()
   
   def isMember(self,t):
      return t in self.members
   
   def add(self,t):
      KTorrent.log(t+" added to "+self.name)
      self.members.append(t)
      KTorrent.log(self.members)
   
   #def remove(self,t):
   #   while members.count(t) > 0: self.members.remove(t)

def torrentAdded(t):
   multi = 0
   #for i in range(0,t.numFiles()):
   #   if t.isMultiMediaFile(i): multi +=1
   if multi == 0 : nomultimedia.add(t)
   elif multi < 3: fewmultimedia.add(t)
   elif multi <15: multimedia.add(t)
   else: lotsofmultimedia.add(t)

def startup():
   KTorrent.log(gName+"starting ("+gVer+")")
   for grp in [nomultimedia,fewmultimedia,multimedia,lotsofmultimedia]:
      KTScriptingPlugin.addGroup(grp.name ,"network-server","/all/MediaGroups/"+grp.name,grp)
      KTorrent.log(grp.name+" created")
   for t in KTorrent.torrents():
      torrentAdded(t)
   KTorrent.log(gName+"starting...done")

def shutdown():
   KTorrent.log(gName+"shutdown complete")

#Global definitionas
nomultimedia=MediaGroup("nomultimedia")
fewmultimedia=MediaGroup("fewmultimedia")
multimedia=MediaGroup("multimedia")
lotsofmultimedia=MediaGroup("lotsofmultimedia")
startup()

I think i've got the isMember stuff right, but for some reason adding keeps failing, it seams to work fine when i try it in a python interpreter (without any KTorrent stuff as i don't know how to import Ktorrent module). What am i doing wrong now?
George
Moderator
Posts
5421
Karma
1

Sun Jul 19, 2009 1:24 pm
The parameter t of isMember, is a torrent and you are storing info hashes in the members list. So for this to work you need to do this :

Code: Select all
def isMember(self,t):
    return t.infoHash() in self.members
Rioting_Pacifist
Registered Member
Posts
27
Karma
0

Sun Jul 19, 2009 7:59 pm
Thanks, that has it working :D but:
1) KTorrent.log(self.members) still logs an empty line, which is confusing as your new isMembers function gets the groups populated :S
2) how can i read info on the torrent class? (e.g help(t) when using a normal python interpretor) i know its documented here but is it possible to load KTorrent & Kross into a normal python interpreter as that would probably make it easier to spot the mistakes im making

p.s thx
George
Moderator
Posts
5421
Karma
1

Thu Jul 23, 2009 11:38 am
RiotingPacifist wrote:Thanks, that has it working :D but:
1) KTorrent.log(self.members) still logs an empty line, which is confusing as your new isMembers function gets the groups populated :S


KTorrent.log takes a string as argument, so try converting that to a string :

KTorrent.log(str(self.members))

2) how can i read info on the torrent class? (e.g help(t) when using a normal python interpretor) i know its documented here but is it possible to load KTorrent & Kross into a normal python interpreter as that would probably make it easier to spot the mistakes im making


Don't think this is possible.
Rioting_Pacifist
Registered Member
Posts
27
Karma
0
can i call KTorrent.group(grpname) on my KTScriptingPlugin.addGroup(name,icon,path,obj) created groups?
and if so what do i pass as grpname (ive tried both obj and name (although i may have called it wrong))

here is my code (atm calling the dict[name]
Code: Select all
   KTScriptingPlugin.addGroup(name,icon,gdir+name,dic[name])
   grp = KTorrent.group(dic[name])
   onlynew = KTScriptingPlugin.readConfigEntry(gName+gVer,name+"/onlynew","on")
   grp.setOnlyApplyOnNewTorrents(onlynew)
George
Moderator
Posts
5421
Karma
1
No this is not possible, there are two kinds of groups :
- Default groups (don't have a group policy, the group itself determines which torrents are part of it)
- Custom groups (have a group policy, are created by the user and the torrents are added by the user)

KTScriptingPlugin.addGroup creates a default group, while KTorrent.group returns a custom group.

I think this is certainly something which should change in the API. It's a bit confusing, and the old distinction between default and custom groups is a bit outdated. I'm gonna change this for 3.3.
Rioting_Pacifist
Registered Member
Posts
27
Karma
0
I figure that the logic behind defauldownlaod dirs and speed limits gets complicated when a torrent can be in multiple groups at once, I added a note to the wiki so other noobs don't get confused by the same thing.


Bookmarks



Who is online

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