User:DieBuche/checkThumbs.py

From Wikimedia Commons, the free media repository
Jump to: navigation, search
import httplib
import sys
import urllib
import logging
import time
LOG_FILENAME = '/pywikipedia/logs/CheckSVG.log'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)


#open file
conn = httplib.HTTPConnection('commons.wikimedia.org')
headers = {'User-Agent': 'User:DieBuche at commons.wiki'}


i=0
j=1
with open(sys.argv[1]) as f:
        for line in f:
                if i==100:
                        print "At line: " + str(i*j)
                        j+=1
                        i=0
                params = urllib.urlencode({})

                line = line.strip().strip('"')
                try:
                        conn.request('HEAD', '/w/thumb.php?f='+  urllib.quote_plus(line) +'&width=120', params, headers)
                        res = conn.getresponse()
                except socket.error:
                        print 'Connection error:' + line
                        logging.debug('Connection error:' + line)
                if res.status == 500:
                        print 'ERROR at:' + line
                        logging.debug(line)
                elif res.status == 200:
                        print 'OK: ' + line
                else:
                        print 'Unexepected:' + line +' returned '+str(res.status)
                        logging.debug('Unexepected:' + line +' returned '+str(res.status))
                #time.sleep(0.1)
                i+=1
conn.close()