Beispiel #1
0
def replaceGenerator():
    query = u"""SELECT ?item ?oldurl WHERE {
  ?item wdt:P195 wd:Q671384 .
  ?item wdt:P31 wd:Q3305213 .
  ?item wdt:P973 ?oldurl .
  FILTER(REGEX(STR(?oldurl), "http://www.smk.dk/en/explore-the-art/search-smk/#/detail/.+"))
  } LIMIT 7000"""
    sq = pywikibot.data.sparql.SparqlQuery()
    queryresult = sq.select(query)

    repo = pywikibot.Site().data_repository()

    for resultitem in queryresult:
        qid = resultitem.get('item').replace(
            u'http://www.wikidata.org/entity/', u'')
        item = pywikibot.ItemPage(repo, title=qid)
        wrongurl = resultitem.get('oldurl')
        righturl = resultitem.get('oldurl').replace(
            u'http://www.smk.dk/en/explore-the-art/search-smk/#/detail/',
            u'http://collection.smk.dk/#/en/detail/')
        yield (item, wrongurl, righturl)
Beispiel #2
0
def setSource(property, pfield, pvalue):
    '''ADD source to wikidata claim
    
     Arguments:
     -Property : property id 
     -pfield   : property name
     -pvalue   : property value
    
    '''

    mysite = pywikibot.Site("wikidata", "wikidata")
    repo = mysite.data_repository()

    sourceField = WItem.Item.property_list_sources[pfield]
    sourceItem = pywikibot.ItemPage(repo, sourceField)
    source = pywikibot.Claim(repo, unicode('P143'))  #imported from
    source.setTarget(sourceItem)

    claim1 = pywikibot.Claim(repo, unicode(property))
    claim1.setTarget(pvalue)
    claim1.addSource(source)
Beispiel #3
0
def WikidataQueryPageGenerator(query, site=None):
    """Generate pages that result from the given WikidataQuery.

    @param query: the WikidataQuery query string.
    @param site: Site for generator results.
    @type site: L{pywikibot.site.BaseSite}

    """
    if site is None:
        site = pywikibot.Site()
    repo = site.data_repository()

    wd_queryset = wdquery.QuerySet(query)

    wd_query = wdquery.WikidataQuery(cacheMaxAge=0)
    data = wd_query.query(wd_queryset)

    pywikibot.output(u'retrieved %d items' % data[u'status'][u'items'])
    for item in data[u'items']:
        itempage = pywikibot.ItemPage(repo, u'Q' + unicode(item))
        yield itempage
    def treat_page_and_item(self, page, item):
        """Treat each item, page is probably None."""
        if not item.exists():
            return

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

        if not u'P136' in claims:
            return

        summary = u'[[Wikidata:WikiProject sum of all paintings/Genre#Normalization|Genre normalization for paintings]]'

        for claim in claims.get(u'P136'):
            currentgenre = claim.getTarget().title()
            if currentgenre in self.genres:
                newgenre = self.genres.get(currentgenre)
                genreItem = pywikibot.ItemPage(self.repo, title=newgenre)
                pywikibot.output(u'Replacing %s with %s' %
                                 (currentgenre, newgenre))
                claim.changeTarget(genreItem, summary=summary)
Beispiel #5
0
def writeACluster2(qnumCluster):
    qnum = qnumCluster[0][
        'qnum']  #any elemnt of qnumClusterList should have the same qnum
    page = pywikibot.ItemPage(wikidata, qnum)

    localClaimsWithSources = propertiesToClaims(qnumCluster)
    remoteClaimsWithSources = getRemoteClaims(qnum)

    for lcs in localClaimsWithSources:
        lc = lcs[0]  #the claim part
        lsl = lcs[1]  #the sources list part

        matchingClaimWithSource = False

        for rcs in remoteClaimsWithSources:
            rc = rcs[0]  #the claim part

            if claimMatch(lc, rc):
                matchingClaimWithSource = rcs
            else:
                pass

        if not matchingClaimWithSource:  #then we didn't find an exact match
            print 'decision add pair ', lcs
            addPair(page, lcs)

        else:  #maybe we can add a source
            sourcesToAdd = list(
            )  #the sources that we'll push to the remoteClaimWithSource, if we find any
            rsl = matchingClaimWithSource[1]  #the source part of the pair
            for ls in lsl:  #localsource in localsourcelist
                if sourceMatch(ls, rsl):
                    pass
                else:
                    sourcesToAdd.append(ls)
            for ls in sourcesToAdd:
                rs = matchingClaimWithSource[0]  #the claim oart if the pair
                print 'add source', ls
                rs.addSource(ls, bot=True)
                positions[str(lc.id) + 'source'] += 1
Beispiel #6
0
    def author(self):
        # tpl_source_meta = get_item_from_listdict(other_sources, 'argument', tplname)
        # if not tpl_source_meta: continue
        if not self.is_author_tpl: return

        pfuncmap = {
            # 'ВИКИДАННЫЕ': self.param_Wikidata,  # сначала ВИКИДАННЫЕ
            # 'ВИКИПЕДИЯ': self.param_Wikipedia,
            # 'ИЗОБРАЖЕНИЕ': self.param_Image,
        }
        for param in self.tpl.params:
            pname = param.name.strip()
            pval = mymwp.get_param_value(self.tpl, pname)
            if not pval: continue

            # сначала ВИКИДАННЫЕ
            if pname == 'ВИКИДАННЫЕ':
                item_id = pval
                if self.itemWD:
                    if self.itemWD.getSitelink('ruwikisource') == item_id:
                        self.changed = mymwp.removeTplParameters(
                            self.tpl, pname)  # очищаем параметр
                else:
                    # подключить элемент ВД
                    self.itemWD = pywikibot.ItemPage(self.wd.WD, item_id)
                    self.itemWD.get()
                    self.itemWD.setSitelink(sitelink={
                        'site': 'ruwikisource',
                        'title': self.title
                    },
                                            summary='Set sitelink')

            if pname == 'ИЗОБРАЖЕНИЕ':
                for c in self.itemWD.claims.get(
                        'P18'):  # 'preferred', 'normal'
                    if c.target._link._text == pval:
                        self.changed = mymwp.removeTplParameters(
                            self.tpl, pname)  # очищаем параметр

                print()
Beispiel #7
0
    def run(self):
        for line in self.file.readlines():
            split = line.split('\t')
            if len(split) < 3 or len(split) % 2 == 0:
                pywikibot.warning('Invalid line: {}'.format(line))
                continue

            try:
                item = pywikibot.ItemPage(self.repo, split[0])
            except Exception as e:
                pywikibot.error(e)
                continue

            try:
                claim = pywikibot.Claim(self.repo, split[1])
            except Exception as e:
                pywikibot.error(e)
                continue

            if not self.set_target(claim, split[2].strip()):
                continue

            qualifiers = []
            references = []
            for prop, value in zip(split[3::2], split[4::2]):
                if prop.startswith('S'):
                    data = references
                    prop = 'P' + prop[1:]
                    key = 'is_reference'
                else:
                    data = qualifiers
                    key = 'is_qualifier'
                snak = pywikibot.Claim(self.repo, prop, **{key: True})
                if self.set_target(snak, value.strip()):
                    data.append(snak)
            for q in qualifiers:
                claim.addQualifier(q)
            if references:
                claim.addSources(references)
            self.user_add_claim(item, claim)
