Purple exclamation mark.svg Planning the future of Botwiki! - Help us bring Botwiki up to date, contribute to our strategy discussion, add bot scripts, and contribute manuals, guides, and tutorials! Almost anything related to bots, particularly those used to edit mediawiki, is welcome.

Red exclamation mark.svg UNABLE TO EDIT? - We've experienced attacks by spambots lately and now require you to confirm your e-mail before you can edit (go to your preferences, enter an e-mail address, and request a confirmation e-mail, then go to your e-mail and click on the confirmation link). We also require new accounts to make a few edits and wait a few minutes before before you can create a page; however, if this is a problem contact us in #botwiki and we can manually confirm your account. Sorry for the inconvenience.

Python:DateRedirects.py

From Botwiki
Jump to: navigation, search
#!/usr/bin/python
# -*- coding: utf-8  -*-
"""
This bot create redirects from pages named [[D MMM]] to the equivallent
[[MMM D]] page, where D is the number of the day and MMM is the name of
the month, in Gregorian calendar.
"""
import wikipedia
import sys
 
# This is required for the text that is shown when you run this script
# with the parameter -help.
 
def main():
    months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
    'August', 'September', 'October', 'November', 'December']
    days = [31,29,31,30,31,30,31,31,30,31,30,31]
 
    for i in range(len(months)):
        for j in range(days[i]):
            rSource = "%s " % (j+1) + "%s " % months[i]
            rDest = "%s " % months[i] + "%s " % (j+1)
            #print ("%s => %s" % (rSource, rDest))
            page = wikipedia.Page(wikipedia.getSite(), rSource)
            try:
                text = page.get()
            except wikipedia.NoPage:
                page2 = wikipedia.Page(wikipedia.getSite(), rDest)
                try:
                    text = page2.get()
                except wikipedia.NoPage:
                    wikipedia.output(u"Destination page %s does not exist; skipping." % page2.aslink())
                    continue
                except wikipedia.IsRedirectPage:
                    wikipedia.output(u"Destination page %s is a redirect; skipping." % page2.aslink())
                    continue
                #create the redirect
                page.put(u"#REDIRECT %s" % page2.aslink(), u"Robot: Creating date redirect")
            except wikipedia.IsRedirectPage:
                wikipedia.output(u"Source page %s is a redirect; skipping." % page.aslink())
                continue
            except wikipedia.LockedPage:
                wikipedia.output(u"Source page %s is locked; skipping." % page.aslink())
                continue
 
 
if __name__ == "__main__":
    try:
        main()
    finally:
        wikipedia.stopme()
Personal tools
Share