User:Matt Crypto/aotd-update.py

A script to create an "Article of the Day" box

 * 1) !/usr/bin/python
 * 2) Script to update an "article of the day" box on Wikipedia.  Cobbled
 * 3) together by User:Matt Crypto (matt_crypto@yahoo.co.uk) and
 * 4) released into the public domain.
 * 5) OPTIONS:
 * 1) OPTIONS:
 * 1) OPTIONS:

articleListFile = "./articles.dat"
 * 1) A file containing a list of articles

outputTable = "./table.txt"
 * 1) A file to output the table

previousDays = 2
 * 1) How many rows to include before today's article (0 or more)

nextDays = 2
 * 1) How many rows to include after (0 or more)

scrambleOrder = True
 * 1) Scramble the order of the articles?

articleIndexFile = "./index.dat"
 * 1) Current article pointer

afterMidnight = True
 * 1) (Set to "False" if you're generating before midnight for the following day)

scoreBase = dict([ \   ("Beale ciphers", 6),    ("Caesar cipher", 8),    ("Certificate authority", 6),    ("Cryptanalysis of the Enigma", 3),    ("Data Encryption Standard", 10),    ("El Gamal", 2),    ("Enigma machine", 10),    ("Frequency analysis", 8),    ("International Data Encryption Algorithm", 6),    ("Plaintext", 7),    ("Public key infrastructure", 5),    ("RC5", 5),    ("Rotor machine", 6),    ("ROT13", 10),    ("Snake oil (cryptography)", 4),    ("Substitution cipher", 6),    ("Timeline of cryptography", 0),    ("Vigenère cipher", 8),    ])
 * 1) Table of scores


 * 1) END OF OPTIONS

from time import time, gmtime, strftime from random import shuffle, seed from urllib import * import urllib import re

oneDayInSeconds = 24 * 60 * 60 timeFmt = "%A, %d %B"

r1 = re.compile(r'\[\[(.*?)\]\]', re.MULTILINE)
 * 1) Regexp for scraping page names

def urlSafe(articleName): return urllib.quote(re.sub(' ', '_', articleName), safe=' :/\'')

print "Creating an ``article of the day'' box."

try: f = open(articleListFile) text = "".join(f.readlines) f.close except: print "Error: Couldn't get an article list..." raise articleList = r1.findall(text) numberOfArticles = len(articleList)
 * 1) Get a list of articles

if scrambleOrder: seed(42) shuffle(articleList)
 * 1) Scramble the article order in a deterministic way

print "Found %d articles:" % len(articleList) print " - ".join(articleList)

try: f = open(articleIndexFile) articleIndex = int("".join(f.readlines)) % numberOfArticles f.close except: print "Couldn't find a current article pointer, so starting at 0." print "Today's article is %s." % articleList[articleIndex]
 * 1) Read in index

header = \ """{| """ footer = re.sub('%s', articleList[articleIndex], footer)
 * 1) Wikicode for the table header
 * }

table = header + "".join(rowList) + footer

try: f = open(outputTable, "w") f.write(table) f.close except: print "Error: Couldn't write article table..." raise
 * 1) Write out the article table

print "Table written to %s" % outputTable

try: f = open(articleIndexFile, "w") f.write("%d" % ((articleIndex + 1) % numberOfArticles)) f.close except: print "Error: Couldn't write article index pointer..." raise
 * 1) Update the index pointing to the current article

print "Done."