Beispiel #8
0
    def make_stats_for_one_grouping(self, grouping, item_count,
                                    higher_grouping):
        """
        Query the data for one group, return the wikitext.
        """
        text = u'|-\n'

        if self.higher_grouping:
            if higher_grouping:
                text += self.format_higher_grouping_text(higher_grouping)
            else:
                text += u'|\n'

        text += u'| {{Q|%s}}\n' % (grouping, )

        if self.grouping_link:
            try:
                group_item = pywikibot.ItemPage(self.repo, grouping)
                group_item.get()
                label = group_item.labels["en"]
            except pywikibot.exceptions.InvalidTitle:
                label = grouping
            text += f('| [[{self.grouping_link}/{label}|{item_count}]] \n')
        else:
            text += f('| {item_count} \n')

        for column_entry in self.columns:
            column_entry_key = column_entry.get_key()
            try:
                column_count = self.column_data.get(column_entry_key).get(
                    grouping)
            except AttributeError:
                column_count = 0
            if not column_count:
                column_count = 0
            percentage = self._get_percentage(column_count, item_count)
            text += f(
                '| {{{{{self.cell_template}|{percentage}|{column_count}|column={column_entry.get_title()}|grouping={grouping}}}}}\n'
            )  # noqa
        return text
def set_property(entity, pid, value, imported=True):
    """Boilerplate for adding a claim and sourcing it."""
    # Add the claim
    claim = pywikibot.Claim(repo, pid)
    claim.setTarget(value)
    entity.addClaim(claim)

    if imported:
        # Instanciate the source
        statedin = pywikibot.Claim(repo, properties.importedfrom)
        itis = pywikibot.ItemPage(repo, entities.enwiki)
        statedin.setTarget(itis)

        # Instanciate the 'retrieved at' property
        retrieved = pywikibot.Claim(repo, properties.retrieved)
        date = pywikibot.WbTime(year=today.year,
                                month=today.month,
                                day=today.day)
        retrieved.setTarget(date)

        # Add the source
        claim.addSources([statedin, retrieved])
Beispiel #10
0
    def get_location_info(self, identify):
        """Get location coordinates for astrology"""
        item = pywikibot.ItemPage(self.repo, identify)

        item_dict = item.get()
        clm_dict = item_dict["claims"]
        # Get things from all the boxes on the page.

        try:
            coords = clm_dict["P625"]
            # Get things from the box on coordinates.
            for c in coords:
                c_data = c.getTarget()
                lat = c_data.lat
                long = c_data.lon
                alt = c_data.alt
                location_info = [lat, long, alt]

        except KeyError:
            location_info = 'None'

        return location_info
Beispiel #11
0
def _parse_value(value):
    # It may not be a string
    if not isinstance(value, str):
        value = str(value)
    # Build an item in case of QID
    value_is_qid = match(QID_REGEX, value)
    if value_is_qid:
        return pywikibot.ItemPage(REPO, value_is_qid.group())
    # Try to build a date
    try:
        # A date should be in the form '1984-11-16/11'
        date_str, precision = value.split('/')
        date_obj = date.fromisoformat(date_str)
        return pywikibot.WbTime(
            date_obj.year,
            date_obj.month,
            date_obj.day,
            precision=int(precision),
        )
    # Otherwise return the value as is
    except ValueError:
        return value
    def processArtist(self, artistinfo):
        """
        """

        artistid = artistinfo.get(u'itemnum')
        artistItem = None
        if artistid in self.artcenterArtists:
            artistTitle = self.artcenterArtists.get(artistid)
            artistItem = pywikibot.ItemPage(self.repo, title=artistTitle)
        elif self.create:
            pywikibot.output(
                u'Trying to establish notability for http://www.imj.org.il/artcenter/newsite/en/?artist=%s'
                % (artistid, ))
            notability = self.getNotability(artistinfo)
            if notability:
                artistItem = self.createArtist(artistinfo, notability)

        if artistItem:
            pywikibot.output(
                u'Working on %s based on http://www.imj.org.il/artcenter/newsite/en/?artist=%s'
                % (artistItem.title(), artistid))
            self.expandArtist(artistItem, artistinfo)
Beispiel #13
0
    def addOsmRelation(self, qid, osmid, summary):
        """
        Add the actual OSM relation

        :param qid: The qid of the Wikidata item to work on
        :param osmid: The OSM relation ID
        :param summary: The summary to use
        :return: Edit in place
        """
        item  = pywikibot.ItemPage(self.repo, qid)
        data = item.get()
        claims = data.get('claims')

        if 'P402' in claims:
            # Already done
            pywikibot.output('Wikidata %s already has OSM %s, no edit needed' % (qid, osmid))
            return

        newclaim = pywikibot.Claim(self.repo, u'P402')
        newclaim.setTarget(osmid)
        pywikibot.output('Adding %s to %s %s' % (osmid, qid, summary))
        item.addClaim(newclaim, summary=summary)
