http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11372
Revision: 11372 Author: multichill Date: 2013-04-16 18:22:45 +0000 (Tue, 16 Apr 2013) Log Message: ----------- Split up and more python way of doing it.
Modified Paths: -------------- branches/rewrite/scripts/claimit.py
Modified: branches/rewrite/scripts/claimit.py =================================================================== --- branches/rewrite/scripts/claimit.py 2013-04-16 16:37:10 UTC (rev 11371) +++ branches/rewrite/scripts/claimit.py 2013-04-16 18:22:45 UTC (rev 11372) @@ -18,38 +18,46 @@ repo = pywikibot.Site().data_repository()
+def addClaims(page, claims): + ''' + The function will add the claims to the wikibase page + ''' + item = pywikibot.ItemPage.fromPage(page) + pywikibot.output('Processing %s' % page) + if not item.exists(): + pywikibot.output('%s doesn't have a wikidata item :(' % page) + #TODO FIXME: We should provide an option to create the page + return False + + for claim in claims: + pywikibot.output('Adding %s --> %s' % (claim.getID(), claim.getTarget().getID())) + item.addClaim(claim) + #TODO FIXME: We should add a source for each claim that is added + #TODO FIXME: We need to check that we aren't adding a duplicate + + def main(): gen = pagegenerators.GeneratorFactory() - claims = list() + commandline_claims = list() for arg in pywikibot.handleArgs(): if gen.handleArg(arg): continue - claims.append(arg) - if len(claims) % 2 != 0: + commandline_claims.append(arg) + if len(commandline_claims) % 2: raise ValueError # or something. - real_claims = list() - c = 0 - while c != len(claims): - claim = pywikibot.Claim(repo, claims[c]) - claim.setTarget(pywikibot.ItemPage(repo, claims[c+1])) - real_claims.append(claim) - c += 2 + claims = list()
+ for i in xrange (0, len(commandline_claims), 2): + claim = pywikibot.Claim(repo, commandline_claims[i]) + claim.setTarget(pywikibot.ItemPage(repo, commandline_claims[i+1])) + claims.append(claim) + + generator = gen.getCombinedGenerator()
- for page in generator: - item = pywikibot.ItemPage.fromPage(page) - pywikibot.output('Processing %s' % page) - if not item.exists(): - pywikibot.output('%s doesn't have a wikidata item :(' % page) - #TODO FIXME: We should provide an option to create the page - continue + if generator: + for page in generator: + addClaims(page, claims)
- for claim in real_claims: - pywikibot.output('Adding %s --> %s' % (claim.getID(), claim.getTarget().getID())) - item.addClaim(claim) - #TODO FIXME: We should add a source for each claim that is added - #TODO FIXME: We need to check that we aren't adding a duplicate - if __name__ == "__main__": - main() \ No newline at end of file + main()