http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11236
Revision: 11236 Author: drtrigon Date: 2013-03-22 21:27:19 +0000 (Fri, 22 Mar 2013) Log Message: ----------- update; new adoptions to wikidata and framework changes/updates
Modified Paths: -------------- trunk/pywikipedia/subster.py trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/subster.py =================================================================== --- trunk/pywikipedia/subster.py 2013-03-22 07:12:16 UTC (rev 11235) +++ trunk/pywikipedia/subster.py 2013-03-22 21:27:19 UTC (rev 11236) @@ -225,22 +225,23 @@ for item in data: for element in datapage.searchentities(u'DrTrigonBot:%s' % item): dataoutpage = pywikibot.DataPage(self.site, element['id']) - dataoutpage = page.toggleTalkPage() + #dataoutpage = page.toggleTalkPage()
- ##dic = json.loads(dataoutpage.get()) - #dic = dataoutpage.getentities() - out = u'* ~~~~~ / [[%s]] / %s / %s' % (element['id'], item, data[item]) + pywikibot.output(u'%s <--- %s = %s' %\ + (dataoutpage.title(asLink=True), item, data[item]))
- pywikibot.output(u'%s <--- "%s"' % (dataoutpage.title(asLink=True), out)) - ## check for changes and then write/change/set values summary = u'Bot: update data because of configuration on %s.' % page.title(asLink=True) #if not self.WD_save(dataoutpage, dic[u'claims'], {u'p32': data}, summary): buf = dataoutpage.get() - if buf.strip().splitlines()[-1].split(u'/')[-1].strip() == data[item]: + propid = 217 # just a cheat to start with ... + claim = [ claim for claim in buf[u'claims'] if (claim['m'][1] == propid) ] + #if buf.strip().splitlines()[-1].split(u'/')[-1].strip() == data[item]: + if claim and (claim[0]['m'][3] == data[item]): pywikibot.output(u'NOTHING TO DO!') else: - dataoutpage.put(buf + u'\n' + out, comment=summary) + #dataoutpage.put(buf + u'\n' + out, comment=summary) + dataoutpage.editclaim(u'p%i' % propid, data[item]) else: # if changed, write! if (substed_content != content):
Modified: trunk/pywikipedia/wikipedia.py =================================================================== --- trunk/pywikipedia/wikipedia.py 2013-03-22 07:12:16 UTC (rev 11235) +++ trunk/pywikipedia/wikipedia.py 2013-03-22 21:27:19 UTC (rev 11236) @@ -4097,11 +4097,11 @@ Supports the same interface as Page, with the following added methods:
setitem : Setting item(s) on a page - setclaimvalue : Set the value of a Wikibase claim - createclaim : Create Wikibase claims + editclaim : Create and/or set the value of a Wikibase claim createitem : Create an item - getentity : Getting item(s) of a page + get : Getting item(s) of a page (like entity, ...) getentities : Get the data for multiple Wikibase entities + DEPRECATED: please use get() instead of getentities() searchentities : Search for entities
""" @@ -4339,6 +4339,7 @@ if data['success'] == u"1": return 302, response.msg, data['success'] return response.code, response.msg, data + def editclaim(self, WDproperty, value,raw_value=False, refs=None, comment=None, token=None, sysop=False,botflag=True): if isinstance(WDproperty,int): propertyID=WDproperty @@ -4358,9 +4359,11 @@ else: raise RuntimeError("Unknown property type: %s" % WDproperty) if not raw_value: - if isinstance(value,int): - pass - elif isinstance(value,basestring): + if isinstance(value, int): # for 'quantity' entity-type + value="{"entity-type":"item","numeric-id":%s}" % value + elif isinstance(value, unicode): # for 'string' entity-type + value = json.dumps(value) + elif isinstance(value, basestring): # for 'quantity' entity-type try: value=int(value) except ValueError: @@ -4373,9 +4376,10 @@ pass else: pass - else: + value="{"entity-type":"item","numeric-id":%s}" % value + else: # otherwise raise RuntimeError("Unknown property type: %s" % value) - value="{"entity-type":"item","numeric-id":%s}" % value + value="{"entity-type":"item","numeric-id":%s}" % value else: pass claims=self.get()['claims'] @@ -4413,7 +4417,7 @@ params['token'] = token else: params['token'] = self.site().getToken(sysop = sysop) - output(u"Changing %s" % self.title()) + output(u"Creating %s" % self.title()) data = query.GetData(params, self.site(), sysop=sysop) if 'error' in data: raise RuntimeError("API query error: %s" % data) @@ -4472,7 +4476,8 @@ raise RuntimeError("API query error: %s" % data) if u'warnings' in data: output(str(data[u'warnings'])) - def getentity(self,force=False, get_redirect=False, throttle=True, + + def _getentity(self,force=False, get_redirect=False, throttle=True, sysop=False, change_edit_time=True): """Returns items of a entity in a dictionary """ @@ -4581,6 +4586,7 @@ self._title = self._contents['entity'].title() return self._contents
+ @deprecate_arg("get", None) def getentities(self, sysop=False): """API module to get the data for multiple Wikibase entities. """ @@ -4591,7 +4597,7 @@ # retrying is done by query.GetData data = query.GetData(params, self.site(), sysop=sysop) entities = data['entities'][self.title().lower()] - debuginfo = data['debuginfo'] + #debuginfo = data['debuginfo']
if 'error' in data: raise RuntimeError("API query error: %s" % data) @@ -4635,7 +4641,7 @@ def get(self, *args, **kwargs): if not hasattr(self, '_contents'): if self._title is None: - self.getentity(*args, **kwargs) + self._getentity(*args, **kwargs) else: pagetext = super(DataPage, self).get(*args, **kwargs) self._contents = json.loads(pagetext)
pywikipedia-svn@lists.wikimedia.org