Created
August 13, 2019 05:51
-
-
Save apple502j/ada39ab4d75ddb3800cc5f9bc40f35aa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from urllib.parse import unquote | |
import re | |
import mw_api_client as mwc | |
try: | |
with open(".lastpage-iw", "r", encoding="utf-8") as f: | |
lastpage = f.read() | |
except OSError: | |
lastpage = "" | |
def make_from_pairs(pairs): | |
s = "" | |
for p in pairs: | |
s += f"[[{p[0]}:{p[1]}]]" | |
return s | |
ALLOWED_IWLINKS = "en", "de", "ru", "fr", "hu", "id", "nl", "tw" | |
IW_REGEX = re.compile(r"\[\[({IWPREFIXES}):([^\]]*)\]\]".format(IWPREFIXES="|".join(ALLOWED_IWLINKS))) | |
def get_pair(content): | |
return [m.groups() for m in IW_REGEX.finditer(content)] | |
jaw = mwc.Wiki("https://ja.scratch-wiki.info/w/api.php", "IWBot/mw-api-client") | |
jaw.login("Username@IWBot","Enter your bot password here...") | |
enw = mwc.Wiki("https://en.scratch-wiki.info/w/api.php", "IWBot/mw-api-client") | |
for page in jaw.allpages(getinfo=True, apfrom=lastpage, apfilterredir="nonredirects", apfilterlanglinks="withlanglinks", **{"assert":"bot"}): | |
print(f"-------------------- NOW DOING {page.title} --------------------") | |
if page.protection: | |
continue | |
content = page.read() | |
pairs = get_pair(content) | |
enw_pair = tuple(filter(lambda a:a[0]=="en", pairs)) | |
if not enw_pair: | |
continue # enwiki not found! | |
enpgname = unquote(enw_pair[0][1]) | |
print(f"------------------- FOUND EN {enpgname} --------------") | |
enpage = enw.page(enpgname) | |
enpcont = enpage.read() | |
enp_pairs = get_pair(content) | |
new_pair = pairs.copy() | |
for index, np in enumerate(pairs): | |
temp = tuple(filter(lambda a:a[0]==np[0], enp_pairs)) | |
if temp: | |
new_pair[index] = temp[0] | |
temp2 = list(filter(lambda a:a[0] not in [b[0] for b in pairs], enp_pairs)) | |
new_pair += temp2 | |
content = IW_REGEX.sub("", content) | |
content += make_from_pairs(new_pair) | |
print("-------------------- CONFIRM ---------------------------") | |
print(f"Old: {pairs}\nNew: {new_pair}") | |
stamp = time.time() | |
if pairs != new_pair and input("Save? > "): | |
page.edit(content, "Interwiki (Automated)") | |
print(f"-------------------- SAVED {page.title} --------------------") | |
else: | |
print(f"-------------------- DONE {page.title} --------------------") | |
with open(".lastpage-iw", "w", encoding="utf-8") as f: | |
f.write(page.title) | |
slpt = stamp + 10 - time.time() | |
if slpt > 0: | |
print(f"----------------- WAITING FOR {round(slpt*10)/10} SECS --------------") | |
time.sleep(slpt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment