def tag_danbooru_item(filename, tags, blacklist=None, board_url=None): """Tag a file using a specific :class:`DanbooruItem` tags.""" resource_manager = Nepomuk.ResourceManager.instance() if not resource_manager.initialized(): # Nepomuk not running - bail out return absolute_path = QtCore.QFileInfo(filename).absoluteFilePath() resource = Nepomuk.File(KUrl(absolute_path)) for tag in tags: if blacklist is not None and tag in blacklist: continue nepomuk_tag = Nepomuk.Tag(tag) nepomuk_tag.setLabel(tag) resource.addTag(nepomuk_tag) if board_url is not None: website_resource = Nepomuk.Resource(board_url) website_resource.addType(Nepomuk.Vocabulary.NFO.Website()) website_resource.setLabel(board_url.prettyUrl()) resource.setDescription( i18n("Retrieved from %1").arg(board_url.prettyUrl())) resource.addIsRelated(website_resource)
def addInformationToFile(fileName, model, ids, field=None): if not Common.isKdeAvailable: return if not isinstance(ids, list): ids = [ids] field = False try: # Obtain ratings ratings = Rpc.session.call('/semantic', 'rating', model, ids, field, Rpc.session.context) # Obtain all tags allTags = Rpc.session.call('/semantic', 'tags', model, ids, field, Rpc.session.context) # Obtain all descriptions allDescriptions = Rpc.session.call('/semantic', 'description', model, ids, field, Rpc.session.context) # Obtain all contacts allContacts = Rpc.session.call('/semantic', 'contacts', model, ids, field, Rpc.session.context) except: ratings = {} allTags = {} allDescriptions = {} allContacts = {} # Calculate average rating rating = 0 if ratings: for x in list(ratings.values()): rating += x rating = rating / len(ratings) # Pickup all tags tags = [] for x in list(allTags.values()): tags += x tags = list(set(tags)) # Pickup all descriptions and merge them into one description = '\n--\n'.join(set(allDescriptions.values())) # Pickup all contacts contacts = [] for x in list(allContacts.values()): contacts += x contacts = list(set(contacts)) from PyKDE4.nepomuk import Nepomuk from PyKDE4.soprano import Soprano resource = Nepomuk.Resource('file://%s' % fileName, Soprano.Vocabulary.Xesam.File()) resource.setTags([Nepomuk.Tag(tag) for tag in tags]) resource.setRating(max(rating, 0)) resource.setDescription(description) if not resource.isValid(): return manager = Nepomuk.ResourceManager.instance() # We cannot use manager.mainModel() because bindings for that function do not exist yet. # So Sebastian suggested this workaround. client = Soprano.Client.DBusClient('org.kde.NepomukStorage') models = client.allModels() if not models: return model = client.createModel(models[0]) emails = ['<mailto:%s>' % contact for contact in contacts] emails = ', '.join(emails) if emails: # Search if there are any contacts on user's addresses with these e-mails. # iterator = model.executeQuery( "PREFIX nco: <http://www.semanticdesktop.org/ontologies/2007/03/22/nco#> SELECT ?name WHERE { ?contact nco:hasEmailAddress ?o. ?contact nco:fullname ?name }", Soprano.Query.QueryLanguageSparql ) iterator = model.executeQuery( "PREFIX nco: <http://www.semanticdesktop.org/ontologies/2007/03/22/nco#> SELECT ?contact WHERE { ?contact nco:hasEmailAddress %s. }" % emails, Soprano.Query.QueryLanguageSparql) contacts = [] # First store all contacts we want the document to be related to because # Soprano doesn't support adding resources while iterating. while next(iterator): x = iterator.binding('contact') if x.isResource(): contacts.append(str(x.uri())) # Add the relation to the corresponding PIMO of the resource. for contact in contacts: resource.addIsRelated(Nepomuk.Resource(x.uri()).pimoThing())