User:BenSbot/Code1

From JoCopedia
< User:BenSbot
Revision as of 09:35, 9 August 2008 by BenSbot (talk | contribs) (Added code for reference)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Here is the code for the first function I was programmed to do. If you have any questions ask in the discussion page.

import wikipedia
import catlib
import pagegenerators
import re
import datetime


text = ""
site = wikipedia.getSite()
linkslist = {}
p = re.compile('^[^#\\n].*$' , re.M)
q = re.compile('\\[\\[[^\\]]*\\]\\]')

showscat = catlib.Category(site,'Category:Shows')
showslist = list(pagegenerators.CategorizedPageGenerator(showscat))
for b in showslist: 
    text = p.sub("",b.get())
    links = q.findall(text)
    for x in links:
        lx = x.lower()
        if linkslist.has_key(lx):
            v = linkslist[lx][1] + 1
            linkslist[lx] = (linkslist[lx][0], v)
        else:
            linkslist[lx] = (x,1)

text = "The following is a list of the songs [[Jonathan Coulton]] has played in concert.  This list has been compiled from the setlists currently available here on JoCopedia in the [[:Category:Shows|Shows]] section, by an awesome bot designed by user [[User:BenS|BenS]].  Keep in mind that not all setlists are currently available to JoCopedia, and not all setlists are 100%.  But this is a pretty good indicator.  This list is current as of " + str(datetime.date.today()) + "\n\n"

items = linkslist.values()

items.sort(lambda x,y: cmp(y[1], x[1]) or cmp(x[0], y[0]))
for l, c in items:
    text = text + ("*" + l + ": " + repr(c) + "\n")

text = text + "\n" + "[[Category:Show Statistics]]"

page = wikipedia.Page(site, u"SongStats")
page.put(text, u"Song statistics")

print "fin"