import pywikibot as pw
from pywikibot import pagegenerators as pg
import re

hywiki = pw.Site('hy', 'wikipedia')
source = pw.Page(hywiki, 'Մասնակից:ԱշոտՏՆՂ/Սևագրություն')
repo = hywiki.data_repository()
for line in source.text.splitlines():
    Q = re.sub(r'#\[\[:d:(Q\d+)\]\]', r'\1', line)
    item = pw.ItemPage(repo, Q)
    if 'hywiki' in item.get()['sitelinks']:
        hypage = pw.Page(hywiki, item.get()['sitelinks']['hywiki'])
        if not hypage.exists():
            item.removeSitelink(site='hywiki')
import pywikibot as pw
from pywikibot import pagegenerators as pg
import re
import sys
from sqlalchemy import create_engine
import sys, os
import pandas as pd


constr = 'mysql+pymysql://{user}:{pwd}@{host}'.format(user=os.environ['MYSQL_USERNAME'],
                                                      pwd=os.environ['MYSQL_PASSWORD'],
                                                      host=os.environ['MYSQL_HOST'])
con = create_engine(constr)

df = pd.read_sql("select ips_site_page, ips_item_id from wikidatawiki_p.wb_items_per_site where ips_site_id = 'hywwiki' and ips_site_page like 'Կատեգորիա:%%'", con)

wikidata = pw.Site('wikidata', 'wikidata')
print(len(df))
for index, row in df.iterrows():
    try:
        item = pw.ItemPage(wikidata, "Q" + str(row.ips_item_id))
        item.setSitelink(sitelink={'site': 'hywwiki', 'title': row.ips_site_page.decode("utf-8").replace("Կատեգորիա:", "Ստորոգութիւն:")}, summary=u'Namespace name has been changed')
    except:
        print(item)
        pass
25
WARNING: API error failed-save: The save has failed.
[[wikidata:Q3740]]