Skip to content

Instantly share code, notes, and snippets.

@frankgeerlings
Created May 15, 2016 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankgeerlings/8dd1338e8fdf9984aa7e49eb0144040d to your computer and use it in GitHub Desktop.
Save frankgeerlings/8dd1338e8fdf9984aa7e49eb0144040d to your computer and use it in GitHub Desktop.
A way to easily translate Wikipedia POTD to a message suitable for Dutch wikipedia (which you will still have to do yourself)
#!/usr/bin/env python
from pywikibot import config2 as config
import sys, pywikibot, re
from datetime import date
from dateutil.parser import parse
if len(sys.argv) < 2:
today = date.today()
print ('Usage: %s %s' % (sys.argv[0], str(today)))
dt = parse(sys.argv[1])
d = date(dt.year, dt.month, dt.day)
english = u'Template:Potd/%s (en)' % str(d)
dutch = u'Template:Potd/%s (nl)' % str(d)
def deinterwiki(t):
nointerwiki = re.sub(r'\[\[:nl:(.*?)\]\]', "[[\\1]]", t)
nodoubles = re.sub(r'\[\[(.*?)\|\1\]\]', "[[\\1]]", nointerwiki)
return nodoubles
"""Haalt POTD-template eraf en de verwijzingen naar :nl: in de links ook"""
def tocleandutch(commons):
if commons == "":
return commons
r = re.match(r'(?:.*?)\|1=(.*?)\|2=', commons)
# r = re.match(r'Potd description\|1=(.*)\|2=nl\|3=\d+\|4=\d+\|5=\d+', commons)
(t,) = r.groups()
return deinterwiki(t)
def reinterwiki(t):
# Vervangt de pipe-vrije met pipes (dus [[Jantje]] wordt [[Jantje|Jantje]] maar laat degene die al pipes hebben met rust
metpipes = re.sub(r'\[\[([^|]+?)\]\]', '[[\\1|\\1]]', t)
# Zet er :nl: voor
metinterwiki = re.sub(r'\[\[(.*?)\]\]', '[[:nl:\\1]]', metpipes)
return metinterwiki
def potdtemplate(t, datum):
return "{{Potd description|1=%s|2=nl|3=%04d|4=%02d|5=%02d}}" % (t, datum.year, datum.month, datum.day)
"""Zet een puur wikitekstje om naar eentje met :nl: en een POTD-template eromheen
zodat-ie rechtstreeks op commons kan opgeslagen"""
def tocommonsdutch(cleandutch, datum):
return potdtemplate(reinterwiki(cleandutch), datum)
def main(*args):
pywikibot.handle_args(args)
pywikibot.ui.encoding='utf-8'
if config.family != 'commons' or config.mylang != 'commons':
pywikibot.warning('Doe dit op commons.')
return
en = pywikibot.Page(pywikibot.Site(), english)
nl = pywikibot.Page(pywikibot.Site(), dutch)
pywikibot.output(en.text)
editable = tocleandutch(nl.text)
saveablebefore = tocommonsdutch(editable, d)
pywikibot.output("EDIT THIS: " + editable)
result = pywikibot.input('Wat is de nl-wikitekst? (ENTER=Overslaan):')
saveable = tocommonsdutch(result, d)
# Aborted or no change, so do nothing
if result == '' or saveable == saveablebefore:
pywikibot.output("Not saving.")
return
pywikibot.output("SAVING THIS: " + saveable)
nl.text = saveable
nl.save(u'Update Dutch (nl) picture of the day description')
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment