User:JarektUploadBot/Upload City of Boston Archives Images.py

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
#!/usr/bin/python
# -*- coding: utf-8  -*-
'''
A program to upload City of Boston Archives images of school buildings
 
'''
import sys, os.path, glob, re, hashlib, base64, StringIO
import wikipedia, upload, csv, urllib2, string
 
def processFile(row):
   # Read line of metadata
   enc='utf-8' 
   metadata = {
    u'name'          : unicode(row.get(u'name')        , enc),
    u'date'          : unicode(row.get(u'date')        , enc),
    u'filename'      : unicode(row.get(u'filename')    , enc),
    u'fname'         : unicode(row.get(u'fname')       , enc),
    u'photographer'  : unicode(row.get(u'photographer'), enc),    
    u'url2'          : unicode(row.get(u'url2')        , enc),
    u'url1'          : unicode(row.get(u'url1')        , enc),
    u'sortkey'       : unicode(row.get(u'sortkey')     , enc),
    u'box'           : unicode(row.get(u'box')         , enc),
    u'folder'        : unicode(row.get(u'folder')      , enc),
    u'address'       : unicode(row.get(u'address')     , enc),
    u'city'          : unicode(row.get(u'city')        , enc),
    u'description'   : unicode(row.get(u'description') , enc),
   }
 
   # Format file description
   template = u"""== {{int:filedesc}} ==
{{Photograph
 |photographer     = %(photographer)s
 |title            = 
 |description      = {{en|%(name)s: %(description)s}}
 |date             = %(date)s
 |medium           = 
 |dimensions       = 
 |institution      = {{Institution:City of Boston Archives}}
 |department       = City of Boston Archives and Records Management Division
 |credit line      = 
 |inscriptions     = 
 |exhibition history = 
 |notes            = 
 |depicted place   = %(address)s, {{city|%(city)s}}
 |references       = 
*[[:File:Guide to the Photographs of School Buildings (City of Boston Archives).pdf|Guide to the Photographs of School Buildings (City of Boston Archives)]]
*The image can also be found on Flickr with other images of this school [%(url1)s here].
 |accession number = 0403.002 (Box %(box)s, Folder %(folder)s)
 |source           = {{City of Boston Archives partnership}}
 |permission       = 
 |other_versions   = 
}}

== {{int:license}} ==
{{City of Boston Archives license (0403.002)}}

[[Category:Historical photographs of public schools in Boston|%(sortkey)s]]
[[Category:%(name)s (Boston, Massachusetts)]]
"""
      
   description = template % metadata
   wikipedia.output( metadata['filename'])
   wikipedia.output(description)
   targetSite = wikipedia.getSite('commons', 'commons')
   # upload file to Commons
   bot = upload.UploadRobot(url=metadata['fname'], 
           description  = description, 
           useFilename  = metadata['filename'], 
           keepFilename = True, 
           verifyDescription=False, 
           targetSite   = targetSite)
   bot.run()


 
def main(args):
   csvFile = 'Boston1.csv'
 
   reader = csv.DictReader(open(csvFile, "rb"), dialect='excel', delimiter=',')
   i=0
   try:
    for row in reader:
        #print 'Row read successfully:', row
        processFile(row)
        i=i+1
        if i==-2:
           return
 
   except csv.Error, e:
     sys.exit('file %s, line %d: %s' % (csvFile, reader.line_num, e))
 
 
if __name__ == "__main__":
   try:
       main(sys.argv[1:])
   finally:
       print "All done!"