def save_page( text: str, page: pywikibot.Page, summary: str, bot: bool = True, minor: bool = False, mode: str = "replace", ) -> None: logger.info(f"Saving to {page.title()}") if not text: raise pywikibot.PageNotSaved( page, message="New page text is blank, page %s was not saved") if mode == "replace": text = text elif mode == "append": text = page.get(force=True) + text elif mode == "prepend": text = text + page.get(force=True) else: raise ValueError( "mode must be 'replace', 'append', or 'prepend', not {mode}") if page.get(force=True) == text: raise pywikibot.PageNotSaved( page, message="Page text did not change, page %s was not saved") else: page.text = text page.save( summary=summary, minor=minor, botflag=bot, quiet=True, ) logger.info(f"Page {page.title(as_link=True)} saved")
def task(self): # pragma: no cover page = Page(self.wiki, "Benutzer:THE IT/Werkstatt") new_text = self.replace_in_page(page.text) page.text = new_text if not self.debug: page.save("Time is ticking") return True
def userPut( self, page: pywikibot.Page, oldtext: str, newtext: str, summary: Optional[str] = None, minor: bool = True, botflag: Optional[bool] = None, ) -> None: if oldtext == newtext: pywikibot.output("No changes were needed on %s" % page.title(as_link=True)) return pywikibot.output("\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title(as_link=True)) pywikibot.showDiff(oldtext, newtext) if summary: pywikibot.output("Summary: %s" % summary) page.text = newtext try: page.save(summary=summary, minor=minor, botflag=botflag) except pywikibot.EditConflict: raise except pywikibot.Error as e: pywikibot.output("Failed to save %s: %r: %s" % (page.title(as_link=True), e, e))
def trySaving(page: pywikibot.Page, content: str, summary: str, overwrite: bool, limitType: str = 'default') -> bool: """Create or overwrite page with given content, checking bot limits. Summary is prepended with link to BRFA and appended with 'Report problems'. """ global _editsDone # pylint: disable=global-statement if limitType not in _editsLimits or limitType not in _editsDone: raise Exception(f'Undefined limit type: "{limitType}"') if _editsDone[limitType] >= _editsLimits[limitType]: return False _editsDone[limitType] += 1 if _onlySimulateEdits: return False page.text = content summary = (f'[[Wikipedia:Bots/Requests_for_approval/' f'{_botName}_{_brfaNumber}|({_brfaNumber})]] ' f'{summary} [[User talk:TokenzeroBot|Report problems]]') page.save(summary, minor=False, botflag=True, watch="nochange", createonly=False if overwrite else True, nocreate=True if overwrite else False, tags='bot trial' if _botTrial else None) return True
def get_wiki_bearbeitungen(self): dummypage = Page(self.wiki, 'Benutzer:THEbotIT/dummy') dummypage.text = '{{subst:NUMBEROFEDITS}}' dummypage.save('get_new_number') del dummypage dummypage = Page(self.wiki, 'Benutzer:THEbotIT/dummy') return dummypage.text
def get_all_sites(self): dummypage = Page(self.wiki, 'Benutzer:THEbotIT/dummy') dummypage.text = '{{subst:NUMBEROFARTICLES}}' dummypage.save('get_new_number') del dummypage dummypage = Page(self.wiki, 'Benutzer:THEbotIT/dummy') return '{}'.format(dummypage.text)
def finish_task(self): if self.data: if not self.debug: page = Page(self.wiki, "RE:Wartung:Strukturfehler") page.text = page.text + self._build_entry() page.save("Neue Fehlermeldungen", botflag=True) super().finish_task()
def task(self): list_platzhalter = [] list_protected = [] lemma_list = self.get_list() for idx, item in enumerate(lemma_list): lemma = Page(self.wiki, item["title"]) if self.is_protected(lemma): list_protected.append(lemma.title()) lemma.protect(protections={ "edit": "autoconfirmed", "move": "autoconfirmed" }, reason="is now common") categories = [item.title() for item in lemma.categories()] if "Kategorie:RE:Platzhalter" in categories: list_platzhalter.append(lemma.title()) self.logger.info( f"{idx}/{len(lemma_list)} prot: {len(list_protected)}, plat: {len(list_platzhalter)} {lemma.title()}" ) page_protected = Page(self.wiki, "Benutzer:THE IT/RE/Arthur Stein/protected") page_protected.text = self.join_lists(list_protected) page_protected.save() page_platzhalter = Page(self.wiki, "Benutzer:THE IT/RE/Arthur Stein/platzhalter") page_platzhalter.text = self.join_lists(list_platzhalter) page_platzhalter.save() return True
def generate_overviews(self): """Create page on wikis with overview of bot results.""" for site in self.sites: sync_overview_page = Page(site, 'User:{0}/sync.py overview' .format(site.user())) output = '== Pages that differ from original ==\n\n' if self.differences[site]: output += ''.join('* [[:%s]]\n' % l for l in self.differences[site]) else: output += 'All important pages are the same' output += ( '\n\n== Admins from original that are missing here ==\n\n') if self.user_diff[site]: output += ''.join('* %s\n' % l.replace('_', ' ') for l in self.user_diff[site]) else: output += ( 'All users from original are also present on this wiki') pywikibot.output(output) sync_overview_page.text = output sync_overview_page.save(self.put_message(site))
class WsStatus(CanonicalBot): def __init__(self, wiki, debug): CanonicalBot.__init__(self, wiki, debug) self.text = None self.stat_page = None def task(self): if self.debug: lemma = 'Benutzer:THEbotIT/' + self.bot_name else: lemma = 'WS:Statistik' self.load_text_from_site(lemma) # self.new_row(str(RowSeitenstatistik(self.wiki, self.logger)), 'SEITENSTATISTIK') self.new_row(str(RowBearbeitungsstand(self.wiki, self.logger)), 'BEARBEITUNGSSTAND') self.save_text_to_site() return True def new_row(self, row, placeholder): self.text = re.sub('<!--BOT:{}-->'.format(placeholder), '<!--BOT:{}-->\n{}' .format(placeholder, row), self.text) def load_text_from_site(self, lemma): self.logger.info('Load text from {}'.format(lemma)) self.stat_page = Page(self.wiki, lemma) self.text = self.stat_page.text def save_text_to_site(self): self.stat_page.text = self.text self.stat_page.save('Statistik wurde aktualisiert.', botflag=True)
def add_old_cfd( page: pywikibot.Page, cfd_page: CfdPage, action: str, result: str, summary: str, ) -> None: """Add {{Old CfD}} to the talk page.""" date = cfd_page.title(with_section=False).rpartition('/')[2] if page.exists(): wikicode = mwparserfromhell.parse(page.text, skip_style_tags=True) for tpl in wikicode.ifilter_templates(): try: template = pywikibot.Page(page.site, str(tpl.name), ns=10) if template not in TPL['old cfd'] or not tpl.has( 'date', ignore_empty=True): continue except pywikibot.InvalidTitle: continue if tpl.get('date').value.strip() == date: # Template already present. return old_cfd = Template('Old CfD') old_cfd.add('action', action) old_cfd.add('date', date) old_cfd.add('section', cfd_page.section()) old_cfd.add('result', result) page.text = str(old_cfd) + '\n' + page.text page.save(summary=summary)
def user_page_the_it(self, korrigiert: Tuple[int, int]): status_string = [] color = make_html_color(20e6, 22e6, korrigiert[0]) status_string.append( f"<span style=\"background:#FF{color}{color}\">{korrigiert[0]}</span>" ) color = make_html_color(5.0e3, 5.25e3, korrigiert[1]) status_string.append( f"<span style=\"background:#FF{color}{color}\">{korrigiert[1]}</span>" ) list_of_lemmas = self.petscan(["RE:Teilkorrigiert", "RE:Korrigiert"], ["RE:Unkorrigiert", "RE:Unvollständig"]) date_page = Page(self.wiki, list_of_lemmas[0]["title"]) date_of_first = str(date_page.oldest_revision.timestamp)[0:10] gap = datetime.now() - datetime.strptime(date_of_first, "%Y-%m-%d") color = make_html_color(3 * 365, 3.5 * 365, gap.days) status_string.append( f"<span style=\"background:#FF{color}{color}\">{date_of_first}</span>" ) user_page = Page(self.wiki, "Benutzer:THE IT/Werkstatt") temp_text = user_page.text temp_text = re.sub(r"<!--RE-->.*<!--RE-->", f"<!--RE-->{' ■ '.join(status_string)}<!--RE-->", temp_text) user_page.text = temp_text user_page.save("todo RE aktualisiert")
def task(self): if self.debug: # activate for debug purpose lemma = "Benutzer:THEbotIT/" + self.bot_name else: lemma = "Die Gartenlaube" page = Page(self.wiki, lemma) temp_text = page.text alle = self.petscan([]) fertig = self.petscan(["Fertig"]) korrigiert = self.petscan(["Korrigiert"]) unkorrigiert = self.petscan(["Unkorrigiert"]) articles = self.petscan([], article=True, not_categories=["Die Gartenlaube Hefte"]) temp_text = self.projektstand(temp_text, alle, fertig, korrigiert, unkorrigiert, articles) temp_text = self.alle_seiten(temp_text, alle) temp_text = self.korrigierte_seiten(temp_text, korrigiert) temp_text = self.fertige_seiten(temp_text, fertig) for year in range(1853, 1900): temp_text = self.year(year, temp_text) page.text = temp_text page.save("Ein neuer Datensatz wurde eingefügt.", botflag=True) return True
def make_magazines(self, dictionary_of_magazines_by_year): for idx_year, year in enumerate(dictionary_of_magazines_by_year): magazines = dictionary_of_magazines_by_year[year] self.logger.debug(f"make_mag_year {idx_year + 1}/" f"{len(dictionary_of_magazines_by_year)}") for idx_mag, magazine in enumerate(magazines): self.logger.debug("make_mag_mag {idx}/{len} ... issue:{year}/{mag}" .format(idx=idx_mag + 1, len=len(magazines), year=year, mag=magazine)) if year == "1986" and magazine == "31": self.logger.warning("There is magazine 1986, 31, " "this is special, no creating here") continue if self.debug: lemma = Page(self.wiki, "Benutzer:THEbotIT/Test") else: lemma = Page(self.wiki, f"Die Gartenlaube ({year})/Heft {int(magazine):d}") new_text = self.make_magazine(year, magazine) if new_text: if hash(new_text.strip()) != hash(lemma.text.strip()): self.logger.debug("Print [[Die Gartenlaube ({year})/Heft {magazine}]]." .format(year=year, magazine=magazine)) if lemma.text != '': lemma.text = new_text lemma.save("Automatische Aktualisierung des Heftes", botflag=True) else: lemma.text = new_text lemma.save("automatische Hefterstellung", botflag=True) else: self.logger.debug("Keine Änderung im Text ({year}/{magazine})." .format(year=year, magazine=magazine))
def logGroup(self, page: pywikibot.Page, users: List[pywikibot.User]) -> None: text = page.get(force=True) if page.exists() else "" for user in users: newLine = f"\n* [[Benutzer:{user.username}|{user.username}]]" if not newLine in text: text += newLine page.text = text page.save(summary=f"Bot: Benutzerliste nach Botlauf aktualisiert.")
def task(self): for idx, item in enumerate(self.get_list()): lemma = Page(self.wiki, item["title"]) temp_text = lemma.text for cat in self.cat_list: temp_text = re.sub(f"\[\[Kategorie:{cat}\]\]\n?", "", temp_text) lemma.text = temp_text lemma.save("remove categories") return True
def history(self, fertig, korrigiert, unkorrigiert): page = Page(self.wiki, "Benutzer:THEbotIT/" + self.bot_name) temp_text = page.text composed_text = "".join(["|-\n", "|", self.timestamp.start_of_run.strftime("%Y%m%d-%H%M"), "||", str(unkorrigiert[1]), "||", str(unkorrigiert[0]), "||", str(int(unkorrigiert[0] / unkorrigiert[1])), "||", str(korrigiert[1]), "||", str(korrigiert[0]), "||", str(int(korrigiert[0] / korrigiert[1])), "||", str(fertig[1]), "||", str(fertig[0]), "||", str(int(fertig[0] / fertig[1])), "\n<!--new line-->"]) temp_text = re.sub("<!--new line-->", composed_text, temp_text) page.text = temp_text page.save("RE Statistik aktualisiert", botflag=True)
def task(self): # pragma: no cover regex = re.compile(r"\n\|PND=") searcher = PetScan() searcher.add_yes_template("ADBDaten") searcher.add_positive_category("ADB:Ohne GND-Link") lemma_list = searcher.run() for lemma in lemma_list: page = Page(self.wiki, lemma["title"]) temp_text = page.text if regex.search(temp_text): self.logger.info(f"change {lemma['title']}") temp_text = regex.sub("\n|GND=", temp_text) page.text = temp_text page.save("PND -> GND", botflag=True) return True
def task(self): lemma_list = self._run_searcher() self._build_database(lemma_list) if self.debug: dump = Page(self.wiki, f"Benutzer:THEbotIT/{self.bot_name}") else: dump = Page(self.wiki, "Liste der Autoren") old_text = dump.text new_text = self._convert_to_table() if new_text[150:] != old_text[150:]: # compare all but the date dump.text = new_text dump.save("Die Liste wurde auf den aktuellen Stand gebracht.", botflag=True) else: self.logger.info("Heute gab es keine Änderungen, " "daher wird die Seite nicht überschrieben.") return True
def check_page(self, pagename): """Check one page.""" pywikibot.output('\nChecking ' + pagename) sys.stdout.flush() page1 = Page(self.original, pagename) txt1 = page1.text if self.options.dest_namespace: dest_ns = int(self.options.dest_namespace) else: dest_ns = None for site in self.sites: if dest_ns is not None: page2 = Page(site, page1.title(with_ns=False), dest_ns) pywikibot.output('\nCross namespace, new title: ' + page2.title()) else: page2 = Page(site, pagename) if page2.exists(): txt2 = page2.text else: txt2 = '' if str(site) in config.replicate_replace: txt_new = multiple_replace(txt1, config.replicate_replace[str(site)]) if txt1 != txt_new: pywikibot.output( 'NOTE: text replaced using config.sync_replace') pywikibot.output('{0} {1} {2}'.format(txt1, txt_new, txt2)) txt1 = txt_new if txt1 != txt2: pywikibot.output('\n {0} DIFFERS'.format(site)) self.differences[site].append(pagename) if self.options.replace: page2.text = txt1 page2.save(self.put_message(site)) else: sys.stdout.write('.') sys.stdout.flush()
def check_page(self, pagename): """Check one page.""" pywikibot.output("\nChecking %s" % pagename) sys.stdout.flush() page1 = Page(self.original, pagename) txt1 = page1.text if self.options.dest_namespace: dest_ns = int(self.options.dest_namespace) else: dest_ns = None for site in self.sites: if dest_ns is not None: page2 = Page(site, page1.title(withNamespace=False), dest_ns) pywikibot.output("\nCross namespace, new title: %s" % page2.title()) else: page2 = Page(site, pagename) if page2.exists(): txt2 = page2.text else: txt2 = '' if str(site) in config.replicate_replace: txt_new = multiple_replace(txt1, config.replicate_replace[str(site)]) if txt1 != txt_new: pywikibot.output( 'NOTE: text replaced using config.sync_replace') pywikibot.output('%s %s %s' % (txt1, txt_new, txt2)) txt1 = txt_new if txt1 != txt2: pywikibot.output("\n %s DIFFERS" % site) self.differences[site].append(pagename) if self.options.replace: page2.text = txt1 page2.save(self.put_message(site)) else: sys.stdout.write('.') sys.stdout.flush()
def check_page(self, pagename): """Check one page.""" pywikibot.output("\nChecking %s" % pagename) sys.stdout.flush() page1 = Page(self.original, pagename) txt1 = page1.text for site in self.sites: if self.options.dest_namespace: prefix = namespaces(site)[int(self.options.dest_namespace)] if prefix: prefix += ':' new_pagename = prefix + page1.titleWithoutNamespace() pywikibot.output("\nCross namespace, new title: %s" % new_pagename) else: new_pagename = pagename page2 = Page(site, new_pagename) if page2.exists(): txt2 = page2.text else: txt2 = '' if str(site) in config.replicate_replace: txt_new = multiple_replace(txt1, config.replicate_replace[str(site)]) if txt1 != txt_new: pywikibot.output( 'NOTE: text replaced using config.sync_replace') pywikibot.output('%s %s %s' % (txt1, txt_new, txt2)) txt1 = txt_new if txt1 != txt2: pywikibot.output("\n %s DIFFERS" % site) self.differences[site].append(pagename) if self.options.replace: page2.text = txt1 page2.save(self.put_message(site)) else: sys.stdout.write('.') sys.stdout.flush()
def task(self): lemma_list = self._search() for idx, lemma in enumerate(lemma_list): page = Page(self.wiki, title='Index:{}'.format(lemma['title'])) self.logger.info('{}/{}:{}'.format(idx, len(lemma_list), page)) match = regex_picture.search(page.text) if match: self.logger.info(match.group(1)) temp = re.sub('\|\d{2,3}px', '', match.group(1)) if not re.search('thumb', match.group(1)): temp = temp + '|thumb' self.logger.info(temp) if temp == match.group(1): self.logger.info('nothing to do here.') continue temp = '|BILD=[[{}]]'.format(temp) temp_text = regex_picture.sub(temp, page.text) page.text = temp_text page.save(botflag=True, summary='set thumb as parameter') return True
def main(): page = Page(site, "Star Wars Wiki:Apêndice de Tradução de obras/JSON") fixes = {} with open("bot/user-fixes.py") as f: exec(f.read( )) # Can't import it normally because of naming and undefined variable if not 'obras' in fixes: raise Exception('Obras dictionary not found') fixes_json = json.dumps(fixes['obras'], ensure_ascii=False, indent=4) new_text = "<pre>{}</pre>".format(fixes_json.replace('\\\\1', '$1')) showDiff(page.text, new_text) if get_user_yes_or_no("Salvar?"): page.text = new_text page.save( "([[User:Thales César|Thales]]) 2.2 Atualizado com novas informações" )
def history(self, fertig: Tuple[int, int], korrigiert: Tuple[int, int], unkorrigiert: Tuple[int, int]): page = Page(self.wiki, "Benutzer:THEbotIT/" + self.bot_name) temp_text = page.text composed_text = "".join([ "|-\n", "|", self.timestamp.start_of_run.strftime("%Y%m%d-%H%M"), "||", str(unkorrigiert[1]), "||", str(unkorrigiert[0]), "||", str(int(unkorrigiert[0] / unkorrigiert[1])), "||", str(korrigiert[1]), "||", str(korrigiert[0]), "||", str(int(korrigiert[0] / korrigiert[1])), "||", str(fertig[1]), "||", str(fertig[0]), "||", str(int(fertig[0] / fertig[1])), "\n<!--new line-->" ]) temp_text = re.sub("<!--new line-->", composed_text, temp_text) page.text = temp_text page.save("RE Statistik aktualisiert", botflag=True)
def generate_overviews(self): """Create page on wikis with overview of bot results.""" for site in self.sites: sync_overview_page = Page(site, 'User:%s/sync.py overview' % site.user()) output = "== Pages that differ from original ==\n\n" if self.differences[site]: output += "".join('* [[:%s]]\n' % l for l in self.differences[site]) else: output += "All important pages are the same" output += "\n\n== Admins from original that are missing here ==\n\n" if self.user_diff[site]: output += "".join('* %s\n' % l.replace('_', ' ') for l in self.user_diff[site]) else: output += "All users from original are also present on this wiki" pywikibot.output(output) sync_overview_page.text = output sync_overview_page.save(self.put_message(site))
def save_page(page_name: str, text: str, sheet_name: str, template_name: str) -> None: '''実際に wiki のページを書き込む''' if template_name is None: template_name = page_name # Bot 編集ページであることを知らせるフッターを付加して更新する sheet_url = get_sheet_url(sheet_name) footer = '\n\n{{bot/編集の注意|template_name = %s | url = %s}}' \ % (template_name, sheet_url) text += footer # ページに変更がない場合には何もしない page = Page(site, page_name) if page.text == text: return page.text = text if args.debug: print(page.text) else: page.save()
def make_magazines(self, dictionary_of_magazines_by_year): for idx_year, year in enumerate(dictionary_of_magazines_by_year): magazines = dictionary_of_magazines_by_year[year] self.logger.debug(f"make_mag_year {idx_year + 1}/" f"{len(dictionary_of_magazines_by_year)}") for idx_mag, magazine in enumerate(magazines): self.logger.debug( f"make_mag_mag {idx_mag + 1}/{len(magazines)} ... issue:{year}/{magazine}" ) if year == "1986" and magazine == "31": self.logger.warning( "There is magazine 1986, 31, this is special, no creating here" ) continue if self.debug: lemma = Page(self.wiki, "Benutzer:THEbotIT/Test") else: lemma = Page( self.wiki, f"Die Gartenlaube ({year})/Heft {int(magazine):d}") new_text = self.make_magazine(year, magazine) if new_text: if hash(new_text.strip()) != hash(lemma.text.strip()): self.logger.debug( f"Print [[Die Gartenlaube ({year})/Heft {magazine}]]." ) if lemma.text != '': lemma.text = new_text lemma.save( "Automatische Aktualisierung des Heftes", botflag=True) else: lemma.text = new_text lemma.save("automatische Hefterstellung", botflag=True) else: self.logger.debug( f"Keine Änderung im Text ({year}/{magazine}).")
def user_page_the_it(self, korrigiert): status_string = [] color = make_html_color(20e6, 22e6, korrigiert[0]) status_string.append(f"<span style=\"background:#FF{color}{color}\">{korrigiert[0]}</span>") color = make_html_color(5.0e3, 5.25e3, korrigiert[1]) status_string.append(f"<span style=\"background:#FF{color}{color}\">{korrigiert[1]}</span>") list_of_lemmas = self.petscan(["RE:Teilkorrigiert", "RE:Korrigiert"], ["RE:Unkorrigiert", "RE:Unvollständig"]) date_page = Page(self.wiki, list_of_lemmas[0]["title"]) date_of_first = str(date_page.oldest_revision.timestamp)[0:10] gap = datetime.now() - datetime.strptime(date_of_first, "%Y-%m-%d") color = make_html_color(3 * 365, 3.5 * 365, gap.days) status_string.append(f"<span style=\"background:#FF{color}{color}\">{date_of_first}</span>") user_page = Page(self.wiki, "Benutzer:THE IT/Werkstatt") temp_text = user_page.text temp_text = re.sub(r"<!--RE-->.*<!--RE-->", f"<!--RE-->{' ■ '.join(status_string)}<!--RE-->", temp_text) user_page.text = temp_text user_page.save("todo RE aktualisiert")
def handle_maariv_paper_page(paper_page: pw.Page) -> None: publish_date = _extract_paper_page_from_title(paper_page.title()) if publish_date is None: return parsed_mw_text = mwparserfromhell.parse(paper_page.text) paper_template = parsed_mw_text.filter_templates( mark_as_paper_template_name)[0] if publish_date_param_name in paper_template: logger.info( f'Page: {paper_page} is already marked with publish date, skipping this paper' ) return paper_template.add(publish_date_param_name, publish_date) logger.info( f'Added publish date: {publish_date} for page: {paper_page.title()}') paper_page.text = parsed_mw_text if SHOULD_SAVE: paper_page.save( summary="MaccabiBotAdd publish dates for maariv papers", botflag=True)
def remove_cfd_tpl(page: pywikibot.Page, summary: str) -> None: """ Remove the CfD template from the page. @param page: Page to edit @param summary: Edit summary """ text = re.sub( r'<!--\s*BEGIN CFD TEMPLATE\s*-->.*?' r'<!--\s*END CFD TEMPLATE\s*-->\n*', '', page.get(force=True), flags=re.I | re.M | re.S, ) wikicode = mwparserfromhell.parse(text, skip_style_tags=True) for tpl in wikicode.ifilter_templates(): try: template = pywikibot.Page(page.site, str(tpl.name), ns=10) if template in TPL['cfd']: wikicode.remove(tpl) except pywikibot.InvalidTitle: continue page.text = str(wikicode).strip() page.save(summary=summary)
def send_log_to_wiki(self): wiki_log_page = f"Benutzer:THEbotIT/Logs/{self.bot_name}" page = Page(self.wiki, wiki_log_page) page.text += self.logger.create_wiki_log_lines() page.save(f"Update of Bot {self.bot_name}", botflag=True)
def save_if_changed(page: Page, text: str, change_msg: str): if text.rstrip() != page.text: page.text = text page.save(change_msg, botflag=True)
size_all_changes = 0 for idx_rev, revision in enumerate(version_history): user = revision.user if user == 'Pfaerrich': if idx_rev > 0: size_prev = len(version_history[idx_rev-1].text) else: size_prev = 0 size_all_changes += abs(len(version_history[idx_rev].text) - size_prev) korrigiert_flag = False if size_all_changes > 0: for version in page.getVersionHistory(): if version.user == 'Pfaerrich': if re.search('orrigiert', version.comment): korrigiert_flag = True break print(size_all_changes, len(page.text), korrigiert_flag) if (size_all_changes / len(page.text)) < 0.03 and not korrigiert_flag: list_for_pfaerrich.append([page.title(), size_all_changes, len(page.text)]) report_page = Page(wiki, 'Benutzer:THEbotIT/List_for_Pfaerrich') header = '{|class="wikitable sortable"\n! Lemma\n! Größe\n! geändert von dir' text = [] for line in list_for_pfaerrich: text.append('|-\n|[[{lemma}]]\n|{size}\n|{changes}'.format(lemma=line[0], size=line[2], changes=line[1])) text = '\n'.join(text) text = '{header}\n{text}\n|}}'.format(header=header, text=text) report_page.text = text report_page.save(botflag=True, summary='blub')
if idx_rev > 0: size_prev = len(version_history[idx_rev - 1].text) else: size_prev = 0 size_all_changes += abs( len(version_history[idx_rev].text) - size_prev) korrigiert_flag = False if size_all_changes > 0: for version in page.getVersionHistory(): if version.user == 'Pfaerrich': if re.search('orrigiert', version.comment): korrigiert_flag = True break print(size_all_changes, len(page.text), korrigiert_flag) if (size_all_changes / len(page.text)) < 0.03 and not korrigiert_flag: list_for_pfaerrich.append( [page.title(), size_all_changes, len(page.text)]) report_page = Page(wiki, 'Benutzer:THEbotIT/List_for_Pfaerrich') header = '{|class="wikitable sortable"\n! Lemma\n! Größe\n! geändert von dir' text = [] for line in list_for_pfaerrich: text.append('|-\n|[[{lemma}]]\n|{size}\n|{changes}'.format( lemma=line[0], size=line[2], changes=line[1])) text = '\n'.join(text) text = '{header}\n{text}\n|}}'.format(header=header, text=text) report_page.text = text report_page.save(botflag=True, summary='blub')
# -*- coding: utf-8 -*- __author__ = 'eso' import sys sys.path.append('../../') import re from pywikibot import Page, Site from tools.petscan import PetScan wiki = Site() searcher = PetScan() searcher.add_yes_template('Biel') lemma_list = searcher.run() for idx, lemma in enumerate(lemma_list): print(idx, len(lemma_list), lemma['title']) link_page = Page(wiki, lemma['title']) temp_text = link_page.text if re.search('\{\{Biel\|', temp_text): temp_text = re.sub('\{\{Biel\|1240647\}\}', '{{Bielefeld|1240647}}', temp_text) temp_text = re.sub('\{\{Biel\|590504\}\}', '{{Bielefeld|590504}}', temp_text) temp_text = re.sub('\{\{Biel\|1732676\}\}', '{{Bielefeld|1732676}}', temp_text) temp_text = re.sub('\{\{Biel\|548435\}\}', '{{Bielefeld|548435}}', temp_text) temp_text = re.sub('\{\{Biel\|32920\}\}', '{{Bielefeld|32920}}', temp_text) if link_page.text != temp_text: link_page.text = temp_text link_page.save(botflag=True, summary='Biel -> Bielefeld')
def ensureIncludedAsTemplate(mainLogPage: pywikibot.Page, subLogPageTitle: str) -> None: if not f"{{{subLogPageTitle}}}" in mainLogPage.get(force=True): mainLogPage.text = mainLogPage.text + f"\n{{{{{subLogPageTitle}}}}}" mainLogPage.save( summary=f"Bot: Unterseite [[{subLogPageTitle}]] eingebunden.")
return presenters_string if __name__ == '__main__': tree = ElementTree.parse(sys.argv[-1]) out = '<templatestyles src="{}" />\n'.format(CSS_PAGE) root = tree.getroot() days = root.findall('.//day') day_index = 1 while days: day = get_next_day(days) days.remove(day) day_string = build_day_string(day) out += '{{#ifeq:{{{day}}}' + '|{}|'.format(day_index) out += '{{#invoke:Program|start|accessibility={{{accessibility}}}|research={{{research}}}|strategy={{{strategy}}}|education={{{education}}}|growth={{{growth}}}|technology={{{technology}}}|partnerships={{{partnerships}}}|technology={{{technology}}}|strategy={{{strategy}}}|health={{{health}}}|partnerships={{{partnerships}}}|multimedia={{{multimedia}}}|glam={{{glam}}}}}' events = day.findall('.//event') while events: event = get_next_event(events) events.remove(event) out += create_wikitext_item(event) out += '{{#invoke:Program|end_}}}}\n' day_index += 1 if '--bot' in sys.argv: from pywikibot import Site from pywikibot import Page page = Page(Site(), PAGE_NAME, PAGE_NAMESPACE) page.text = out page.save() else: print(out)
class ThanksMeter: def __init__(self): self._site = Site('bg', fam='wikipedia') self._page = Page(self._site, 'Потребител:Iliev/Мерсиметър') self._settings_page = Page(self._site, 'Потребител:Iliev/Мерсиметър/settings.json') self._settings = jload(self._settings_page.text) self._user_blocks = {} def _is_user_blocked(self, user): try: return self._user_blocks[user] except KeyError: self._user_blocks[user] = User(self._site, user).is_blocked() return self._user_blocks[user] def _must_ignore_user(self, user): if user in self._settings['ignored_users']: return True elif self._is_user_blocked(user): self._settings['ignored_users'].append(user) return True return False def _get_thanks(self, since_datetime): thanks = dict(r=dict(), s=dict(), c=0) for thank in self._site.logevents(logtype='thanks', end=since_datetime): if self._must_ignore_user(thank.user()): continue try: thanks['r'][thank.page().title(with_ns=False)] += 1 except KeyError: thanks['r'][thank.page().title(with_ns=False)] = 1 try: thanks['s'][thank.user()] += 1 except KeyError: thanks['s'][thank.user()] = 1 thanks['c'] += 1 return self._sort_user_thanks(thanks) def _sort_user_thanks(self, user_thanks_dict): presort = dict( r=dict(sorted(user_thanks_dict['r'].items())), s=dict(sorted(user_thanks_dict['s'].items())), c=user_thanks_dict['c'] ) return dict( r=dict(sorted(presort['r'].items(), key=lambda _: _[1], reverse=True)), s=dict(sorted(presort['s'].items(), key=lambda _: _[1], reverse=True)), c=presort['c'] ) def _draw_table(self, user_thanks_dict, title): self._page.text += '{| class="wikitable sortable col-2-right"\n' self._page.text += f'|+ {title}\n' self._page.text += '! Редактор !! Брой\n|-\n' for user, thanks in user_thanks_dict.items(): self._page.text += f'| [[Потребител:{user}|{user}]] || {thanks}\n|-\n' self._page.text += '|}\n' def init_page(self): script_url = 'https://github.com/kerberizer/wikimedia-scripts/blob/master/thanksmeter.py' self._page.text = f"''Тази страница е генерирана автоматично от [{script_url} скрипт] в " self._page.text += "'''{{subst:CURRENTTIME}}''' [[UTC]] на '''{{subst:CURRENTDAY}} " self._page.text += "{{subst:CURRENTMONTHNAME}} {{subst:CURRENTYEAR}}''' г.''\n" def save_page(self): try: self._page.save(summary='Бот: актуализация', minor=False) except APIError as e: print('ERROR: Cannot save page: APIError: ' + str(e)) def draw_tables(self, since_datetime, group_title): thanks_help = ':en:Help:Notifications/Thanks' thanks = self._get_thanks(since_datetime) self._page.text += f'== {group_title} ==\n' self._page.text += f": ''Общо [[{thanks_help}|благодарности]]: " self._page.text += f"'''{thanks['c']}'''''\n" self._page.text += '<div style="float: left;">\n' self._draw_table(thanks['r'], 'Получени') self._page.text += '</div><div style="float: left;">\n' self._draw_table(thanks['s'], 'Изпратени') self._page.text += '</div>{{br}}\n'