import pywikibot
from pywikibot import pagegenerators

wikidata = pywikibot.Site('wikidata', 'wikidata')
commons = pywikibot.Site('commons', 'commons')
def iter_items():
    query = r'SELECT ?item WHERE { ?item wdt:P31 wd:Q729. MINUS { ?item wdt:P18 _:b24. } ?sitelink schema:isPartOf <https://commons.wikimedia.org/>. } LIMIT 500'
    
    gen = pagegenerators.WikidataSPARQLPageGenerator(
        query, wikidata)
    return gen
def get_commons_sitelink(item):
    sitelink = item.getSitelink(commons)
    return sitelink
def check_for_images(page):
    
    
    def check_gallery(gallery):
        return bool(gallery.page_image())
    
    
    if page.namespace().id == 14:
        #It'sa category
        cat_info = cat.categoryinfo
        if cat_info['files'] > 0:
            return True
        elif cat_info['subcats'] > 0:
            for file in cat.members(recurse=True, namespaces=6):
                return True
        elif cat_info['pages'] > 0:
            for gallery in cat.members(recurse=True, namespaces=0):
                if check_gallery(gallery):
                    return True
        else:
            return False
    elif page.namespace().id == 0:
        return check_gallery(page)
    else:
        return False
def main():  
    for item in iter_items():
        page = get_commons_sitelink(item)
        print(item, page)
        #if check_for_images(page):
        #    print(f'{item.title()} ({item.getID()}): {page.title()}')

main()
---------------------------------------------------------------------------
NoPage                                    Traceback (most recent call last)
<ipython-input-17-3fcc14309193> in <module>
      6         #    print(f'{item.title()} ({item.getID()}): {page.title()}')
      7 
----> 8 main()

<ipython-input-17-3fcc14309193> in main()
      1 def main():
      2     for item in iter_items():
----> 3         page = get_commons_sitelink(item)
      4         print(item, page)
      5         #if check_for_images(page):

<ipython-input-3-e7fa190a6370> in get_commons_sitelink(item)
      1 def get_commons_sitelink(item):
----> 2     sitelink = item.getSitelink(commons)
      3     return sitelink

/srv/paws/pwb/pywikibot/page.py in getSitelink(self, site, force)
   4566         dbname = self.getdbName(site)
   4567         if dbname not in self.sitelinks:
-> 4568             raise pywikibot.NoPage(self)
   4569         else:
   4570             return self.sitelinks[dbname]

NoPage: Page [[wikidata:Q3006133]] doesn't exist.

Licensing

Copyright 2019 AntiCompositeNumber

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

To fork:

  1. Open the raw page and save it to your computer
  2. Go to your PAWS control panel and sign in using OAUTH
  3. Click Upload and upload the file from step 1
  4. To run, open the notebook and click Cell > Run All
p = pywikibot.ItemPage(wikidata, 'Q3006133')
for link in p.iterlinks():
    print(link)
[[wikipedia:en:Elmendorf Beast]]
[[wikipedia:fr:Créature d'Elmendorf]]
[[wikipedia:pl:Bestia z Elmendorf]]
[[wikipedia:zh:埃尔门多夫野兽]]