Entries in "Wiki"
Automating Case Wiki Tasks
A while ago Chris added a login method to the CAS module in CaseClasses. It returns a mechanize Browser object so that you can programmatically surf the web as if you had logged into CAS in a real web browser.
CaseClasses also has a Codes module that has the abbreviated codes for majors, departments, and buildings. I combined these two features to tackle the Building codes project on the Case Wiki.
P.S.: There is a MediaWiki API that would normally be used to do this kind of stuff, but according to Greg, editing is not fully functional yet.
Think you could add a lot to the wiki with some automated task? Here's how it was done.
First, you'll need mechanize and CaseClasses:
$ sudo easy_install mechanize
$ sudo easy_install http://opensource.case.edu/svn/CaseClasses/python/trunk
Now log into CAS with mechanize:
import Case
from getpass import getpass
username = 'bmb12'
password = getpass() # Enter a password without echoing
cas = Case.CAS()
browser = cas.login(username, password)
You can open any page with browser and interact with it as a logged in Case user. So let's go to the Case Wiki and log in:
browser.set_handle_robots(False)
browser.open("http://wiki.case.edu")
browser.follow_link(text_regex='Log In')
Editing can be done like so:
browser.open("http://wiki.case.edu/User:Brian.Beck")
browser.follow_link(text='Edit this page')
browser.select_form(name='editform')
browser['wpTextbox1'] += " Also, this guy sucks!"
browser.submit()
Automating the building code edits was done like so:
for code, name in Case.Codes.buildings.iteritems():
url = "http://wiki.case.edu/%s" % name.replace(' ', '_')
try:
browser.open(url)
except:
print "Didn't find %r." % name
else:
browser.follow_link(text='Edit this page')
browser.select_form(name='editform')
source = browser['wpTextbox1']
add_text = "The building code for %s is [[building code:=%s]].\r\n"
add_text %= (name, code)
if 'code:=' not in source:
insert_at = source.find('{{Building')
if insert_at != -1:
new_source = source[:insert_at] + add_text + source[insert_at:]
else:
new_source = source + add_text
browser['wpTextbox1'] = new_source
browser.submit()
print "Added building code for %r." % name
Happy automating!
Update: The same has now been done for the Street addresses project. Check out the discussion to see how.Saturday Mapping Thrills
Today I decided that I needed to be punished and proceeded to create and geocode 58 parking lots on the Case Wiki.
This will slightly help the campus crime mapper since some reports are given lot numbers as their location. You probably noticed that the crime mapper got a bit fancier. I made icons for a few categories of violations, and took care of multiple incidents being located in one spot. It's also running with FastCGI now so maybe it's faster?
One pattern I've already started to notice is that some places are hotspots for a certain type of violation. Check out the Crimes in 2006 page and you can tell that it's a bad idea to park your bike near Fribley, and to park your car on Carlton Road. (Also on that page, zoom way out for a surprise!)
Well, I'm all geocoded out after hunting down parking lots for the past couple hours. Suggestions for the crime mapper are welcome.
P.S. The code is now on opensource.case.edu and there's a to-do list on the wiki. Feel free to browse around or contribute!
Update: Hey all you geocoders and wiki mappers, check out this awesome resource I just found. It's a build identification glossary with location names, aliases, and addresses. This is exactly what the crime mapper needs...