Beispiel #14
0
def get_from_wikidata(search_text):
    result_list = []

    name = search_text.capitalize()

    def getItems(site, itemtitle):
        params = {
            'action': 'wbsearchentities',
            'format': 'json',
            'language': 'en',
            'type': 'item',
            'search': itemtitle
        }
        request = api.Request(site=site, **params)
        return request.submit()

    wikidataEntries = getItems(site_wikidata, name)
    search = wikidataEntries['search']

    search_id = None
    print(len(search))

    for result in search:
        item = pywikibot.ItemPage(repo_wikidata, result["id"])
        item_dict = item.get()
        try:
            instance_of = item_dict["claims"]["P31"][0].getTarget().getID()
        except:
            continue
        if instance_of == "Q5":
            cont = item._content
            p_label = cont["labels"]["en"]["value"]
            wiki_vars.persons[cont["id"]] = cont
            result_list += [{
                "wiki_id": cont["id"],
                "label": lbl_wikidata(cont["id"])
            }]

    return result_list
Beispiel #15
0
def add_to_wd(x, service, url, qid, repo):
    item = ''
    try:
        item = pywikibot.ItemPage(repo, qid)
    except:
        pywikibot.output(u'Error: QID not found!')
        return ''
    
    claims = item.get(u'claims')
    
    if propery[service] in claims[u'claims']:
        pywikibot.output(u'Error: Already have {} ID!'.format(service))
        return ''
    
    retrieved, statedin = getRef(repo, url)
    stringclaim = pywikibot.Claim(repo, propery[service]) #add the value
    stringclaim.setTarget(x)
    item.addClaim(stringclaim, summary=u'Adding {} ID'.format(service))
    
    stringclaim.addSources([retrieved, statedin], summary=u'Adding reference(s) to {} ID'.format(service))
    
    pywikibot.output(u'Success: Added {} ID!'.format(service))
Beispiel #16
0
 def test_item_missing(self):
     wikidata = self.get_repo()
     # this item is deleted
     item = pywikibot.ItemPage(wikidata, 'Q404')
     self.assertEqual(item._link._title, 'Q404')
     self.assertEqual(item.title(), 'Q404')
     self.assertFalse(hasattr(item, '_content'))
     self.assertEqual(item.id, 'Q404')
     self.assertEqual(item.getID(), 'Q404')
     self.assertEqual(item.getID(numeric=True), 404)
     self.assertFalse(hasattr(item, '_content'))
     self.assertRaises(pywikibot.NoPage, item.get)
     self.assertTrue(hasattr(item, '_content'))
     self.assertEqual(item.id, 'Q404')
     self.assertEqual(item.getID(), 'Q404')
     self.assertEqual(item._link._title, 'Q404')
     self.assertEqual(item.title(), 'Q404')
     self.assertRaises(pywikibot.NoPage, item.get)
     self.assertTrue(hasattr(item, '_content'))
     self.assertEqual(item._link._title, 'Q404')
     self.assertEqual(item.getID(), 'Q404')
     self.assertEqual(item.title(), 'Q404')
Beispiel #17
0
    def addImageToWikidata(self,
                           metadata,
                           imagefile,
                           summary=u'Added the image'):
        """
        Add the image to the Wikidata item. This might add an extra image if the item already has one
        """
        artworkItem = pywikibot.ItemPage(self.repo,
                                         title=metadata.get(u'item'))
        data = artworkItem.get()
        claims = data.get('claims')

        newclaim = pywikibot.Claim(self.repo, u'P18')
        newclaim.setTarget(imagefile)

        foundimage = False
        replacedimage = False
        if u'P18' in claims:
            for claim in claims.get(u'P18'):
                if claim.getTarget().title() == newclaim.getTarget().title():
                    pywikibot.output(u'The image is already on the item')
                    foundimage = True
            if not foundimage and len(claims.get(u'P18')) == 1:
                claim = claims.get(u'P18')[0]
                newimagesize = imagefile.latest_file_info.size
                currentsize = claim.getTarget().latest_file_info.size
                # Only replace if new one is at least 4 times larger
                if currentsize * 4 < newimagesize:
                    summary = u'replacing with much larger image'
                    claim.changeTarget(imagefile, summary=summary)
                    replacedimage = True

        if not foundimage and not replacedimage:
            pywikibot.output('Adding %s --> %s' %
                             (newclaim.getID(), newclaim.getTarget()))
            artworkItem.addClaim(newclaim, summary=summary)
        # Only remove if we had one suggestion. Might improve the logic in the future here
        if u'P4765' in claims and len(claims.get(u'P4765')) == 1:
            artworkItem.removeClaims(claims.get(u'P4765')[0], summary=summary)
    def __init__(self, generator):
        """
        Arguments:
            * generator    - A generator that yields Dict objects.
            The dict in this generator needs to contain 'idpid' and 'collectionqid'
            * create       - Boolean to say if you want to create new items or just update existing

        """
        self.generator = generator
        self.repo = pywikibot.Site().data_repository()
        self.jgsitem = pywikibot.ItemPage(self.repo, u'Q2284748')
        self.snkitem = pywikibot.ItemPage(self.repo, u'Q28045665')
        self.dvritem = pywikibot.ItemPage(self.repo, u'Q28045660')
        self.rbkitem = pywikibot.ItemPage(self.repo, u'Q28045674')
        self.icnitem = pywikibot.ItemPage(self.repo, u'Q2066737')
        #self.rceitem = pywikibot.ItemPage(self.repo, u'Q18600731')
        self.heirsitem = pywikibot.ItemPage(self.repo, u'Q28065304')
Beispiel #19
0
def main(*args):
    collectionid = None

    for arg in pywikibot.handle_args(args):
        if arg.startswith('-collectionid:'):
            if len(arg) == 14:
                collectionid = pywikibot.input(
                        u'Please enter the collectionid you want to work on:')
            else:
                collectionid = arg[14:]

    if not collectionid:
        pywikibot.output(u'Usage: python pwb.py /path/to/art_uk_works_link.py -collectionid:Q180788')
        return

    collectionVenueId = getCollectionVenue(collectionid)
    if not collectionVenueId:
        pywikibot.output(u'No valid collection or venue found')
        return

    (invcache,artukcache) = fillCaches(collectionid)
    dictGen = getArtUKWorkGenerator(collectionVenueId, artukcache)
    repo = pywikibot.Site().data_repository()

    for work in dictGen:
        print(work)
        if work.get(u'invnum') in invcache:
            artworkItemTitle = invcache.get(work.get(u'invnum'))
            pywikibot.output(u'Found an artwork to add a link to: %s' % (artworkItemTitle,))
            artworkItem = pywikibot.ItemPage(repo, title=artworkItemTitle)
            data = artworkItem.get()
            claims = data.get('claims')
            if not u'P1679' in claims:
                newclaim = pywikibot.Claim(repo, u'P1679')
                newclaim.setTarget(work[u'artukid'])
                summary = u'adding link based on [[%s]] with inventory number %s' % (collectionid, work.get(u'invnum'),)
                pywikibot.output(summary)
                artworkItem.addClaim(newclaim, summary=summary)
Beispiel #20
0
    def createNewItemFromPage(self, page, bot=True, **kwargs):
        """
        Create a new Wikibase item for a provided page.

        @param page: page to fetch links from
        @type page: pywikibot.Page
        @param bot: Whether to mark the edit as a bot edit
        @type bot: bool
        @return: pywikibot.ItemPage of newly created item
        @rtype: pywikibot.ItemPage
        """
        sitelinks = {
            page.site.dbName(): {
                'site': page.site.dbName(),
                'title': page.title(),
            }
        }
        labels = {
            page.site.lang: {
                'language': page.site.lang,
                'value': page.title(),
            }
        }
        for link in page.iterlanglinks():
            sitelinks[link.site.dbName()] = {
                'site': link.site.dbName(),
                'title': link.title,
            }
            labels[link.site.lang] = {
                'language': link.site.lang,
                'value': link.title,
            }
        data = {
            'sitelinks': sitelinks,
            'labels': labels,
        }
        result = self.editEntity({}, data, bot=bot, **kwargs)
        return pywikibot.ItemPage(self, result['entity']['id'])
def get_wikidata_info(qid, site=None, cache=None):
    """
    Query Wikidata for additional info about a list entry with a qid.

    If a cache is provided this is used to reduce the number
    of lookups.

    Death year is looked up to assist with Public Domain logic.

    @param qid: Qid for the Wikidata item
    @param site: pywikibot.Site object for Wikidata
    @param cache: dict to use for caching
    @return: bool
    """
    wikidata = site or pywikibot.Site('wikidata', 'wikidata')
    # cannot use cache = cache or {} as this also discards an empty cache
    if cache is None:
        cache = {}

    if qid in cache:
        return cache[qid]

    item = pywikibot.ItemPage(wikidata, qid)
    if not item.exists():
        cache[qid] = {}
    else:
        commonscat = return_first_claim(item, 'P373')
        creator = return_first_claim(item, 'P1472')
        death_year = return_first_claim(item, 'P570')
        if death_year:
            death_year = death_year.year
        cache[qid] = {
            'commonscat': commonscat,
            'creator': creator,
            'death_year': death_year
        }

    return cache[qid]
Beispiel #22
0
def village():
    # parse layers of JSON to get to the links themselves
    query = API_JSON.get("query")
    backlinks = query.get("backlinks")
    # the loop that does the actual heavy lifting
    for link in backlinks:
        title = link["title"]
        site = pywikibot.Site("wikidata", "wikidata")
        repo = site.data_repository()
        page = pywikibot.Page(repo, title)
        item = pywikibot.ItemPage(repo, title)
        item.get()
        ic = item.claims
        id = item.descriptions
        il = item.labels
        ia = item.aliases
        if "P31" in ic:
            try:
                p31val = str(
                    ic["P31"][0].getTarget())  # get value of "instance of"
            except (KeyError):  # in case no P31
                log("noP31", title, "")
                print("no P31 value: " + title)
            if p31val == "[[wikidata:Q532]]":  # is it a village?
                labelItem(page, title, item, il, ia)
                if id.get("en"):  # is there an English-language description?
                    if re.match(
                            "^village *(in|of)* *(Antigua (&|and) Barbuda|The Bahamas|Bosnia (& |and )?Herzegovina|(Cape|Cabo) Verde|Central African Republic|(the |la )?République centrafricaine|(the )?Republic of (the )?Congo|(the )?Democratic Republic of (the )?Congo|Costa Rica|(the )?Czech Republic|(the )?Dominican Republic|East Timor|Timor Leste|El Salvador|Equatorial Guinea|The Gambia|Guinea Bissau|Ivory Coast|C[oô]te d'?Ivoire|(Former Yugoslav )?Republic of Macedonia|(the )?Republic of Ireland|North Korea|South Korea|(the )?Marshall Islands|The Netherlands|New Zealand|Papua New Guinea|The Philippines|(the )?Russian Federation|(St\.?|Saint) Kitts( (&|and) Nevis)?|(St\.?|Saint) Lucia|(St\.?|Saint) Vincent ((and|&) the Grenadines)?|San Marino|S[aã]o Tom[eé]( (and|&) Pr[ií]ncipe)?|Saudi Arabia|Sierra Leone|(the )?Solomon Islands|South Africa|South Sudan|Sri Lanka|Trinidad (and|&) Tobago|(the )?United Arab Emirates|(the )?United Kingdom|(the )?United States( of America)?|(the )?Vatican( City)?|[a-z])*$",
                            id.get("en"), re.I):  # is the description basic?
                        describeItem(item, ic, il, title)
                    else:
                        print(title + " description not changed"
                              )  # marker in cmd line for no description update
                else:
                    describeItem(item, ic, il, title)
            else:
                log("P31errors", title, "")
                print("P31 error (not a village): " + title)
def _delete_or_deprecate(action, qid, tid, catalog, catalog_pid) -> None:
    item = pywikibot.ItemPage(REPO, qid)
    item_data = item.get()
    item_claims = item_data.get('claims')
    # This should not happen:
    # the input item is supposed to have at least an identifier claim.
    # We never know, Wikidata is alive.
    if not item_claims:
        LOGGER.error(
            '%s has no claims. Cannot %s %s identifier %s',
            qid,
            action,
            catalog,
            tid,
        )
        return
    identifier_claims = item_claims.get(catalog_pid)
    # Same comment as the previous one
    if not identifier_claims:
        LOGGER.error(
            '%s has no %s claims. Cannot %s %s identifier %s',
            qid,
            catalog_pid,
            action,
            catalog,
            tid,
        )
        return
    for claim in identifier_claims:
        if claim.getTarget() == tid:
            if action == 'delete':
                item.removeClaims([claim], summary='Invalid identifier')
            elif action == 'deprecate':
                claim.changeRank('deprecated',
                                 summary='Deprecate arguable claim')
            LOGGER.debug('%s claim: %s', action.title() + 'd', claim.toJSON())
    LOGGER.info('%s %s identifier statement from %s',
                action.title() + 'd', catalog, qid)
Beispiel #24
0
def main():
    filename = 'data/composition/52.json'
    with open(filename, 'r') as fp:
        members = json.load(fp)
    for member in tqdm.tqdm(members):
        if 'photo_url' in member:
            continue
        # First try wikipedia, then wikidata
        wiki_slug = member['wiki'].replace(
            'https://nl.wikipedia.org/wiki/',
            '').replace('https://fr.wikipedia.org/wiki/', '')
        r = requests.get(
            f'https://nl.wikipedia.org/w/api.php?action=query&prop=pageprops&titles={wiki_slug}&format=json'
        ).json()
        pages = r["query"]["pages"]
        if pages.keys():
            page = pages[list(pages.keys())[0]]['pageprops']
            if 'page_image_free' in page:
                #print('has one', wiki_slug)
                r = requests.get(
                    f'https://nl.wikipedia.org/w/api.php?action=query&generator=images&prop=imageinfo&titles={wiki_slug}&iiprop=url|dimensions|mime&format=json'
                ).json()
                for image in r['query']['pages'].values():
                    if page['page_image_free'] in image['imageinfo'][0]['url']:
                        member['photo_url'] = image['imageinfo'][0]['url']
                        break

        if 'photo_url' not in member and 'wikibase_item' in member:
            item = pywikibot.ItemPage(repo, member['wikibase_item'])
            image_object = item.page_image()
            if image_object:
                member['photo_url'] = image_object.get_file_url()

        #if 'photo_url' in member:
        #    print(member['photo_url'])

        with open(filename, 'w') as fp:
            json.dump(members, fp, ensure_ascii=False)
Beispiel #25
0
    def processPainting(self, item):
        """
        Work on a individual painting. Add the genre if a label is found
        """
        data = item.get()
        claims = data.get('claims')

        if u'P136' in claims:
            # Already done
            return

        gotmatch = self.findLabelMatch(item)

        if not gotmatch:
            return

        summary = u'based on (%s)"%s" ' % gotmatch

        genreclaim = pywikibot.Claim(self.repo, u'P136')
        genreclaim.setTarget(pywikibot.ItemPage(self.repo, title=u'Q134307'))
        pywikibot.output('Adding genre claim to %s %s' %
                         (item.title(), summary))
        item.addClaim(genreclaim, summary=summary)
