def create_references(claim): try: source_claim_list = [] for k, v in references.items(): source_claim = pywikibot.Claim(repo, k, isReference=True) trgt_item = None if v[0] == 'id': trgt_item = pywikibot.ItemPage(repo, v[1]) elif v[0] == 'url': trgt_item = v[1] source_claim.setTarget(trgt_item) source_claim_list.append(source_claim) claim.addSources(source_claim_list) print('References added') return True except: return False
def addItemStatement(self, item, pid, qid): ''' Helper function to add a statement ''' if not qid: return False claims = item.get().get('claims') if pid in claims: return newclaim = pywikibot.Claim(self.repo, pid) destitem = pywikibot.ItemPage(self.repo, qid) newclaim.setTarget(destitem) pywikibot.output(u'Adding %s->%s to %s' % (pid, qid, item)) item.addClaim(newclaim) return newclaim
def addDateProperty(self, itempage, datestring, property, refurl): ''' Try to find a valid date and add it to the itempage using property :param itempage: The ItemPage to update :param datestring: The string containing the date :param property: The property to add (for example date of birth or date of death) :param refurl: The url to add as reference :return: ''' datestring = datestring.strip() dateregex = u'^(?P<day>\d\d)-(?P<month>\d\d)-(?P<year>\d\d\d\d)$' datelocationregex = u'^[^,]+,[\s\t\r\n]*(?P<day>\d\d)-(?P<month>\d\d)-(?P<year>\d\d\d\d)$' yearregex = u'^(?P<year>\d\d\d\d)$' yearlocationregex = u'^[^,]+,[\s\t\r\n]*(?P<year>\d\d\d\d)$' datematch = re.match(dateregex, datestring) datelocationmatch = re.match(datelocationregex, datestring) yearmatch = re.match(yearregex, datestring) yearlocationmatch = re.match(yearlocationregex, datestring) newdate = None if datematch: newdate = pywikibot.WbTime( year=int(datematch.group(u'year')), month=int(datematch.group(u'month')), day=int(datematch.group(u'day'))) elif datelocationmatch: newdate = pywikibot.WbTime( year=int(datelocationmatch.group(u'year')), month=int(datelocationmatch.group(u'month')), day=int(datelocationmatch.group(u'day'))) #elif monthmatch: # newdate = pywikibot.WbTime( year=int(monthmatch.group(u'year')), # month=int(monthmatch.group(u'month'))) elif yearmatch: newdate = pywikibot.WbTime( year=int(yearmatch.group(u'year'))) elif yearlocationmatch: newdate = pywikibot.WbTime( year=int(yearlocationmatch.group(u'year'))) else: #print datestring return False newclaim = pywikibot.Claim(self.repo, property) newclaim.setTarget(newdate) itempage.addClaim(newclaim) self.addReference(itempage, newclaim, refurl)
def run(self): # pywikibot/families/eagle_family.py site = pywikibot.Site('en', 'eagle') repo = site.data_repository() idProp = self.getOption('idProp') insertProp = self.getOption('insertProp') matchDict = self.getOption('matchDict') itemList = get_property_list.getItemsForProperty( repo, idProp, additionalProperties=[insertProp]) for i in itemList: if i['property'] not in matchDict: continue self.current_page = pywikibot.ItemPage(repo, i['title']) pywikibot.output('Item matched by P' + idProp + ': ' + i['property']) toInsert = matchDict[i['property']] pywikibot.output('Attempting to insert P' + insertProp + ': ' + toInsert) if insertProp in i['additionalProperties']: if i['additionalProperties'][insertProp] == toInsert: pywikibot.output('The item already has the property ' + insertProp + ' with an identical content. Skipping') else: pywikibot.output('The item already has the property ' + insertProp + ' with a different content: ' + i['additionalProperties'][insertProp] + '. Skipping') continue newClaim = pywikibot.Claim(repo, 'P' + insertProp) newClaim.setTarget(toInsert) if self.user_confirm( 'Do you really want do add the claim for property P' + insertProp + '?'): self.current_page.addClaim(newClaim)
def add_start_time(repo, item, data, ticker_info, exchange): if get_valid_qualifier(exchange, START_TIME): return if not ticker_info.get('listdate'): return start_date = isoparse(ticker_info.get('listdate')) wb_start_date = pywikibot.WbTime(year=start_date.year, month=start_date.month, day=start_date.day) start_claim = pywikibot.Claim(repo, START_TIME, is_qualifier=True) start_claim.setTarget(wb_start_date) update_qualifiers(repo, exchange, [start_claim], comment='import start ticker time') name = ticker_info.get('name') references = make_reference(repo, name) update_sources(exchange, references, "add reference")
def __init__(self, generator, wdproperty=u'P18'): """ Initializer. @param generator: A generator that yields Page objects @type generator: generator @param wdproperty: The property to add. Should be of type commonsMedia @type wdproperty: str """ super(IllustrateRobot, self).__init__() self.generator = generator self.wdproperty = wdproperty self.cacheSources() claim = pywikibot.Claim(self.repo, self.wdproperty) if claim.type != 'commonsMedia': raise ValueError(u'%s is of type %s, should be commonsMedia' % (self.wdproperty, claim.type))
def proposed_by(self, textvalue): prop = self.current_page if 'P3254' not in prop.claims: try: int(textvalue) except ValueError as e: pywikibot.exception(e) else: claim = pywikibot.Claim(self.repo, 'P3254') target = ('https://www.wikidata.org/wiki/' 'Wikidata:Property_proposal/Archive/{}#{}').format( textvalue, prop.id) claim.setTarget(target) claim.addSource(self.get_source()) return self.user_add_claim(prop, claim, summary=self.make_summary()) return False
def create_references(claim, references): try: source_claim_list = [] for k, v in references.items(): source_claim = pywikibot.Claim(repo, k, isReference=True) trgt_item = None if v[0] == 'id': trgt_item = pywikibot.ItemPage(repo, v[1]) elif v[0] == 'url': trgt_item = v[1] source_claim.setTarget(trgt_item) source_claim_list.append(source_claim) claim.addSources(source_claim_list) logging.info('{} References added to claim'.format( len(source_claim_list))) return True except: return False
def __init__(self, generator, wdproperty='P18') -> None: """ Initializer. @param generator: A generator that yields Page objects @type generator: generator @param wdproperty: The property to add. Should be of type commonsMedia @type wdproperty: str """ super().__init__() self.generator = generator self.wdproperty = wdproperty self.cacheSources() claim = pywikibot.Claim(self.repo, self.wdproperty) if claim.type != 'commonsMedia': raise ValueError('{} is of type {}, should be commonsMedia'.format( self.wdproperty, claim.type))
def __init__(self, generator, wdproperty=u'P18'): """ Constructor. Arguments: * generator - A generator that yields Page objects. * wdproperty - The property to add. Should be of type commonsMedia """ super(IllustrateRobot, self).__init__() self.generator = pagegenerators.PreloadingGenerator(generator) self.wdproperty = wdproperty self.cacheSources() claim = pywikibot.Claim(self.repo, self.wdproperty) if claim.type != 'commonsMedia': raise ValueError(u'%s is of type %s, should be commonsMedia' % (self.wdproperty, claim.type))
def run(self): """ Starts the robot. """ for itempage in self.generator: pywikibot.output(u'Working on %s' % (itempage.title(), )) data = itempage.get() claims = data.get('claims') # Do some checks if u'P195' not in claims: pywikibot.output(u'No collection found, skipping') continue if len(claims[u'P195']) != 1: pywikibot.output(u'Found multiple collections, skipping') continue if u'P276' in claims: if not self.correctlocation: pywikibot.output( u'Already has a location and not correcting, done') continue elif len(claims[u'P276']) != 1: pywikibot.output( u'Found multiple locations to correct, skipping') continue else: locationclaim = claims.get('P276')[0] currentlocation = locationclaim.getTarget() collection = claims.get('P195')[0].getTarget() # TODO: Figure out how to get the magic edit summary on Wikidata summary = 'replacing [[Wikidata:WikiProject sum of all paintings/Imprecise location|imprecise location]] [[%s]]' % ( currentlocation.title(), ) pywikibot.output(u'Adding collection %s as new location' % (collection.title(), )) locationclaim.changeTarget(collection, summary=summary) else: #Get the collection so we can add it later collection = claims.get('P195')[0].getTarget() summary = u'based on collection (which has coordinates)' newclaim = pywikibot.Claim(self.repo, u'P276') newclaim.setTarget(collection) pywikibot.output(u'Adding collection %s as location' % (collection.title(), )) itempage.addClaim(newclaim, summary=summary)
def run(self): """ Starts the robot. """ data = self.property.get() claims = data.get('claims') if len(self.nonHumans) > 100: pywikibot.output( u'More than 100 non-humans, not working on this one') return if not u'P2302' in claims: # Something went wrong return for claim in claims.get(u'P2302'): if not claim.getTarget().title() == u'Q21503247': # Not item requires statement constraint continue foundHumanProp = None for humanProp in self.humanProps: if claim.has_qualifier(u'P2306', humanProp): foundHumanProp = humanProp if not foundHumanProp: continue for nonHuman in self.nonHumans: if claim.has_qualifier(u'P2303', nonHuman.title()): pywikibot.output( u'The item %s is already listed as exception for %s' % ( nonHuman.title(), foundHumanProp, )) else: pywikibot.output(u'Going to add %s as exception to %s' % ( nonHuman.title(), foundHumanProp, )) newqualifier = pywikibot.Claim(self.repo, u'P2303') newqualifier.setTarget(nonHuman) summary = u'not a human, adding as exception to [[Property:%s]]' % ( foundHumanProp, ) claim.addQualifier(newqualifier, summary=summary)
def run(self): """ Starts the robot. """ for itempage in self.generator: pywikibot.output(u'Working on %s' % (itempage.title(), )) if not itempage.exists(): pywikibot.output(u'Item does not exist, skipping') continue data = itempage.get() claims = data.get('claims') # Do some checks so we are sure we found exactly one inventory number and one collection if u'P651' not in claims: pywikibot.output(u'No Biografisch portaal found, skipping') continue if u'P1749' in claims: pywikibot.output( u'Already has Parlement & Politiek ID, skipping') continue bioid = claims.get(u'P651')[0].getTarget() biourl = u'http://www.biografischportaal.nl/persoon/%s?' % ( bioid, ) refurl = biourl # Do some checking if it actually exists? bioPage = requests.get(biourl, verify=False) ppregex = u'href=\"http:\/\/www\.parlementairdocumentatiecentrum\.nl\/id\/([^\"]+)\"\>' ppmatch = re.search(ppregex, bioPage.text) if ppmatch: ppid = ppmatch.group(1) # Add the P&P id to the item newclaim = pywikibot.Claim(self.repo, u'P1749') newclaim.setTarget(ppid) summary = u'Adding link to Parlement & Politiek based on link on Biografisch Portaal number %s' % ( bioid, ) pywikibot.output(summary) itempage.addClaim(newclaim, summary=summary) self.addReference(itempage, newclaim, refurl)
def run(self): """ Starts the robot. """ for itempage in self.generator: pywikibot.output(u'Working on %s' % (itempage.title(), )) if not itempage.exists(): pywikibot.output(u'Item does not exist, skipping') continue data = itempage.get() claims = data.get('claims') # Do some checks so we are sure we found exactly one inventory number and one collection if u'P651' not in claims: pywikibot.output(u'No Biografisch portaal found, skipping') continue if u'P650' in claims: pywikibot.output(u'Already has RKDartists, skipping') continue bioid = claims.get(u'P651')[0].getTarget() biourl = u'http://www.biografischportaal.nl/persoon/%s?' % ( bioid, ) refurl = biourl # Do some checking if it actually exists? bioPage = requests.get(biourl, verify=False) rkdregex = u'href\=\"http:\/\/www\.rkd\.nl\/rkddb\/dispatcher\.aspx\?action=search\&database\=ChoiceArtists\&search\=priref\=(\d+)\"\>' rkdmatch = re.search(rkdregex, bioPage.text) if rkdmatch: rkdid = rkdmatch.group(1) # Add the RKD artists id to the item newclaim = pywikibot.Claim(self.repo, u'P650') newclaim.setTarget(rkdid) summary = u'Adding link to RKDartists based on link on Biografisch Portaal number %s' % ( bioid, ) pywikibot.output(summary) itempage.addClaim(newclaim, summary=summary) self.addReference(itempage, newclaim, refurl)
def addClaim(repo='', item='', claim='', value='', valuelang='', ref=''): if repo and item and claim and value: print("Adding claim", claim, "value", value.encode('utf-8')) claimx = pywikibot.Claim(repo, claim) if re.search(r'\d\d\d\d-\d\d-\d\d', value): target = pywikibot.WbTime(year=int(value.split('-')[0]), month=int(value.split('-')[1]), day=int(value.split('-')[2])) elif value.startswith('Q'): target = pywikibot.ItemPage(repo, value) elif valuelang: target = pywikibot.WbMonolingualText(text=value, language=valuelang) else: target = value claimx.setTarget(target) item.addClaim(claimx, summary='BOT - Adding 1 claim') if ref: addReference(repo=repo, claim=claimx, ref=ref)
def run(self): """ Starts the bot. """ for page in self.generator: pywikibot.output(u'Working on %s' % page.title()) item = pywikibot.ItemPage.fromPage(page) if item.exists(): pywikibot.output(u'Found %s' % item.title()) imagename = page.properties().get('page_image') if imagename: claims = item.get().get('claims') if self.wdproperty in claims: pywikibot.output( u'Item %s already contains image (%s)' % (item.title(), self.wdproperty)) else: newclaim = pywikibot.Claim(self.repo, self.wdproperty) commonssite = pywikibot.Site("commons", "commons") imagelink = pywikibot.Link(imagename, source=commonssite, defaultNamespace=6) image = pywikibot.ImagePage(imagelink) if image.isRedirectPage(): image = pywikibot.ImagePage( image.getRedirectTarget()) if not image.exists(): pywikibot.output( '[[%s]] doesn\'t exist so I can\'t link to it' % (image.title(), )) continue newclaim.setTarget(image) pywikibot.output( 'Adding %s --> %s' % (newclaim.getID(), newclaim.getTarget())) item.addClaim(newclaim) # A generator might yield pages from multiple sites source = self.getSource(page.site) if source: newclaim.addSource(source, bot=True)
def _insert_references_local(self, claim, new_references): # Add new sources if 'references' not in claim and new_references: claim['references'] = [] refs = defaultdict(list) for pid, value in new_references: reference = pywikibot.Claim(self.repo, pid) target_value = value if reference.type == 'wikibase-item': target_value = self.get_item(qid=value) elif reference.type == 'quantity': target_value = self.__dict2quantity(value) reference.setTarget(target_value) reference.isReference = True refs[pid].append(reference.toJSON()) if refs: claim['references'].append({'snaks': refs})
def run(self): """ Starts the robot. """ searchregex = u'\s*\|\s*museum_internal_id\s*=\s*(\d+)' for filepage in self.generator: text = filepage.get() match = re.search(searchregex, text) if not match: pywikibot.output(u'No match found') continue idnumber = match.group(1) if len(idnumber) == 1: invnum = u'DPG00%s' % (idnumber, ) elif len(idnumber) == 2: invnum = u'DPG0%s' % (idnumber, ) else: invnum = u'DPG%s' % (idnumber, ) artworkItem = None if invnum in self.artworkIds: artworkItemTitle = u'Q%s' % (self.artworkIds.get(invnum), ) print artworkItemTitle artworkItem = pywikibot.ItemPage(self.repo, title=artworkItemTitle) else: pywikibot.output(u'No artwork item found for id %s' % (invnum, )) continue data = artworkItem.get() claims = data.get('claims') if u'P18' not in claims: newclaim = pywikibot.Claim(self.repo, u'P18') newclaim.setTarget(filepage) pywikibot.output( 'Adding %s --> %s based on inventory number %s' % (newclaim.getID(), newclaim.getTarget(), invnum)) artworkItem.addClaim(newclaim)
def add_claim(self, claim): """ Function to add a claim to the item. @:param claim: claim @:type claim: dict @:return: wd_claim @:rtype: pywikibot.page.Claim """ wd_claim = pywikibot.Claim(self.SITE, claim['id']) wd_claim.setTarget(self.get_target(wd_claim, claim['value'])) self.item.addClaim(wd_claim, bot=True, summary='New claim added by JudgeBot') return wd_claim
def test_search_entity(self): bot = Bot.factory([ '--test', '--user=FLOSSbotCI', '--verbose', ]) plugin = Plugin(bot, bot.args) # ensure space, - and _ are accepted name = WikidataHelper.random_name() + "-some thing_else" entity = { "labels": { "en": { "language": "en", "value": name, } }, } first = plugin.bot.site.editEntity({'new': 'item'}, entity) first = pywikibot.ItemPage(bot.site, first['entity']['id'], 0) second = plugin.bot.site.editEntity({'new': 'item'}, entity) second = pywikibot.ItemPage(bot.site, second['entity']['id'], 0) with pytest.raises(ValueError) as e: plugin.search_entity(plugin.bot.site, name, type='item') assert "found multiple items" in str(e.value) claim = pywikibot.Claim(plugin.bot.site, plugin.P_instance_of, 0) claim.setTarget(plugin.Q_Wikimedia_disambiguation_page) first.addClaim(claim) found = plugin.search_entity(bot.site, name, type='item') assert found.getID() == second.getID() plugin.bot.site.editEntity({'new': 'item'}, entity) with pytest.raises(ValueError) as e: plugin.search_entity(plugin.bot.site, name, type='item') assert "found multiple items" in str(e.value) Plugin.authoritative['test'][name] = second.getID() found = plugin.search_entity(plugin.bot.site, name, type='item') assert found.getID() == second.getID()
def add_commonsmedia(self, prop, filename, qualifiers=None, references=None): claim = pywikibot.Claim(self.repo, prop) commons = pywikibot.Site("commons", "commons") imagelink = pywikibot.Link(filename, source=commons, defaultNamespace=6) image = pywikibot.FilePage(imagelink) if image.isRedirectPage(): image = pywikibot.page.ImagePage(image.getRedirectTarget()) if not image.exists(): raise Exception(f"Could not find image: {filename}") claim.setTarget(image) self.add_claim(claim, qualifiers, references)
def addItemStatement(self, item, pid, qid, url): ''' Helper function to add a statement ''' if not qid: return False claims = item.get().get('claims') if pid in claims: return newclaim = pywikibot.Claim(self.repo, pid) destitem = pywikibot.ItemPage(self.repo, qid) if destitem.isRedirectPage(): destitem = destitem.getRedirectTarget() newclaim.setTarget(destitem) pywikibot.output(u'Adding %s->%s to %s' % (pid, qid, item)) item.addClaim(newclaim) self.addReference(item, newclaim, url)
def import_claims(self): imports_count = 0 for claim_index, claim in self.claims_df.iterrows(): regulator_qid = claim['regulator_QID'] property_pid = claim['property'] target_qid = claim['target_QID'] site = pywikibot.Site("wikidata", "wikidata") repo = site.data_repository() item = pywikibot.ItemPage(repo, regulator_qid) item.get() if item.claims: if property_pid in item.claims: if item.claims[property_pid][0].getTarget() == target_qid: continue claim = pywikibot.Claim(repo, property_pid) target = pywikibot.ItemPage(repo, target_qid) claim.setTarget(target) item.addClaim(claim, summary=u'Adding claim') imports_count += 1 return imports_count
def make_claim(value, prop, target_site): """ Create a pywikibot Claim representation of the internally formatted value. @param value: str|dict The internally formatted claim value @param prop: str Property of the claim @param target_site: pywikibot.Site to which Structured Data is uploaded @return: pywikibot.Claim @raises: ValueError """ repo = target_site.data_repository() claim = pywikibot.Claim(repo, prop) if common.is_str(value): _set_claim_target(claim, value) elif isinstance(value, dict): set_complex_claim_value(value, claim) else: raise ValueError( 'Incorrectly formatted property value: {}'.format(value)) return claim
def add_source_for_claim(repo, claim): existing_sources = claim.getSources() rfc_editor_database_item = pywikibot.ItemPage(repo, 'Q33133762') source_with_required_claim = find_source_with_claim(repo, existing_sources, 'P248', rfc_editor_database_item) if source_with_required_claim is None: new_stated_in_claim = pywikibot.Claim(repo, 'P248') new_stated_in_claim.setTarget(rfc_editor_database_item) new_retrieved_claim = create_retrieved_claim_for_today(repo) claim.addSources([new_stated_in_claim, new_retrieved_claim]) else: existing_retrieved_claim = False for source_property_id, source_claim in source_with_required_claim.items(): if source_property_id == 'P813': existing_retrieved_claim = True break if not existing_retrieved_claim: new_source = source_with_required_claim new_retrieved_claim = create_retrieved_claim_for_today(repo) new_source.append(new_retrieved_claim) claim.removeSources(source_with_required_claim) claim.addSources(new_source)
def number_of_ids(self, textvalue): prop = self.current_page if prop.type == 'external-id' and 'P4876' not in prop.claims: remove = False match = self.regexes['quantity'].search(textvalue) if match: try: num = int(match.group('value')) except ValueError as e: pywikibot.exception(e) else: target = pywikibot.WbQuantity(num, site=self.repo) claim = pywikibot.Claim(self.repo, 'P4876') claim.setTarget(target) claim.addSource(self.get_source()) remove = self.user_add_claim(prop, claim, summary=self.make_summary()) else: remove = True return remove
def testWikibase(self): if not site.has_transcluded_data: return repo = site.data_repository() item = pywikibot.ItemPage.fromPage(mainpage) self.assertType(item, pywikibot.ItemPage) self.assertEqual(item.getID(), 'Q5296') self.assertEqual(item.title(), 'Q5296') self.assertTrue('en' in item.labels) self.assertEqual(item.labels['en'], 'Main Page') self.assertTrue('en' in item.aliases) self.assertTrue('HomePage' in item.aliases['en']) self.assertEqual(item.namespace(), 0) item2 = pywikibot.ItemPage(repo, 'q5296') self.assertEqual(item2.getID(), 'Q5296') self.assertEqual(item.labels['en'], 'Main Page') prop = pywikibot.PropertyPage(repo, 'Property:P21') self.assertEqual(prop.type, 'wikibase-item') self.assertEqual(prop.namespace(), 120) claim = pywikibot.Claim(repo, 'p21') self.assertRaises(ValueError, claim.setTarget, value="test") claim.setTarget(pywikibot.ItemPage(repo, 'q1')) self.assertEqual(claim._formatDataValue(), {'entity-type': 'item', 'numeric-id': 1}) # test WbTime t = pywikibot.WbTime(site=wikidata, year=2010, hour=12, minute=43) self.assertEqual(t.toTimestr(), '+00000002010-01-01T12:43:00Z') self.assertRaises(ValueError, pywikibot.WbTime, site=wikidata, precision=15) self.assertRaises(ValueError, pywikibot.WbTime, site=wikidata, precision='invalid_precision') # test WbQuantity q = pywikibot.WbQuantity(amount=1234, error=1) self.assertEqual(q.toWikibase(), {'amount': 1234, 'lowerBound': 1233, 'upperBound': 1235, 'unit': '1', }) q = pywikibot.WbQuantity(amount=5, error=(2, 3)) self.assertEqual(q.toWikibase(), {'amount': 5, 'lowerBound': 2, 'upperBound': 7, 'unit': '1', }) self.assertRaises(ValueError, pywikibot.WbQuantity, amount=None, error=1) self.assertRaises(NotImplementedError, pywikibot.WbQuantity, amount=789, unit='invalid_unit') # test WikibasePage.__cmp__ self.assertEqual(pywikibot.ItemPage.fromPage(mainpage), pywikibot.ItemPage(repo, 'q5296'))
def handle_source_url(self, source): ret = False for prop in self.url_props: keys = set(source.keys()) if prop not in keys: continue if keys - (self.whitelist_props | {prop}): continue if len(source[prop]) > 1: #continue? return ret snak = next(iter(source[prop])) url = snak.getTarget() if not url: continue target = None try: if url.startswith(self.url_start): target = pywikibot.ItemPage(self.repo, url[len(self.url_start):]) elif url.startswith(self.repo.concept_base_uri): target = pywikibot.ItemPage( self.repo, url[len(self.repo.concept_base_uri):]) except pywikibot.InvalidTitle: pass except ValueError: pass if target: if target.isRedirectPage(): target = target.getRedirectTarget() if target != snak.on_item: snak = pywikibot.Claim(self.repo, self.inferred_from, isReference=True) snak.setTarget(target) source.setdefault(self.inferred_from, []).append(snak) source.pop(prop) ret = True return ret
def add_to_wd(x, service, url, qid, repo): item = '' try: item = pywikibot.ItemPage(repo, qid) except: pywikibot.output(u'Error: QID not found!') return '' claims = item.get(u'claims') if propery[service] in claims[u'claims']: pywikibot.output(u'Error: Already have {} ID!'.format(service)) return '' retrieved, statedin = getRef(repo, url) stringclaim = pywikibot.Claim(repo, propery[service]) #add the value stringclaim.setTarget(x) item.addClaim(stringclaim, summary=u'Adding {} ID'.format(service)) stringclaim.addSources([retrieved, statedin], summary=u'Adding reference(s) to {} ID'.format(service)) pywikibot.output(u'Success: Added {} ID!'.format(service))
def addImageToWikidata(self, metadata, imagefile, summary=u'Added the image'): """ Add the image to the Wikidata item. This might add an extra image if the item already has one """ artworkItem = pywikibot.ItemPage(self.repo, title=metadata.get(u'item')) data = artworkItem.get() claims = data.get('claims') newclaim = pywikibot.Claim(self.repo, u'P18') newclaim.setTarget(imagefile) foundimage = False replacedimage = False if u'P18' in claims: for claim in claims.get(u'P18'): if claim.getTarget().title() == newclaim.getTarget().title(): pywikibot.output(u'The image is already on the item') foundimage = True if not foundimage and len(claims.get(u'P18')) == 1: claim = claims.get(u'P18')[0] newimagesize = imagefile.latest_file_info.size currentsize = claim.getTarget().latest_file_info.size # Only replace if new one is at least 4 times larger if currentsize * 4 < newimagesize: summary = u'replacing with much larger image' claim.changeTarget(imagefile, summary=summary) replacedimage = True if not foundimage and not replacedimage: pywikibot.output('Adding %s --> %s' % (newclaim.getID(), newclaim.getTarget())) artworkItem.addClaim(newclaim, summary=summary) # Only remove if we had one suggestion. Might improve the logic in the future here if u'P4765' in claims and len(claims.get(u'P4765')) == 1: artworkItem.removeClaims(claims.get(u'P4765')[0], summary=summary)