#!/usr/bin/python

from BeautifulSoup import BeautifulSoup
import re

import optparse
import sys
from datetime import datetime

import mechanize
import cookielib

# grab arguments

parser = optparse.OptionParser(description='Download comics')

parser.add_option('-c', metavar='comic_name',
                   help='Short name for comic')

parser.add_option('-t', metavar='comic_type',
                   help='Type of comic (normal or vintage)', 
                   default="normal")

parser.add_option('-d', metavar='comic_date',
                   help='Comic date (YYYYMMDD). Defaults to current date.', 
                   default=datetime.today().strftime('%Y%m%d'))

parser.add_option('-o', metavar='output_directory',
                   help='Output directory')
                                                  
(options, args) = parser.parse_args()
comic_name = options.c
comic_type = options.t
comic_date = options.d
output_directory = options.o

if not comic_name:
    print "Comic name (-c) required"
    sys.exit(1)

if not output_directory:
    print "Output directory (-o) required"
    sys.exit(1)    
# Browser
br = mechanize.Browser()

# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(False)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)

# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

br.addheaders = [('User-agent', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.7; en-US; rv:1.9.2.22) Gecko/20110902 Firefox/3.6.22'), ('Referer', 'http://dailyink.com/features/Beetle_Bailey')]

data = {
    'normal' : {'bbailey' : '42',
              'babyblues' : '22',
              'hagar' : '1302',
              'bizarro' : '72',
              'blondie' : '82',
              'buckles' : '752',
              'hinlois' : '242',
              'lockhorns' : '292',
              'mfillmore' : '312',
              'mothergoose' : '1292',
              'mutts' : '1572',
              'phantom' : '1692',
              'shermanslagoon' : '1442',
              'shoe' : '1852',
              'zits' : '642',
              },

    'vintage' : {'bbailey' : '232',
                 'phantom' : '332',
                 'phantomsunday' : '442',
                 'officehours' : '322',
              }
}

try:
    comic_id = data[comic_type][comic_name]
except KeyError:
    print "Key %s of type %s not found" % (comic_name, comic_type)
    sys.exit(1)

if comic_type == 'vintage':
    js_url = "http://safr.kingfeatures.com/idn/di/series/js/index.php?cn=32&zn=62&sn=%s&wt=0&fs=0&fd=%s&null=0" % (comic_id, comic_date)
else:
    js_url = "http://safr.kingfeatures.com/idn/di/zone/js/index.php?cn=32&zn=62&fn=%s&wt=0&fs=0&fd=%s&null=0" % (comic_id, comic_date) 

script_request = br.open(js_url)

js = script_request.read()

if comic_type == 'vintage':
    date_regex = re.compile("Original\s*Publication\s*Date\s*:\s*.*?,\s*([A-Za-z]+\s*\d{1,2},\s*\d{4})")
    date_match = date_regex.search(js)
    if date_match:
        date_output = datetime.strptime(date_match.group(1), '%B %d, %Y').strftime('%Y%m%d')
    else:
        date_output = '_' + comic_date
else:
    date_output = comic_date
    
img_src_regex = re.compile("src\s*=\s*'(.*?)'")

img_match = img_src_regex.search(js)
img_url = img_match.group(1)

img_response = br.open(img_url)
img = img_response.read()

content_type = img_response.info().getheader('Content-Type')

if content_type == 'image/gif':
    extension = 'gif'
elif content_type == 'image/png':
    extension = 'png'
elif content_type == 'image/jpeg':
    extension = 'jpg'
else:
    extension = 'wtf'

output_file = "%s/%s-%s.%s" % (output_directory, comic_name, date_output, extension)

f=open(output_file, 'w')
f.write(img)
f.close()

downloaded_bytes = len(img)
content_length = int(img_response.info().getheader('content-length'))

if downloaded_bytes != content_length:
    print "%s ERROR Content-Length %d vs Downloaded Bytes %d" % (comic_name, content_length, downloaded_bytes)
else:
    print "%s OK" % output_file
