Skip to content

Commit

Permalink
feat: fix wikidata param position; improve str cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
amasotti committed Jan 14, 2024
1 parent 6894f92 commit ed8b64c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
19 changes: 18 additions & 1 deletion bot/wikidata/WikidataBot.py
@@ -1,4 +1,6 @@
# wikidata_bot.py
import re

import pywikibot
from pywikibot.pagegenerators import WikidataSPARQLPageGenerator

Expand All @@ -7,7 +9,22 @@ def __init__(self):
self.site = pywikibot.Site().data_repository()

def _clean_city_name(self, city_name):
return city_name.replace("[", "").replace("]", "").strip()
"""
Clean the city name from brackets or alternative names
It could be found in the wikitext in the following formats:
- [[City name]] -- remove the brackets
- [[City name|Alt name]] -- remove the brackets and the alternative name
- City Name -- keep as it is
:param city_name: City name to clean
:return: The cleaned city name
"""
# Remove brackets
city_name = city_name.replace("[[", "").replace("]]", "")

# Remove alternative names
city_name = re.sub(r'\|.*', '', city_name)

return city_name

def get_wikidata_entity_by_wikipedia_article_name(self,article_name, alt, lang='it'):

Expand Down
3 changes: 2 additions & 1 deletion bot/wikivoyage/VoyBot.py
Expand Up @@ -43,6 +43,7 @@ def process_wikidata_in_citylist(self, templates):
"""
wd_bot = WikidataBot()
for template in templates:

# Conditions
is_target_template = (template.name == CITY_TEMPLATE_ITEM_NAME
or template.name == DESTINATION_TEMPLATE_ITEM_NAME)
Expand All @@ -69,7 +70,7 @@ def _process_wikidata(self, name, wikidata_id, template):
self.write_log_line(f"{self.current_page} -- No wikidata item found for {name}")
else:
pywikibot.logging.stdout(f"\tFound wikidata item for {name}: {wikidata_id}")
template.add("wikidata", wikidata_id)
template.add("wikidata", wikidata_id, before="descrizione", preserve_spacing=True)

def write_log_line(self, text, file="logs/citylist_log.log"):
"""
Expand Down

0 comments on commit ed8b64c

Please sign in to comment.