# python pwb.py addSitelinkandLabel -lang:ku -family:wikipedia -transcludes:"Şablon:Înterwîkî etîket û danasîn"
import pywikibot
import re
from pywikibot import pagegenerators
from pywikibot.bot import AutomaticTWSummaryBot, ConfigParserBot, SingleSiteBot
def parse_template(page_text):
template_start = page_text.find("{{Înterwîkî etîket û danasîn|")
template_end = page_text.find("}}", template_start)
if template_start != -1 and template_end != -1:
template_content = page_text[template_start + len("{{Înterwîkî etîket û danasîn|"):template_end].split('|')
# Initialize variables
lang_code = None
interwiki_title = None
label = None
description = None
# Check each parameter
for param in template_content:
if param.startswith('d='):
description = param[2:].strip()
elif param.startswith('e='):
label = param[2:].strip()
else:
if lang_code is None:
lang_code = param.strip()
elif interwiki_title is None:
interwiki_title = param.strip()
# Adjust label and description based on 'd=' and 'e='
if 'd=' in description:
label = description.split('d=')[1].strip()
description = label
elif 'e=' in label:
description = label.split('e=')[1].strip()
return lang_code, interwiki_title, label, description
def get_wikidata_id(lang_code, interwiki_title):
site = pywikibot.Site(lang_code, "wikipedia")
page = pywikibot.Page(site, interwiki_title)
# Get the Wikidata item ID based on the updated Wikipedia title
try:
item_page = pywikibot.Page(pywikibot.Link(f'{lang_code}:{interwiki_title}', source=site.data_repository()))
item_id = item_page.data_item().id
return item_id
except pywikibot.exceptions.InvalidTitleError:
pywikibot.error(f"Invalid title: {interwiki_title}")
return None
def add_sitelink(page, item_id):
site = pywikibot.Site("ku", "wikipedia")
repo = site.data_repository()
try:
item = pywikibot.ItemPage(repo, title=item_id)
item.setSitelink(page, summary=f"Added sitelink {page} ([[User:Balyozbot#Task1|Task 1]])")
print(f"Sitelink added for {page.title()} to {item_id}")
except pywikibot.exceptions.OtherPageSaveError as e:
print(f"Error adding sitelink for {page.title()}: {str(e)}")
except Exception as e:
print(f"Error in add_sitelink function: {str(e)}")
def add_description_to_wikidata(item_id, label, description):
site = pywikibot.Site('wikidata', 'wikidata')
repo = site.data_repository()
try:
# Create ItemPage using QID
item = pywikibot.ItemPage(repo, title=item_id)
item_dict = item.get()
labels = item_dict.get('labels', {})
descriptions = item_dict.get('descriptions', {})
existing_label = labels.get('ku', None)
existing_description = descriptions.get('ku', None)
if existing_label is None:
item.editLabels({"ku": label}, summary=f"Added [ku] label: {label} ([[User:Balyozbot#Task1|Task 1]])")
print(f"label '{label}' added to {item_id}")
else:
print(f"Label already exists: {existing_label}")
if existing_description is None:
item.editDescriptions({"ku": description}, summary=f"Added [ku] description: {description} ([[User:Balyozbot#Task1|Task 1]])")
print(f"Description '{description}' added to {item_id}")
else:
print(f"Description already exists: {existing_description}")
except pywikibot.exceptions.OtherPageSaveError as e:
print(f"Error adding label and description to Wikidata for item {item_id}: {str(e)}")
except Exception as e:
print(f"Error in add_description_to_wikidata function: {str(e)}")
def remove_template(page_text):
template_start = page_text.find("{{Înterwîkî etîket û danasîn|")
template_end = page_text.find("}}", template_start)
if template_start != -1 and template_end != -1:
# Find the position of the line break after the template
line_break_pos = page_text.find("\n", template_end + 2)
# Remove the template from the page
if line_break_pos != -1:
return page_text[:template_start] + page_text[line_break_pos + 1:]
else:
return page_text[:template_start] + page_text[template_end + 2:]
else:
return page_text
def is_disambiguation_page(lang_code, interwiki_title):
site = pywikibot.Site(lang_code, "wikipedia")
page = pywikibot.Page(site, interwiki_title)
# Load the page content to get categories
page.get()
# Check if the page has the "Disambiguation pages" category
for category in page.categories():
if category.title() == 'Category:Disambiguation pages':
return True
return False
class AppendTextBot(
SingleSiteBot,
ConfigParserBot,
AutomaticTWSummaryBot,
):
summary_key = 'basic-changing'
use_redirects = False
update_options = {
'summary': None,
'text': '',
'top': False,
}
def treat_page(self) -> None:
page = self.current_page
text = page.text
if page.namespace() == 0:
lang_code, interwiki_title, label, description = parse_template(text)
if lang_code and interwiki_title:
interwiki_page = pywikibot.Page(pywikibot.Site('en', 'wikipedia'), interwiki_title)
# Check if the page is a redirect
if interwiki_page.isRedirectPage():
# If it is a redirect, update interwiki_title to the redirected page title
interwiki_title = interwiki_page.getRedirectTarget().title()
if is_disambiguation_page(lang_code, interwiki_title):
pywikibot.output(f"Skipping disambiguation page: {page.title()}")
# Add "Category:Pages interwiki linking to disambiguation pages" only if it doesn't exist
category_page = pywikibot.Page(page.site, "Kategorî:Rûpelên bi înterwîkiyê yên diçin rûpelên cudakirinê")
category_link = f'[[{category_page.title()}]]'
if category_link not in page.text:
page.text = page.text + "\n" + category_link
page.save(summary="Bot: +[[Kategorî:Rûpelên bi înterwîkiyê yên diçin rûpelên cudakirinê]]")
return
item_id = get_wikidata_id(lang_code, interwiki_title)
if item_id:
try:
add_sitelink(page, item_id)
except pywikibot.exceptions.OtherPageSaveError as e:
print(f"Error saving sitelink for page {page.title()}: {str(e)}. Skipping the current page.")
return
except Exception as e:
print(f"Error processing page {page.title()}: {str(e)}. Skipping the current page.")
return
add_description_to_wikidata(item_id, label, description)
updated_text = remove_template(text)
text = updated_text
summary="Bot: Rûpel bi Wîkîdaneyê hat girêdan û şablon hat rakirin"
pywikibot.output(f"Final text to put: {text}")
self.put_current(text, summary=summary)
else:
print(f"Unable to get Wikidata ID for page: {page.title()}. Skipping the current page.")
else:
print(f"Invalid template or missing information on page: {page.title()}")
else:
print(f"Page not in mainspace: {page.title()}")
def main(*args: str) -> None:
local_args = pywikibot.handle_args(args)
gen_factory = pagegenerators.GeneratorFactory()
local_args = gen_factory.handle_args(local_args)
options = {'text': ''}
for arg in local_args:
option, _, value = arg.partition(':')
if option in ('summary', 'text'):
if not value:
pywikibot.input(f'Please enter a value for {option}')
options[option] = value
else:
options[option] = True
gen = gen_factory.getCombinedGenerator(preload=True)
if not pywikibot.bot.suggest_help(missing_generator=not gen):
bot = AppendTextBot(generator=gen, **options)
bot.run()
if __name__ == '__main__':
main()