Ejemplo n.º 1
0
def main():


    # site = pywikibot.Site('test', "wikidata")
    site = pywikibot.Site('wikidata', "wikidata")
    repo = site.data_repository()
    # item = pywikibot.ItemPage(repo, "Q121006")  # test:
    item = pywikibot.ItemPage(repo, 'Q4115189')  # Wikidata Sandbox

    # Add claim
    prop = 'P1082'
    claim = pywikibot.Claim(repo, prop)  # , rank='preferred')
    #value = to_value[insee_code]
    value = 6
    quantity = pywikibot.WbQuantity(value, site=site)
    #print(type(quantity))
    claim.setTarget(quantity)
    # TODO: Remove if population is not the last one
    # claim.setRank('preferred')
    item.addClaim(claim, summary="Adding population")
    #claim.changeRank('preferred')

    # Add qualifier
    point_in_time = 'P585'
    # TODO: REMOVE
    # point_in_time = 'P66'  # TEST
    qualifier = pywikibot.Claim(repo, point_in_time)
    # TODO: date
    date = pywikibot.WbTime(year=2015)  # , month=3, day=20)
    qualifier.setTarget(date)
    #claim.addQualifier(qualifier, summary=u'Adding a qualifier.')
    qualifier.isQualifier = True
    # set_qualifier
    claim.qualifiers[qualifier.getID()] = [qualifier]


    # Add source
    statedin = pywikibot.Claim(repo, 'P248')
    insee = pywikibot.ItemPage(repo, 'Q156616')
    statedin.setTarget(insee)
    statedin.isReference = True

    title = pywikibot.Claim(repo, 'P1476')
    text = pywikibot.WbMonolingualText("Recensement de la population 2015", 'fr')  # monolingual text
    title.setTarget(text)
    title.isReference = True

    publication_date = pywikibot.Claim(repo, 'P577')
    date = pywikibot.WbTime(year=2017, month=12, day=27)
    publication_date.setTarget(date)
    publication_date.isReference = True

    # claim.addSources([statedin, publication_date], summary="Adding sources.")
    # claim.addSources([statedin], summary="Adding sources.")
    #claim.addSources([statedin, title, publication_date], summary="Adding sources.")

    # set_source
    claims = [statedin, title, publication_date]
    source = defaultdict(list)
    for c in claims:
        source[c.getID()].append(c)
    claim.sources.append(source)


    claim.changeRank('preferred', summary="Add population 2015")

    """
Ejemplo n.º 2
0
                try:
                    target = pywikibot.WbMonolingualText(text=text,
                                                         language=language)
                except Exception, e:
                    msg = str(e).replace("\n", " ").replace("\r", " ")
                    print("WbMonolingualText ERROR IN " + entity_id + " FOR " +
                          value + ": " + msg)
                    pass

            elif datatype == 'string' or datatype == 'url':
                target = value.strip()

            elif datatype == 'quantity':
                try:
                    target = pywikibot.WbQuantity(site=repo,
                                                  amount=value['amount'],
                                                  unit=value['unit'])
                except Exception, e:
                    msg = str(e).replace("\n", " ").replace("\r", " ")
                    print("WbQuantity ERROR IN " + entity_id + " FOR " +
                          str(value['amount']) + ": " + msg)
                    pass

            elif datatype == "external-id":
                try:
                    target = value.strip()
                except Exception, e:
                    msg = str(e).replace("\n", " ").replace("\r", " ")
                    print("getTarget external-id ERROR IN " + entity_id +
                          " FOR " + str(value) + ": " + msg)
                    pass
Ejemplo n.º 3
0
    item_id = Basel_mapping[row['Wohnviertel_id']]
    item = pywikibot.ItemPage(repo, item_id)
    item.get()
    #print(item.claims)

    #year_list = range(2018, 2019)
    #for year in year_list:
    population_claim = existing_claim_from_year(item, year)
    #print(population_claim)
    if (population_claim is None):
        print(
            str(year) + ": " + row['Wohnviertel_id'] + " -> " + item_id +
            ": " + str(population_value) + " Einwohner")
        population_claim = pywikibot.Claim(repo, population_prop_id)
        population_claim.setTarget(
            pywikibot.WbQuantity(amount=population_value, site=site))
        item.addClaim(population_claim)

        timeQualifier = pywikibot.Claim(repo, time_prop_id)
        yearObj = pywikibot.WbTime(year=year, month=12, day=31)
        timeQualifier.setTarget(yearObj)
        population_claim.addQualifier(timeQualifier)

        source = pywikibot.Claim(repo, url_prop_id)
        source.setTarget(source_url)
        retrieved = pywikibot.Claim(repo, retrieved_prop_id)
        today = datetime.datetime.today()
        retrieved_date = pywikibot.WbTime(year=today.year,
                                          month=today.month,
                                          day=today.day)
        retrieved.setTarget(retrieved_date)
Ejemplo n.º 4
0
    def testWikibase(self):
        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('HomePage', 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})

        # test WbTime
        t = pywikibot.WbTime(site=repo, year=2010, hour=12, minute=43)
        self.assertEqual(t.toTimestr(), '+00000002010-01-01T12:43:00Z')
        self.assertRaises(ValueError, pywikibot.WbTime, site=repo, precision=15)
        self.assertRaises(ValueError, pywikibot.WbTime, site=repo, 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', })
        q = pywikibot.WbQuantity(amount=0.044405586)
        q_dict = {'amount': 0.044405586, 'lowerBound': 0.044405586,
                  'upperBound': 0.044405586, 'unit': '1', }
        self.assertEqual(q.toWikibase(), q_dict)
        # test other WbQuantity methods
        self.assertEqual("%s" % q,
                         '{\n'
                         '    "amount": %(val)r,\n'
                         '    "lowerBound": %(val)r,\n'
                         '    "unit": "1",\n'
                         '    "upperBound": %(val)r\n'
                         '}' % {'val': 0.044405586})
        self.assertEqual("%r" % q,
                         "WbQuantity(amount=%(val)s, "
                         "upperBound=%(val)s, lowerBound=%(val)s, "
                         "unit=1)" % {'val': 0.044405586})
        self.assertEqual(q, q)

        # test WbQuantity.fromWikibase() instantiating
        q = pywikibot.WbQuantity.fromWikibase({u'amount': u'+0.0229',
                                               u'lowerBound': u'0',
                                               u'upperBound': u'1',
                                               u'unit': u'1'})
        self.assertEqual(q.toWikibase(),
                         {'amount': 0.0229, 'lowerBound': 0, 'upperBound': 1,
                          'unit': '1', })

        # test WbQuantity error handling
        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(self.mainpage),
                         pywikibot.ItemPage(repo, 'q5296'))
Ejemplo n.º 5
0
        upToDateClaims = []
        count = int(split[1])
        for claim in item.claims.get('P1082', []):
            if claim.getRank() == 'preferred':
                claim.setRank('normal')
                upToDateClaims.append(claim)
            if (claim.qualifiers.get('P585')
                    and claim.qualifiers['P585'][0].target_equals(date)):
                hasNewClaim = True
                break

        if hasNewClaim is True:
            continue

        newClaim = pywikibot.Claim(repo, 'P1082')
        newClaim.setTarget(pywikibot.WbQuantity(count, site=repo))
        newClaim.setRank('preferred')

        newClaim_date = pywikibot.Claim(repo, 'P585', is_qualifier=True)
        newClaim_date.setTarget(date)
        newClaim.addQualifier(newClaim_date)

        newClaim_criter = pywikibot.Claim(repo, 'P1013', is_qualifier=True)
        newClaim_criter.setTarget(pywikibot.ItemPage(repo, 'Q2641256'))
        newClaim.addQualifier(newClaim_criter)

        newClaim_men = pywikibot.Claim(repo, 'P1540', is_qualifier=True)
        newClaim_men.setTarget(pywikibot.WbQuantity(int(split[2]), site=repo))
        newClaim.addQualifier(newClaim_men)

        newClaim_women = pywikibot.Claim(repo, 'P1539', is_qualifier=True)
Ejemplo n.º 6
0
                    claim.setRank('normal')
                    item.editEntity(
                        {'claims': [claim.toJSON()]},
                        summary=
                        f'{test_edit}Changing ranked to normal for population (P1082) statement.'
                    )

        if has_pop_for_year:
            continue

        # Add a new claim as "preferred"

        # CLAIM
        quantity_claim = pywikibot.Claim(repo, u'P1082')
        wb_quant = pywikibot.WbQuantity(
            data_mun[municipality_num],
            site=repo)  # adding population without unit
        quantity_claim.setTarget(wb_quant)
        quantity_claim.setRank("preferred")

        item.addClaim(
            quantity_claim,
            summary=
            f'{test_edit}Adding population (P1082) for January 1, {year_adding}.'
        )

        # QUALIFIER
        point_it = pywikibot.Claim(
            repo, u'P585', is_qualifier=True
        )  # point in time (P585). Data type: Point in time
        dateCre = pywikibot.WbTime(site=repo, year=year_adding, month=1,
Ejemplo n.º 7
0
    def test_format_claim_value_unitless_quantity(self):
        self.set_claim_type('quantity')
        expected_data = pywikibot.WbQuantity(12.34, site=self.mock_site)

        data = format_claim_value(self.mock_claim, '12.34')
        self.assertEqual(data, expected_data)
Ejemplo n.º 8
0
    def run(self):
        """
        Starts the robot.
        """
            
        for metadata in self.generator:
            # Buh, for this one I know for sure it's in there
            
            #print metadata[u'id']
            #print metadata[u'url']

            # Do some url magic so that all url fields are always filled
            if not metadata.get('refurl'):
                metadata['refurl']=metadata['url']
            if not metadata.get('idrefurl'):
                metadata['idrefurl']=metadata['refurl']
            if not metadata.get('describedbyurl'):
                metadata['describedbyurl']=metadata['url']
            if not metadata.get('imagesourceurl'):
                metadata['imagesourceurl']=metadata['url']

            artworkItem = None
            newclaims = []
            if metadata[u'id'] in self.artworkIds:
                artworkItemTitle = self.artworkIds.get(metadata[u'id'])
                print (artworkItemTitle)
                artworkItem = pywikibot.ItemPage(self.repo, title=artworkItemTitle)

            elif self.create:
                #Break for now
                #print u'Let us create stuff'
                #continue
                #print u'WTFTFTFTFT???'
                
                #print 'bla'


                data = {'labels': {},
                        'descriptions': {},
                        }

                # loop over stuff
                if metadata.get('title'):
                    for lang, label in metadata['title'].items():
                        data['labels'][lang] = {'language': lang, 'value': label}

                if metadata.get('description'):
                    for lang, description in metadata['description'].items():
                        data['descriptions'][lang] = {'language': lang, 'value': description}
                
                identification = {}
                summary = u'Creating new item with data from %s ' % (metadata[u'url'],)
                pywikibot.output(summary)
                try:
                    result = self.repo.editEntity(identification, data, summary=summary)
                except pywikibot.data.api.APIError:
                    # TODO: Check if this is pywikibot.OtherPageSaveError too
                    # We got ourselves a duplicate label and description, let's correct that by adding collection and the id
                    pywikibot.output(u'Oops, already had that one. Trying again')
                    for lang, description in metadata['description'].items():
                        data['descriptions'][lang] = {'language': lang, 'value': u'%s (%s %s)' % (description, metadata['collectionshort'], metadata['id'],) }
                    try:
                        result = self.repo.editEntity(identification, data, summary=summary)
                    except pywikibot.data.api.APIError:
                        pywikibot.output(u'Oops, retry also failed. Skipping this one.')
                        # Just skip this one
                        continue
                    pass

                artworkItemTitle = result.get(u'entity').get('id')

                # Make a backup to the Wayback Machine when we have to wait anyway
                self.doWaybackup(metadata)

                # Wikidata is sometimes lagging. Wait for additional 5 seconds before trying to actually use the item
                time.sleep(5)
                
                artworkItem = pywikibot.ItemPage(self.repo, title=artworkItemTitle)

                # Add to self.artworkIds so that we don't create dupes
                self.artworkIds[metadata[u'id']]=artworkItemTitle

                # Add the id to the item so we can get back to it later
                newclaim = pywikibot.Claim(self.repo, self.idProperty)
                newclaim.setTarget(metadata[u'id'])
                pywikibot.output('Adding new id claim to %s' % artworkItem)
                artworkItem.addClaim(newclaim)

                self.addReference(artworkItem, newclaim, metadata[u'idrefurl'])
                
                newqualifier = pywikibot.Claim(self.repo, u'P195') #Add collection, isQualifier=True
                newqualifier.setTarget(self.collectionitem)
                pywikibot.output('Adding new qualifier claim to %s' % artworkItem)
                newclaim.addQualifier(newqualifier)

                collectionclaim = pywikibot.Claim(self.repo, u'P195')
                collectionclaim.setTarget(self.collectionitem)
                pywikibot.output('Adding collection claim to %s' % artworkItem)
                artworkItem.addClaim(collectionclaim)

                # Add the date they got it as a qualifier to the collection
                if metadata.get(u'acquisitiondate'):
                    if type(metadata[u'acquisitiondate']) is int or (len(metadata[u'acquisitiondate'])==4 and \
                                                                   metadata[u'acquisitiondate'].isnumeric()): # It's a year
                        acdate = pywikibot.WbTime(year=metadata[u'acquisitiondate'])
                        colqualifier = pywikibot.Claim(self.repo, u'P580')
                        colqualifier.setTarget(acdate)
                        pywikibot.output('Adding new acquisition date qualifier claim to collection on %s' % artworkItem)
                        collectionclaim.addQualifier(colqualifier)
                # FIXME: Still have to rewrite this part
                '''
                if metadata.get(u'acquisitiondate'):
                    colqualifier = pywikibot.Claim(self.repo, u'P580')
                    acdate = None
                    if len(painting[u'acquisitiondate'])==4 and painting[u'acquisitiondate'].isnumeric(): # It's a year
                        acdate = pywikibot.WbTime(year=painting[u'acquisitiondate'])
                    elif len(painting[u'acquisitiondate'].split(u'-', 2))==3:
                        (acday, acmonth, acyear) = painting[u'acquisitiondate'].split(u'-', 2)
                        acdate = pywikibot.WbTime(year=int(acyear), month=int(acmonth), day=int(acday))
                    if acdate:
                        colqualifier.setTarget(acdate)

                '''
                
                self.addReference(artworkItem, collectionclaim, metadata[u'refurl'])

                # For the meta collections the option to add extra inventory number and extra collections
                if metadata.get(u'extracollectionqid'):
                    extracollectionitem = pywikibot.ItemPage(self.repo, metadata.get(u'extracollectionqid'))
                    collectionclaim = pywikibot.Claim(self.repo, u'P195')
                    collectionclaim.setTarget(extracollectionitem)
                    pywikibot.output('Adding collection claim to %s' % artworkItem)
                    artworkItem.addClaim(collectionclaim)

                    if metadata.get(u'extraid'):
                        newclaim = pywikibot.Claim(self.repo, self.idProperty)
                        newclaim.setTarget(metadata[u'extraid'])
                        pywikibot.output('Adding extra new id claim to %s' % artworkItem)
                        artworkItem.addClaim(newclaim)

                        self.addReference(artworkItem, newclaim, metadata[u'idrefurl'])

                        newqualifier = pywikibot.Claim(self.repo, u'P195') #Add collection, isQualifier=True
                        newqualifier.setTarget(extracollectionitem)
                        pywikibot.output('Adding new qualifier claim to %s' % artworkItem)
                        newclaim.addQualifier(newqualifier)

                # And in some cases we need this one
                if metadata.get(u'extracollectionqid2'):
                    extracollectionitem = pywikibot.ItemPage(self.repo, metadata.get(u'extracollectionqid2'))
                    collectionclaim = pywikibot.Claim(self.repo, u'P195')
                    collectionclaim.setTarget(extracollectionitem)
                    pywikibot.output('Adding collection claim to %s' % artworkItem)
                    artworkItem.addClaim(collectionclaim)

            
            if artworkItem and artworkItem.exists():
                metadata['wikidata'] = artworkItem.title()

                data = artworkItem.get()
                claims = data.get('claims')

                # Add missing labels
                # FIXME: Move to a function
                # FIXME Do something with aliases too
                labels = data.get('labels')
                if metadata.get('title'):
                    labelschanged = False
                    for lang, label in metadata['title'].items():
                        if lang not in labels:
                            labels[lang] = label
                            labelschanged = True
                    if labelschanged:
                        summary = u'Adding missing label(s) from %s' % (metadata.get(u'refurl'),)
                        try:
                            artworkItem.editLabels(labels, summary=summary)
                        except pywikibot.OtherPageSaveError:
                            # Just skip it for no
                            pywikibot.output(u'Oops, already had that label/description combination. Skipping')
                            pass

                # Add missing descriptions
                # FIXME Move to a function
                descriptions = copy.deepcopy(data.get('descriptions'))
                if metadata.get('description'):
                    descriptionschanged = False
                    for lang, description in metadata['description'].items():
                        if lang not in descriptions:
                            descriptions[lang] = description
                            descriptionschanged = True
                    if descriptionschanged:
                        summary = u'Adding missing description(s) from %s' % (metadata.get(u'refurl'),)
                        try:
                            artworkItem.editDescriptions(descriptions, summary=summary)
                        except pywikibot.exceptions.OtherPageSaveError: # pywikibot.data.api.APIError:
                            # We got ourselves a duplicate label and description, let's correct that by adding collection and the id
                            descriptions = copy.deepcopy(data.get('descriptions'))
                            pywikibot.output(u'Oops, already had that label/description combination. Trying again')
                            for lang, description in metadata['description'].items():
                                if lang not in descriptions:
                                    descriptions[lang] = u'%s (%s %s)' % (description,
                                                                             metadata['collectionshort'],
                                                                             metadata['id'],)
                            artworkItem.editDescriptions(descriptions, summary=summary)
                            pass
                #print claims

                # instance of
                self.addItemStatement(artworkItem, u'P31', metadata.get(u'instanceofqid'), metadata.get(u'refurl'))

                # location
                self.addItemStatement(artworkItem, u'P276', metadata.get(u'locationqid'), metadata.get(u'refurl'))

                # creator
                self.addItemStatement(artworkItem, u'P170', metadata.get(u'creatorqid'), metadata.get(u'refurl'))                

                # Inception
                self.addInception(artworkItem, metadata)

                # genre
                self.addItemStatement(artworkItem, u'P136', metadata.get(u'genreqid'), metadata.get(u'refurl'))

                # Try to add the acquisitiondate to the existing collection claim
                # TODO: Move to function and also work with multiple collection claims
                if u'P195' in claims:
                    if len(claims.get(u'P195'))==1 and metadata.get(u'acquisitiondate'):
                        collectionclaim = claims.get(u'P195')[0]
                        # Would like to use collectionclaim.has_qualifier(u'P580')
                        if collectionclaim.getTarget()==self.collectionitem and not collectionclaim.qualifiers.get(u'P580'):
                            dateregex = u'^(\d\d\d\d)-(\d\d)-(\d\d)'
                            datematch = re.match(dateregex, str(metadata[u'acquisitiondate']))
                            acdate = None
                            if type(metadata[u'acquisitiondate']) is int or (len(metadata[u'acquisitiondate'])==4 and \
                                    metadata[u'acquisitiondate'].isnumeric()): # It's a year
                                acdate = pywikibot.WbTime(year=metadata[u'acquisitiondate'])
                            elif datematch:
                                #print metadata[u'acquisitiondate']
                                acdate = pywikibot.WbTime(year=int(datematch.group(1)),
                                                          month=int(datematch.group(2)),
                                                          day=int(datematch.group(3)))
                            else:
                                try:
                                    acdate = pywikibot.WbTime.fromTimestr(metadata[u'acquisitiondate'])
                                    # Pff, precision is t0o high. Hack to fix this
                                    if acdate.precision > 11:
                                        acdate.precision=11
                                except ValueError:
                                    pywikibot.output(u'Can not parse %s' % metadata[u'acquisitiondate'])
                            if acdate:
                                colqualifier = pywikibot.Claim(self.repo, u'P580')
                                colqualifier.setTarget(acdate)
                                pywikibot.output('Update collection claim with start time on %s' % artworkItem)
                                collectionclaim.addQualifier(colqualifier)
                                # This might give multiple similar references
                                #self.addReference(artworkItem, collectionclaim, metadata[u'refurl'])

                    # Try to add the extra collection
                    if metadata.get(u'extracollectionqid'):
                        foundExtraCollection = False
                        extracollectionitem = pywikibot.ItemPage(self.repo, metadata.get(u'extracollectionqid'))
                        for collectionclaim in claims.get(u'P195'):
                            if collectionclaim.getTarget()==extracollectionitem:
                                foundExtraCollection = True
                        if not foundExtraCollection:
                            newclaim = pywikibot.Claim(self.repo, u'P195')
                            newclaim.setTarget(extracollectionitem)
                            pywikibot.output('Adding extra collection claim to %s' % artworkItem)
                            artworkItem.addClaim(newclaim)
                            self.addReference(artworkItem, newclaim, metadata[u'refurl'])

                # material used
                # FIXME: This does not scale at all.
                if u'P186' not in claims and metadata.get(u'medium'):
                    if metadata.get(u'medium') == u'oil on canvas':
                        oil_paint = pywikibot.ItemPage(self.repo, u'Q296955')
                        canvas = pywikibot.ItemPage(self.repo, u'Q4259259')
                        painting_surface = pywikibot.ItemPage(self.repo, u'Q861259')
                        
                        newclaim = pywikibot.Claim(self.repo, u'P186')
                        newclaim.setTarget(oil_paint)
                        pywikibot.output('Adding new oil paint claim to %s' % artworkItem)
                        artworkItem.addClaim(newclaim)

                        self.addReference(artworkItem, newclaim, metadata[u'refurl'])

                        newclaim = pywikibot.Claim(self.repo, u'P186')
                        newclaim.setTarget(canvas)
                        pywikibot.output('Adding new canvas claim to %s' % artworkItem)
                        artworkItem.addClaim(newclaim)

                        newqualifier = pywikibot.Claim(self.repo, u'P518') #Applies to part
                        newqualifier.setTarget(painting_surface)
                        pywikibot.output('Adding new qualifier claim to %s' % artworkItem)
                        newclaim.addQualifier(newqualifier)

                        self.addReference(artworkItem, newclaim, metadata[u'refurl'])

                # Height in centimetres. Expect something that can be converted to a Decimal with . and not ,
                if u'P2048' not in claims and metadata.get(u'heightcm'):
                    newheight = pywikibot.WbQuantity(amount=metadata.get(u'heightcm'),
                                                     unit=u'http://www.wikidata.org/entity/Q174728',
                                                     site=self.repo)
                    newclaim = pywikibot.Claim(self.repo, u'P2048')
                    newclaim.setTarget(newheight)
                    pywikibot.output('Adding height in cm claim to %s' % artworkItem)
                    artworkItem.addClaim(newclaim)

                    self.addReference(artworkItem, newclaim, metadata[u'refurl'])

                # Width in centimetres. Expect something that can be converted to a Decimal with . and not ,
                if u'P2049' not in claims and metadata.get(u'widthcm'):
                    newwidth = pywikibot.WbQuantity(amount=metadata.get(u'widthcm'),
                                                    unit=u'http://www.wikidata.org/entity/Q174728',
                                                    site=self.repo)
                    newclaim = pywikibot.Claim(self.repo, u'P2049')
                    newclaim.setTarget(newwidth)
                    pywikibot.output('Adding width in cm claim to %s' % artworkItem)
                    artworkItem.addClaim(newclaim)

                    self.addReference(artworkItem, newclaim, metadata[u'refurl'])

                # Depth (or thickness) in centimetres.
                # Expect something that can be converted to a Decimal with . and not ,
                # Some museums provide this, but not a lot
                if u'P2610' not in claims and metadata.get(u'depthcm'):
                    newdepth = pywikibot.WbQuantity(amount=metadata.get(u'depthcm'),
                                                    unit=u'http://www.wikidata.org/entity/Q174728',
                                                    site=self.repo)
                    newclaim = pywikibot.Claim(self.repo, u'P2610')
                    newclaim.setTarget(newdepth)
                    pywikibot.output('Adding depth in cm claim to %s' % artworkItem)
                    artworkItem.addClaim(newclaim)

                    self.addReference(artworkItem, newclaim, metadata[u'refurl'])

                self.addImageSuggestion(artworkItem, metadata)

                # Quite a few collections have custom id's these days.
                if metadata.get(u'artworkidpid'):
                    if metadata.get(u'artworkidpid') not in claims:
                        newclaim = pywikibot.Claim(self.repo, metadata.get(u'artworkidpid') )
                        newclaim.setTarget(metadata[u'artworkid'])
                        pywikibot.output('Adding artwork id claim to %s' % artworkItem)
                        artworkItem.addClaim(newclaim)
                # Described at url
                else:
                    if u'P973' not in claims:
                        newclaim = pywikibot.Claim(self.repo, u'P973')
                        newclaim.setTarget(metadata[u'describedbyurl'])
                        pywikibot.output('Adding described at claim to %s' % artworkItem)
                        artworkItem.addClaim(newclaim)
                    else:
                        foundurl = False
                        for claim in claims.get(u'P973'):
                            if claim.getTarget()==metadata[u'describedbyurl']:
                                foundurl=True
                        if not foundurl:
                            newclaim = pywikibot.Claim(self.repo, u'P973')
                            newclaim.setTarget(metadata[u'describedbyurl'])
                            pywikibot.output('Adding additional described at claim to %s' % artworkItem)
                            artworkItem.addClaim(newclaim)

                # iiif manifest url
                if u'P6108' not in claims and metadata.get(u'iiifmanifesturl'):
                    newclaim = pywikibot.Claim(self.repo, u'P6108')
                    newclaim.setTarget(metadata[u'iiifmanifesturl'])
                    pywikibot.output('Adding IIIF manifest url claim to %s' % artworkItem)
                    artworkItem.addClaim(newclaim)
                    self.addReference(artworkItem, newclaim, metadata[u'refurl'])
Ejemplo n.º 9
0
def addPopData(repo, source_url, name_lt, pop_year, pop_count):

    statoffice_wd = 'Q12663462'  # LT stat office

    #population record date
    pop_day = 1
    pop_mon = 1

    #data access date
    now = datetime.datetime.now()
    access_day = now.day
    access_mon = now.month
    access_year = now.year

    logging.info("Checking ... %s" % (name_lt))

    sparql = """PREFIX schema: <http://schema.org/>
    SELECT DISTINCT ?item ?LabelEN ?page_titleLT ?itemLabel WHERE {
    ?item wdt:P17 wd:Q37.
    ?item wdt:P31 ?sub1 . ?sub1 (wdt:P279)* wd:Q486972 .
    ?article schema:about ?item.
    ?article schema:isPartOf <https://lt.wikipedia.org/>.
    ?article schema:name ?page_titleLT.
    filter(STR(?page_titleLT) = '%s')
    }
    LIMIT 1""" % (name_lt, )
    wd_pages = pagegenerators.WikidataSPARQLPageGenerator(sparql,
                                                          site=wikidata)
    wd_pages = list(wd_pages)

    wd_count = 0
    for wd_page in wd_pages:

        if wd_page.exists():
            wd_count += 1
            dictionary = wd_page.get()

            if not existingClaimFromYear(wd_page, pop_year, pop_mon, pop_day):
                time.sleep(10)

                population_claim = pywikibot.Claim(repo, 'P1082')
                population_claim.setTarget(
                    pywikibot.WbQuantity(amount=pop_count,
                                         site=repo))  #, error=1
                pywikibot.output(
                    'Adding %s --> %s' %
                    (population_claim.getID(), population_claim.getTarget()))
                wd_page.addClaim(population_claim)

                #time qualifier
                qualifier = pywikibot.Claim(repo, 'P585')
                pop_date = pywikibot.WbTime(year=pop_year,
                                            month=pop_mon,
                                            day=pop_day,
                                            precision='day')
                qualifier.setTarget(pop_date)
                population_claim.addQualifier(qualifier)

                #method qualifier       "demographic balance"
                qualifier = pywikibot.Claim(repo, 'P459')
                method = pywikibot.ItemPage(repo, 'Q15911027')
                qualifier.setTarget(method)
                population_claim.addQualifier(qualifier)

                #source as wiki page:
                sourceWiki = pywikibot.Claim(repo, 'P248')
                sourceWiki.setTarget(pywikibot.ItemPage(repo, statoffice_wd))

                #url as source
                source = pywikibot.Claim(repo, 'P854')
                source.setTarget(source_url)

                #accessed
                accessed = pywikibot.Claim(repo, 'P813')
                accessed_date = pywikibot.WbTime(year=access_year,
                                                 month=access_mon,
                                                 day=access_day,
                                                 precision='day')
                accessed.setTarget(accessed_date)

                population_claim.addSources([sourceWiki, source, accessed])

            else:
                logging.info("Population claim already exists on %s" %
                             (wd_page.title()))

        else:
            logging.warning('[[%s]]: no data page in Wikidata' %
                            (wd_page.title()))

    if (wd_count == 0):
        logging.warning('No Wikidata match found for ' % (name_lt))