Ejemplo n.º 1
0
 def procesPage(self, page):
     """Process a single page/item."""
     item = pywikibot.DataPage(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
     else:
         pagetext = page.get()
         pagetext = pywikibot.removeDisabledParts(pagetext)
         templates = pywikibot.extract_templates_and_params(pagetext)
         for (template, fielddict) in templates:
             # We found the template we were looking for
             if template.replace(u'_', u' ') == self.templateTitle:
                 for field, value in fielddict.items():
                     # This field contains something useful for us
                     if field in self.fields:
                         # Check if the property isn't already set
                         claim = self.fields[field]
                         if claim in item.get().get('claims'):
                             pywikibot.output(
                                 u'A claim for %s already exists. Skipping'
                                 % (claim, ))
                             # TODO FIXME: This is a very crude way of dupe
                             # checking
                         else:
                             # Try to extract a valid page
                             match = re.search(
                                 re.compile(
                                     r'\[\[(?P<title>[^\]|[#<>{}]*)(\|.*?)?\]\]'
                                 ), value)
                             if match:
                                 try:
                                     link = match.group(1)
                                     linkedPage = pywikibot.Page(
                                         self.site, link)
                                     if linkedPage.isRedirectPage():
                                         linkedPage = linkedPage.getRedirectTarget(
                                         )
                                     linkedItem = pywikibot.DataPage(
                                         linkedPage)
                                     pywikibot.output(
                                         'Adding %s --> %s' %
                                         (claim, linkedItem.getID()))
                                     refs = self.setSource(
                                         self.site().language())
                                     if refs:
                                         item.editclaim(str(claim),
                                                        linkedItem.getID(),
                                                        refs=set(refs))
                                     else:
                                         item.editclaim(
                                             str(claim), linkedItem.getID())
                                 except pywikibot.NoPage:
                                     pywikibot.output(
                                         "[[%s]] doesn't exist so I can't link to it"
                                         % linkedItem.title())
Ejemplo n.º 2
0
def ClaimFinder(page_title, claim_num):
    fa_result = False
    fa_result_more = []
    en_wdata = pywikibot.DataPage(pywikibot.Page(faSite, page_title))
    try:
        items = en_wdata.get()
    except:
        return False
    if items['claims']:
        case = items['claims']
        for i in case:
            if i['m'][1] == claim_num:
                fa_result = data2fa(i[u'm'][3][u'numeric-id'])
                break
Ejemplo n.º 3
0
def data2fa(number, strict=False):
    data = pywikibot.DataPage(int(number))
    try:
        items = data.get()
    except:
        return ""
    if isinstance(items['links'], list):
        items['links'] = {}
    if items['links'].has_key('fawiki'):
        return items['links']['fawiki']['name']
    if strict:
        return ""
    if items['label'].has_key('fa'):
        return items['label']['fa']
    try:
        return items['label']['en']
    except:
        return ""
Ejemplo n.º 4
0
def dataLoad(wikiId):
    page = pywikibot.Page(site, wikiId)
    data = pywikibot.DataPage(page)
    return data