Skip to content

Instantly share code, notes, and snippets.

@hugovk
Created February 6, 2014 07:07
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 hugovk/8839550 to your computer and use it in GitHub Desktop.
Save hugovk/8839550 to your computer and use it in GitHub Desktop.
Hacky script used for renaming (most) of the Wikipedia/Wikimedia photos of members of congress into bioguide IDs for use by https://github.com/sunlightlabs/congress/
#!/usr/bin/env python
import csv
import os
import re
import sys
def mvit(infile, outfile):
for suffix in [
"_113th_Congress_2013",
"_113th_Congress",
"",
"_official_portrait",
"_portrait",
"_official",
"pic",
"%2C_official_113th_Congress_photo_portrait",
"_official_portrait_112th_Congress",
"_official_portrait%2C_2009",
"%2C_official_portrait%2C_112th_Congress",
"%2C_official_portrait%2C_113th_Congress",
"_2010",
"%2C_official_110th_Congress_photo",
"_official_photo",
"_113th",
"-Senate_Portrait",
"_Official_Photo_110th_Congress",
"2C_official_portrait%2C_112th_Congress_alternate",
"_official_portrait_2009_crop",
"--Official_113th_Congressional_Portrait--",
"_2013",
"_official_photo_2009_2",
"%2C_official_111th_Congress_photo_portrait",
]:
for suffix2 in [".jpg", "2.jpg", "_2.jpg"]:
infile2 = infile + suffix + suffix2
mvexists(infile2, outfile)
infile2 = infile2.replace(" ", "_")
mvexists(infile2, outfile)
infile2 = infile2.translate(None, "_")
slashpos = infile2.index("/")
infile2 = infile2[0:slashpos+2] + infile2[slashpos+2:].lower()
mvexists(infile2, outfile)
def mvexists(infile, outfile):
if os.path.exists(infile):
cmd = "mv " + infile + " " + outfile
print cmd
os.rename(infile, outfile)
def main():
filename = "legislators.csv"
ifile = open(filename, "rb")
# reader = csv.reader(ifile)
reader = csv.reader(x.replace('\0', '') for x in ifile)
rownum = 0
for rownum,row in enumerate(reader):
# Save header row.
if rownum == 0:
header = row
for colnum, col in enumerate(row):
if re.match("title", col):
title = colnum
elif re.match("firstname", col):
firstname = colnum
elif re.match("middlename", col):
middlename = colnum
elif re.match("nickname", col):
nickname = colnum
elif re.match("lastname", col):
lastname = colnum
elif re.match("bioguide_id", col):
bioguide_id = colnum
else:
outfile = "done/" + row[bioguide_id] + ".jpg"
titletext = row[title]
if titletext == "Del": titletext = "Rep"
# First last
infile = titletext + "/" + row[firstname] + "_" + row[lastname]
mvit(infile, outfile)
infile = titletext + "/" + row[firstname] + row[lastname]
mvit(infile, outfile)
infile = titletext + "/" + row[firstname] + "*" + row[lastname]
mvit(infile, outfile)
# First middle last
infile = titletext + "/" + row[firstname] + "_" + row[middlename] + "_" + row[lastname]
mvit(infile, outfile)
# First initial last
if len(row[middlename]):
infile = titletext + "/" + row[firstname] + "_" + row[middlename][0] + "._" + row[lastname]
mvit(infile, outfile)
infile = titletext + "/" + row[firstname] + "_" + row[middlename][0] + "_" + row[lastname]
mvit(infile, outfile)
# Nickname last
if len(row[nickname]):
infile = titletext + "/" + row[nickname] + "_" + row[lastname]
mvit(infile, outfile)
ifile.close()
if __name__ =="__main__":
main()
# End of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment