Skip to content

Commit

Permalink
better determine where to place template
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenOrJames committed Nov 25, 2015
1 parent 35cc5ab commit accb3b7
Showing 1 changed file with 67 additions and 15 deletions.
82 changes: 67 additions & 15 deletions enwiki/wp_alps_mountains.py
Expand Up @@ -14,18 +14,24 @@
site = pywikibot.Site()
site.login()


class AlpsMountainsBot(object):

def __init__(self):
self.site = pywikibot.Site()
template = pywikibot.Page(site, "Template:WikiProject Mountains")
self.template_titles = list()
self.template_titles.append(template.title(withNamespace=False).lower())
wp_template = pywikibot.Page(site, "Template:WikiProject Mountains")
shell_template = pywikibot.Page(site, "Template:WikiProject Mountains")
self.wp_template_titles = self._get_titles(wp_template)
self.shell_titles = self._get_titles(shell_template)
self.added_template = mwparserfromhell.parse(
"{{WikiProject Mountains|class=|importance=|alps=yes|alps-importance=}}")

def _get_titles(self, template):
"""Gets a list of the lowercase titles of a template and its redirects"""
titles = [template.title(withNamespace=False).lower()]
for reference in template.getReferences(withTemplateInclusion=False, redirectsOnly=True):
self.template_titles.append(reference.title(withNamespace=False).lower())
self.template_titles = list(set(self.template_titles))
self.added_template = mwparserfromhell.parse("{{WikiProject Mountains|class=|importance=|alps=yes|alps-importance=}}\n")

titles.append(reference.title(withNamespace=False).lower())
return list(set(titles))

def run(self):
category = pywikibot.Category(site, "Category:Mountains of the Alps")
for page in category.articles():
Expand All @@ -34,16 +40,21 @@ def run(self):
else:
talk = page.toggleTalkPage()
article = talk.toggleTalkPage()

try:
talk_text = talk.get()
except pywikibot.exceptions.NoPage:
talk_text = ""
except pywikibot.exceptions.IsRedirectPage:
continue

talk_code = mwparserfromhell.parse(talk_text)
found = False
shell = None
for template in talk_code.ifilter_templates():
if not template.name.lower().strip() in self.template_titles:
if template.name.lower().strip() in self.shell_titles:
shell = template
if not template.name.lower().strip() in self.wp_template_titles:
continue
found = True
if not template.has_param("alps"):
Expand All @@ -64,16 +75,57 @@ def run(self):
break
if stub:
self.added_template.filter_templates()[0].add("class", "stub")
talk_code.insert(0, self.added_template)

# If there is a WikiProjectBannerShell, use it.
if shell is not None:
shell = mwparserfromhell.parse(shell)
shell = shell.nodes[0]
shell.get(1).value.append("\n" + str(self.added_template))
else:
lead_section = talk_code.get_sections()[0]
lead_templates = lead_section.filter_templates()
if lead_templates:
before = None
after = None
for t in lead_templates:
n = t.name.lower().strip()
if "wikiproject" in n or "wp" in n:
after = t
if "toc" in n:
before = t
break
if before is not None:
lead_section.insert_before(before, str(self.added_template)+"\n")
elif after is not None:
lead_section.insert_after(after, "\n"+str(self.added_template))
else:
if not lead_section.endswith("\n"):
lead_section.append("\n")
lead_section.append(self.added_template)
lead_section.append("\n\n")
else:
lead_section.insert(0, str(self.added_template)+"\n\n")
summary = "Added"
else:
summary = "Updated"
if talk_text != unicode(talk_code):
print talk.title()
pywikibot.showDiff(talk_text, unicode(talk_code))
talk.put(unicode(talk_code), "[[Wikipedia:Bots|Bot]]: %s {{WikiProject Mountains}} template ([[Wikipedia talk:WikiProject Mountains|discussion]])" % summary)

if talk_text.strip() != str(talk_code):
try:
print(talk.title())
except UnicodeEncodeError:
pass
pywikibot.showDiff(talk_text, str(talk_code).strip())
talk.put(
str(talk_code),
"[[Wikipedia:Bots|Bot]]: %s {{WikiProject Mountains}} template " % summary +
"([[Wikipedia talk:WikiProject Mountains|discussion]])"
)
else:
print talk.title() + " (no edits)"
try:
print(talk.title() + " (no edits)")
except UnicodeEncodeError:
pass


if __name__ == "__main__":
try:
Expand Down

0 comments on commit accb3b7

Please sign in to comment.