Beispiel #26
0
def do_month(cur, year):
    pg = pywikibot.Page(wp, unicode(year))
    print pg
    item = pywikibot.ItemPage.fromPage(pg)
    if item.exists():
        print item.getID()
        #instance of January
        yr_item = pywikibot.ItemPage(repo, 'Q577')
        insert(cur, item, yr_item, instance_of, 'en')
        #part of 1900
        decade = pywikibot.ItemPage.fromPage(
            pywikibot.Page(wp, get_decade(year)))
        insert(cur, item, decade, part_of, 'en')
        #preceded_by
        prev = pywikibot.ItemPage.fromPage(
            pywikibot.Page(wp, unicode(year - 1)))
        if prev.exists():
            insert(cur, item, prev, preceded_by, 'en')
        #followed_by
        follow = pywikibot.ItemPage.fromPage(
            pywikibot.Page(wp, unicode(year + 1)))
        if follow.exists():
            insert(cur, item, follow, followed_by, 'en')
Beispiel #27
0
    def addCatalogCode(self, item, metadata):
        """
        Add the catalog code (P528)

        :param item: The artwork item to work on
        :param metadata: All the metadata about this artwork, should contain the catalogcode and catalogqid field
        :return: Nothing, updates item in place
        """
        claims = item.get().get('claims')

        if 'P528' not in claims and metadata.get('catalogcode') and metadata.get('catalogqid'):
            catalog = pywikibot.ItemPage(self.repo, metadata.get('catalogqid'))

            newclaim = pywikibot.Claim(self.repo, 'P528')
            newclaim.setTarget(metadata.get('catalogcode'))
            pywikibot.output('Adding new catalog code to %s' % item)
            item.addClaim(newclaim)

            newqualifier = pywikibot.Claim(self.repo, 'P972') # Catalog
            newqualifier.setTarget(catalog)
            pywikibot.output('Adding new qualifier claim to %s' % item)
            newclaim.addQualifier(newqualifier)
            self.addReference(item, newclaim, metadata['refurl'])
Beispiel #28
0
    def create_item(self):
        """Create item in repo for current_page."""
        data = {
            'sitelinks': {
                self.site.dbName(): {
                    'site': self.site.dbName(),
                    'title': self.current_page.title()
                }
            },
            'labels': {
                self.site.lang: {
                    'language': self.site.lang,
                    'value': self.current_page.title()
                }
            }
        }
        summary = (u'Bot: New item with sitelink from %s' %
                   self.current_page.title(asLink=True, insite=self.repo))

        item = pywikibot.ItemPage(self.repo)
        item.editEntity(data, new='item', summary=summary)
        output('Created item {item}'.format(item=item.getID()))
        return item
Beispiel #29
0
    def getCreatorItem(self, creator):
        '''
        '''
        try:
            site = pywikibot.Site('commons', 'commons')
            creatorPage = pywikibot.Page(site, title=creator, ns=100)

            if creatorPage.exists():
                if creatorPage.isRedirectPage():
                    creatorPage = creatorPage.getRedirectTarget()
                pywikibot.output(u'Got a creator page %s' %
                                 (creatorPage.title(), ))

                regex = u'\|\s*[wW]ikidata\s*=\s*(Q\d+)\s*'
                match = re.search(regex, creatorPage.get())
                if match:
                    creatorItem = pywikibot.ItemPage(self.repo,
                                                     title=match.group(1))
                    return creatorItem
        except pywikibot.exceptions.InvalidTitle:
            pywikibot.output(u'Found an invalid title')
            pass
        return None
def _parse_value(value):
    # It may not be a string
    if not isinstance(value, str):
        value = str(value)
    # Build an item in case of QID
    value_is_qid = search(QID_REGEX, value)
    if value_is_qid:
        return pywikibot.ItemPage(REPO, value_is_qid.group())
    # Try to build a date
    try:
        date_value = date.fromisoformat(value)
        # Precision hack: it's a year if both month and day are 1
        precision = (vocabulary.YEAR if date_value.month == 1
                     and date_value.day == 1 else vocabulary.DAY)
        return pywikibot.WbTime(
            date_value.year,
            date_value.month,
            date_value.day,
            precision=precision,
        )
    # Otherwise return the value as is
    except ValueError:
        return value