def main(): P65 = pywikibot.PropertyPage(datasite, 'P65') for backlink in P65.backlinks(): changeWpAndMoe(backlink.title()) P66 = pywikibot.PropertyPage(datasite, 'P66') for backlink in P66.backlinks(): changeWpAndMoe(backlink.title())
def test_alternate_property_namespace(self): prop = pywikibot.PropertyPage(self.repo, 'P21') self.assertEqual(prop.namespace(), 92) self.assertEqual(prop.id, 'P21') self.assertEqual(prop.title(), 'Prop:P21') self.assertEqual(prop._defined_by(), {'ids': 'P21'}) prop = pywikibot.PropertyPage(self.repo, 'Prop:P21') self.assertEqual(prop.namespace(), 92) self.assertEqual(prop.id, 'P21') self.assertEqual(prop.title(), 'Prop:P21') self.assertEqual(prop._defined_by(), {'ids': 'P21'})
def get_property(self, label=None, pid=None): """ Return the fisrt property with the given label""" if pid is not None: return pywikibot.PropertyPage(self.repo, pid) if label is not None: if label in self.label_pid: pid = self.label_pid[label] return pywikibot.PropertyPage(self.repo, pid) return None
def add_property(self, summary, label, description, aliases, data_type): """Create new property if it doesn't already exists. Return the property PID.""" pid = self.get_pid(label) if pid is not None: return pid new_prop = pywikibot.PropertyPage(self.repo, datatype=data_type) data = { 'labels': { 'en': label }, 'aliases': { 'en': aliases }, 'descriptions': { 'en': description } } self.editEntity(new_prop, data, summary, asynchronous=False) pid = new_prop.getID() # Keep it in the cache self.label_pid[label] = pid return pid
def newProperties(startdate): payload = { 'action': 'query', 'list': 'recentchanges', 'rctype': 'new', 'rcnamespace': '120', 'rclimit': 100, 'rcend': str(startdate), 'format': 'json' } r = requests.get('https://www.wikidata.org/w/api.php', params=payload) data = r.json() data['query']['recentchanges'].sort(key=lambda m: m['pageid']) externalIdProps = [] generalProps = [] for m in data['query']['recentchanges']: entity = pywikibot.PropertyPage(repo, m['title'].replace('Property:', '')) entity.get() label = entity.labels['en'] if 'en' in entity.labels else m[ 'title'].replace('Property:', '') props = externalIdProps if entity.type == 'external-id' else generalProps props.append('[[:d:{0}|{1}]]'.format(m['title'], label)) externalIdText = ', '.join(externalIdProps) if externalIdProps else 'none' otherText = ', '.join(generalProps) if generalProps else 'none' text = '* Newest [[d:Special:ListProperties|properties]]:\n' + \ '** General datatypes: ' + otherText + '\n' + \ '** External identifiers: ' + externalIdText return text
def changeP67toP71(title): title = title.replace('Item:', '') title = title.replace('Property:', '') print(title) if title[0] == 'Q': myitem = pywikibot.ItemPage(datasite, title) elif title[0] == 'P': myitem = pywikibot.PropertyPage(datasite, title) else: print('\t Not Wikibase page') return myitem.get() if 'P71' not in myitem.claims: new_claim = pywikibot.page.Claim(datasite, 'P71') url = myitem.claims['P67'][0].getTarget() targettitle = url.replace('https://www.wikidata.org/wiki/', '') print('\t', targettitle) new_claim.setTarget(targettitle) print('\t', new_claim) myitem.addClaim(new_claim, summary='轉換屬性') myitem.removeClaims(myitem.claims['P67'], summary='移除舊屬性')
def getItem(site, title): if title[0] == 'Q': return pywikibot.ItemPage(site, title) if title[0] == 'P': return pywikibot.PropertyPage(site, title) print('\t Not Wikibase page') return None
def testWikibase(self): """Wikibase tests.""" repo = self.get_repo() item_namespace = repo.namespaces[0] self.assertEqual(item_namespace.defaultcontentmodel, 'wikibase-item') item = pywikibot.ItemPage.fromPage(self.mainpage) self.assertIsInstance(item, pywikibot.ItemPage) self.assertEqual(item.getID(), 'Q5296') self.assertEqual(item.title(), 'Q5296') self.assertIn('en', item.labels) self.assertTrue(item.labels['en'].lower().endswith('main page')) self.assertIn('en', item.aliases) self.assertIn('Home page', item.aliases['en']) self.assertEqual(item.namespace(), 0) item2 = pywikibot.ItemPage(repo, 'q5296') self.assertEqual(item2.getID(), 'Q5296') item2.get() self.assertTrue(item2.labels['en'].lower().endswith('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._formatValue(), { 'entity-type': 'item', 'numeric-id': 1 })
def setUpClass(cls): """Set up test class.""" super(TestIsbnWikibaseBot, cls).setUpClass() # Check if the unit test item page and the property both exist item_ns = cls.get_repo().item_namespace for page in cls.get_site().search('IsbnWikibaseBotUnitTest', total=1, namespaces=item_ns): cls.test_page_qid = page.title() item_page = ItemPage(cls.get_repo(), page.title()) for pid, claims in item_page.get()['claims'].items(): for claim in claims: prop_page = pywikibot.PropertyPage(cls.get_repo(), claim.getID()) prop_page.get() if ('ISBN-10' in prop_page.labels.values() and claim.getTarget() == '097522980x'): return raise unittest.SkipTest(u'%s: "ISBN-10" property was not found in ' u'"IsbnWikibaseBotUnitTest" item page' % cls.__name__) raise unittest.SkipTest( u'%s: "IsbnWikibaseBotUnitTest" item page was not found' % cls.__name__)
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.getType(), '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 WikibasePage.__cmp__ self.assertEqual(pywikibot.ItemPage.fromPage(mainpage), pywikibot.ItemPage(repo, 'q5296'))
def get_formatter_regex(self): if 'formatter' not in self.regexes: prop = pywikibot.PropertyPage(self.repo, 'P1630') prop.get() self.regexes['formatter'] = re.compile( self.get_regex_from_prop(prop)) return self.regexes['formatter']
def main(): for backlink in pywikibot.PropertyPage( datasite, 'P1').backlinks(namespaces=[120]): # 巴哈姆特作品資料 myitem = pywikibot.ItemPage(datasite, backlink.title()) claims = myitem.get()['claims'] if 'P23' not in claims: importAcgGamerLink(backlink.title())
def get_formatter_and_regex(self, prop): if prop not in self.cache: formatter = regex = None ppage = pywikibot.PropertyPage(self.repo, prop) if 'P1630' in ppage.claims: if len(ppage.claims['P1630']) > 1: preferred = [ cl for cl in ppage.claims['P1630'] if cl.rank == 'preferred' ] if len(preferred) == 1: formatter = preferred[0].target else: formatter = ppage.claims['P1630'][0].target if 'P1793' in ppage.claims: if len(ppage.claims['P1793']) > 1: preferred = [ cl for cl in ppage.claims['P1793'] if cl.rank == 'preferred' ] if len(preferred) == 1: regex = preferred[0].target else: regex = ppage.claims['P1793'][0].target self.cache[prop] = (formatter, regex) return self.cache[prop]
def test_globe_coordinate(self): property_page = pywikibot.PropertyPage(wikidata, 'P625') self.assertEquals(property_page.type, 'globe-coordinate') self.assertEquals(property_page.getType(), 'globecoordinate') claim = pywikibot.Claim(wikidata, 'P625') self.assertEquals(claim.type, 'globe-coordinate') self.assertEquals(claim.getType(), 'globecoordinate')
def subgenerator(self): for page in self._generator: if '/' in page.title(): continue if page.isTalkPage(): page = page.toggleTalkPage() if page.namespace() == self.repo.property_namespace: yield pywikibot.PropertyPage(self.repo, page.title(with_ns=False))
def changeWpAndMoe(title): title = title.replace('Item:', '') title = title.replace('Property:', '') print(title) data = { 'claims': [] } if title[0] == 'Q': myitem = pywikibot.ItemPage(datasite, title) elif title[0] == 'P': myitem = pywikibot.PropertyPage(datasite, title) else: print('\t Not Wikibase page') return myitem.get() if 'P68' not in myitem.claims and 'P65' in myitem.claims: url = myitem.claims['P65'][0].getTarget() targettitle = url.replace('https://zh.wikipedia.org/wiki/', '') targettitle = converttitle(zhsite, targettitle) if targettitle: new_claim = pywikibot.page.Claim(datasite, 'P68') new_claim.setTarget(targettitle) print('\t Add P68', targettitle) data['claims'].append(new_claim.toJSON()) if 'P70' not in myitem.claims and 'P66' in myitem.claims: url = myitem.claims['P66'][0].getTarget() targettitle = url.replace('https://zh.moegirl.org/', '') targettitle = converttitle(moesite, targettitle) if targettitle: new_claim = pywikibot.page.Claim(datasite, 'P70') new_claim.setTarget(targettitle) print('\t Add P70', targettitle) data['claims'].append(new_claim.toJSON()) if 'P65' in myitem.claims: data['claims'].append({"id": myitem.claims['P65'][0].toJSON()['id'], "remove": ""}) print('\t Del P65') if 'P66' in myitem.claims: data['claims'].append({"id": myitem.claims['P66'][0].toJSON()['id'], "remove": ""}) print('\t Del P66') print('\t', data) session.post(API, data={ 'action': 'wbeditentity', 'format': 'json', 'id': title, 'data': json.dumps(data), 'summary': '轉換屬性', 'token': csrftoken, 'bot': 1, }).json()
def set_target(self, snak, value): if value in ('somevalue', 'novalue'): snak.setSnakType(value) return True if snak.type == 'wikibase-item': snak.setTarget(pywikibot.ItemPage(self.repo, value)) return True elif snak.type == 'wikibase-property': snak.setTarget(pywikibot.PropertyPage(self.repo, value)) return True elif snak.type == 'quantity': match = self.quantityR.fullmatch(value) if match: amount, error, unit = match.groups() else: match = self.quantity_oldR.fullmatch(value) if match: amount, lower, upper, unit = match.groups() error = upper, lower # it *is* the other way around if match: if unit: unit = pywikibot.ItemPage(self.repo, 'Q' + unit) quantity = WbQuantity(amount, unit, error, site=self.repo) snak.setTarget(quantity) return True elif snak.type == 'time': iso, _, prec = value.rpartition('/') if iso: time = WbTime.fromTimestr(iso, precision=int(prec), site=self.repo) snak.setTarget(time) return True elif snak.type in ('string', 'external-id', 'url', 'math'): if value.startswith('"') and value.endswith('"'): snak.setTarget(value[1:-1]) return True elif snak.type == 'commonsMedia': if value.startswith('"') and value.endswith('"'): repo = self.repo.image_repository() snak.setTarget(pywikibot.FilePage(repo, value[1:-1])) return True #elif snak.type in ('geo-shape', 'tabular-data'): elif snak.type == 'monolingualtext': lang, _, text = value.partition(':') if text and text.startswith('"') and text.endswith('"'): monotext = WbMonolingualText(text[1:-1], lang) snak.setTarget(monotext) return True elif snak.type == 'globe-coordinate': match = self.globeR.fullmatch(value) if match: coord = Coordinate(*map(float, match.groups()), site=self.repo) snak.setTarget(coord) return True return False
def test_globe_coordinate(self): """Test a coordinate PropertyPage has the correct type.""" wikidata = self.get_repo() property_page = pywikibot.PropertyPage(wikidata, 'P625') self.assertEqual(property_page.type, 'globe-coordinate') self.assertEqual(property_page.getType(), 'globecoordinate') claim = pywikibot.Claim(wikidata, 'P625') self.assertEqual(claim.type, 'globe-coordinate') self.assertEqual(claim.getType(), 'globecoordinate')
def test_new_claim(self): """Test that PropertyPage.newClaim uses cached datatype.""" wikidata = self.get_repo() property_page = pywikibot.PropertyPage(wikidata, 'P625') property_page.get() claim = property_page.newClaim() self.assertEqual(claim.type, 'globe-coordinate') # Now verify that it isnt fetching the type from the property # data in the repo by setting the cache to the incorrect type # and checking that it is the cached value that is used. property_page._type = 'wikibase-item' claim = property_page.newClaim() self.assertEqual(claim.type, 'wikibase-item')
def get(self, wikibase_repo): if len(wiki_item_exists(wikibase_repo, "Wikidata QID")) > 0: self.itemIdentifier = str( wiki_item_exists(wikibase_repo, "Wikidata QID")[0]['id']) else: wikibase_item = pywikibot.PropertyPage(wikibase_repo, datatype='external-id') data = {} mylabels = {"en": "Wikidata QID"} mydescriptions = { "en": "Corresponding QID in Wikidata (do not change the label of this property otherwise you will break WikibaseSync)" } data['labels'] = mylabels data['descriptions'] = mydescriptions wikibase_item.editEntity( data, summary=u'Insert a property to have a wikidata identifier') self.itemIdentifier = str(wikibase_item.getID()) if len(wiki_item_exists(wikibase_repo, "Wikidata PID")) > 0: self.propertyIdentifier = str( wiki_item_exists(wikibase_repo, "Wikidata PID")[0]['id']) else: wikibase_item = pywikibot.PropertyPage(wikibase_repo, datatype='external-id') data = {} mylabels = {"en": "Wikidata PID"} mydescriptions = { "en": "id in wikidata of the corresponding properties (do not change this property otherwise you will break WikibaseSync)" } data['labels'] = mylabels data['descriptions'] = mydescriptions wikibase_item.editEntity( data, summary=u'Insert a property to have a wikidata identifier') self.propertyIdentifier = str(wikibase_item.getID())
def addWpAndMoe(title): title = title.replace('Item:', '') title = title.replace('Property:', '') print(title) data = {'claims': []} if title[0] == 'Q': myitem = pywikibot.ItemPage(datasite, title) elif title[0] == 'P': myitem = pywikibot.PropertyPage(datasite, title) else: print('\t Not Wikibase page') return myitem.get() label = myitem.labels['zh-tw'] print('\t', label) if 'P68' not in myitem.claims: targettitle = converttitle(zhsite, label) if targettitle: new_claim = pywikibot.page.Claim(datasite, 'P68') new_claim.setTarget(targettitle) print('\t Add P68', targettitle) data['claims'].append(new_claim.toJSON()) if 'P70' not in myitem.claims: targettitle = converttitle(moesite, label) if targettitle: new_claim = pywikibot.page.Claim(datasite, 'P70') new_claim.setTarget(targettitle) print('\t Add P70', targettitle) data['claims'].append(new_claim.toJSON()) if data['claims']: print('\t', data) session.post(API, data={ 'action': 'wbeditentity', 'format': 'json', 'id': title, 'data': json.dumps(data), 'summary': '自動新增對應維基頁面', 'token': csrftoken, 'bot': 1, }).json()
def getData(self, page): #get data from page & WD #return Person object sex = {'Q6581097': 'mężczyzna', 'Q6581072': 'kobieta'} obj = Person() obj.title = page.title() try: wd = pywikibot.ItemPage.fromPage(page) wdcontent = wd.get() obj.wditem = '[[:d:%s]]' % wd.title() obj.wdexists = True if self.getOption('labels'): pywikibot.output(wdcontent['claims'].keys()) except: pywikibot.output('WikiData page do not exists') obj.wdexists = False return (obj) wbs = pywikibot.Site('wikidata', 'wikidata') for pid, claims in wdcontent['claims'].items(): for claim in claims: trg = claim.getTarget() prp = pywikibot.PropertyPage(wbs, title=pid) prpcontent = prp.get() #pywikibot.output('Property content:%s' % self.getLabel(prp,['xhs','pl','sco','en'])) #pywikibot.output('Property:%s Value:%s' % (pid,trg)) if pid in ['P21', 'P569', 'P570', 'P106']: if self.getOption('labels'): pywikibot.output( 'P:%s, V:%s' % (self.getLabel(prp, ['pl', 'en']), trg)) if pid == 'P21': obj.sex = sex[trg.title()] if pid == 'P31': obj.instanceof = self.getLabel(trg, ['pl', 'en']) if pid == 'P569': obj.setDoB(trg) if pid == 'P570': obj.setDoD(trg) if pid == 'P106': lbl = self.getLabel(trg, ['pl', 'en']) if lbl: obj.occupation.append(lbl) return (obj)
def __init__(self, wdprop): """ Arguments: * wdprop - The Wikidata property to work on """ self.wdprop = wdprop self.repo = pywikibot.Site().data_repository() self.property = pywikibot.PropertyPage(self.repo, self.wdprop) self.nonHumans = self.getNonHumans(self.wdprop) self.humanProps = [ u'P19', # place of birth (P19) u'P21', # sex or gender (P21) u'P106', # occupation (P106) u'P569' # date of birth (P569) ]
def addWikidataUrl(title, targettitle): print(title) if title[0] == 'Q': myitem = pywikibot.ItemPage(datasite, title) url = 'https://www.wikidata.org/wiki/{}'.format(targettitle) elif title[0] == 'P': url = 'https://www.wikidata.org/wiki/Property:{}'.format(targettitle) myitem = pywikibot.PropertyPage(datasite, title) else: print('\t Not Wikibase page') return new_claim = pywikibot.page.Claim(datasite, 'P67') new_claim.setTarget(url) print('\t', new_claim) myitem.addClaim(new_claim, summary='設定維基數據網址')
def test_add_claim_with_qualifier(self): """Test adding a claim with a qualifier to an item and a property.""" testsite = self.get_repo() item = pywikibot.ItemPage(testsite, 'Q68') item.get() if 'P115' in item.claims: item.removeClaims(item.claims['P115']) claim = pywikibot.page.Claim(testsite, 'P115', datatype='wikibase-item') target = pywikibot.ItemPage(testsite, 'Q271') claim.setTarget(target) item.addClaim(claim) item.get(force=True) end_date = pywikibot.page.Claim(testsite, 'P88', is_qualifier=True) end_date.setTarget(pywikibot.WbTime(year=2012)) item.claims['P115'][0].addQualifier(end_date) # Testing all again but this time in properties item = pywikibot.PropertyPage(testsite, 'P115') item.get() if 'P115' in item.claims: to_remove = [] for claim in item.claims['P115']: to_remove.append({'id': claim.toJSON()['id'], 'remove': ''}) item.editEntity({'claims': to_remove}) claim = pywikibot.page.Claim(testsite, 'P115', datatype='wikibase-item') target = pywikibot.ItemPage(testsite, 'Q271') claim.setTarget(target) item.editEntity({'claims': [claim.toJSON()]}) item.get(force=True) end_date = pywikibot.page.Claim(testsite, 'P88', is_qualifier=True) end_date.setTarget(pywikibot.WbTime(year=2012)) item.claims['P115'][0].addQualifier(end_date)
def main(): properties = AllpagesPageGenerator(namespace=120) for property in properties: try: p = property.title().replace('Property:', '') # property id ppage = pywikibot.PropertyPage(repo, p) # property page tpage = pywikibot.Page(site, 'Property talk:' + p) # property talk page # if tpage.exists() and ppage.exists() # deactivated due to pywikibot bug if tpage.exists(): ppage.get() talkpage = re.sub(r'{{(q|Q)\|q?Q?(\d+)}}', r'Q\2', tpage.get()) talkpage = re.sub(r'{{(p|P)\|p?P?(\d+)}}', r'P\2', talkpage) wikicode = mwparserfromhell.parse(talkpage) templates = wikicode.filter_templates() report = u'' for t in templates: if t.name in types: try: report += oneConstraint( p, ppage.type, t) # create report for one constraint except Exception, e: logwrite(str(e) + '\n') if len(report) > 1: try: query = 'SELECT (count(DISTINCT(?item)) AS ?cnt) WHERE {{ {{ ?item wdt:{p} [] . }} UNION {{ ?ref pr:{p} [] . ?statement1 prov:wasDerivedFrom ?ref . ?item ?p1 ?statement1 }} UNION {{ ?statement1 pq:{p} [] . ?item ?p1 ?statement1 }} }}'.format( p=p) data, _ = requestquery(query) cnt = data['results']['bindings'][0]['cnt']['value'] except: cnt = '' report = header.format( time.strftime("%Y-%m-%d %H:%M (%Z)"), cnt) + report cpage = pywikibot.Page( site, 'Wikidata:Database reports/Constraint violations/' + p) cpage.put(report, comment='report update for [[Property:' + p + ']]', minorEdit=False) except Exception, e: logwrite(str(e) + '\n')
def test_edit_entity_new_property(self): """Test creating a new property using C{PropertyPage.editEntity}.""" testsite = self.get_repo() ts = str(time.time()) data = { 'labels': { 'en': { 'language': 'en', 'value': 'Pywikibot test new property', } }, 'descriptions': { 'en': { 'language': 'en', 'value': 'Pywikibot test new property - ' + ts, } } } prop = pywikibot.PropertyPage(testsite, datatype='string') prop.editEntity(data)
def main(): properties = AllpagesPageGenerator(namespace=120) for property in properties: try: p = property.title().replace('Property:', '') # property id ppage = pywikibot.PropertyPage(repo, p) # property page ppage.get() if 'P2302' not in ppage.claims: continue cpage = pywikibot.Page( site, 'Wikidata:Database reports/Constraint violations/' + p) if cpage.exists(): lastedit = cpage.getVersionHistory()[0] if (datetime.datetime.now() - lastedit[1]).days < 1: continue if lastedit[2] == 'KrBot2' and 'ERROR:' not in cpage.text: continue report = u'' for co in ppage.claims['P2302']: if co.getTarget().getID() in types: try: report += oneConstraint( p, ppage.type, co) # create report for one constraint except Exception, e: logwrite(str(e) + '\n') if len(report) > 1: try: query = 'SELECT (count(DISTINCT(?item)) AS ?cnt) WHERE {{ {{ ?item wdt:{p} [] . }} UNION {{ ?ref pr:{p} [] . ?statement1 prov:wasDerivedFrom ?ref . ?item ?p1 ?statement1 }} UNION {{ ?statement1 pq:{p} [] . ?item ?p1 ?statement1 }} }}'.format( p=p) data, _ = requestquery(query) cnt = data['results']['bindings'][0]['cnt']['value'] except: cnt = '' report = header.format(time.strftime("%Y-%m-%dT%H:%M:%SZ"), cnt) + report cpage.put(report, comment='report update for [[Property:' + p + ']]', minorEdit=False) except Exception, e: logwrite(str(e) + '\n')
def get(self, wikibase_repo): wikibase_item = pywikibot.PropertyPage(wikibase_repo, datatype='external-id') try: data = {} mylabels = {"en": "Wikidata QID"} mydescriptions = {"en": "Corresponding QID in Wikidata"} data['labels'] = mylabels data['descriptions'] = mydescriptions wikibase_item.editEntity( data, summary=u'Insert a property to have a wikidata identifier') self.itemIdentifier = str(wikibase_item.getID()) except (APIError, pywikibot.exceptions.OtherPageSaveError) as e: # this happens when a property with the same label already exists x = re.search(r'\[\[Property:.*\]\]', str(e)) if x: self.itemIdentifier = str( x.group(0).replace("[[Property:", "").split("|")[0]) else: print("This should not happen 1") try: data = {} mylabels = {"en": "Wikidata PID"} mydescriptions = { "en": "id in wikidata of the corresponding properties" } data['labels'] = mylabels data['descriptions'] = mydescriptions wikibase_item.editEntity( data, summary=u'Insert a property to have a wikidata identifier') self.propertyIdentifier = str(wikibase_item.getID()) except (APIError, pywikibot.exceptions.OtherPageSaveError) as e: # this happens when a property with the same label already exists x = re.search(r'\[\[Property:.*\]\]', str(e)) if x: self.propertyIdentifier = str( x.group(0).replace("[[Property:", "").split("|")[0]) else: print("This should not happen 2")
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'))