1. Byron Clark
  2. comicbox


Byron Clark  committed e83283c

Add a useragent option.

  • Participants
  • Parent commits 85f76ce
  • Branches default

Comments (0)

Files changed (4)

File bin/comicbox

View file
     parser = optparse.OptionParser()
     parser.add_option('-f', '--file', dest='filename',
                       help='alternate configuration FILE', metavar='FILE')
+    parser.add_option('-u', '--user-agent', dest='useragent',
+                      help='USERAGENT to use for http requests', metavar='USERAGENT')
     (options, args) = parser.parse_args()
     import comicbox.configuration
         logger.fatal('unable to load configuration: %s' % (err,))
+    if options.useragent:
+        import comicbox.globals
+        comicbox.globals.USERAGENT=options.useragent
     backend_name, backend_args, strips = conf
     # backend_name should look like 'file_archive', need to find the class

File comicbox/download.py

View file
 from request_builder import build_request
-def fetch_strips(strips, backend, date):
+def fetch_strips(strips, backend, date, useragent=None):
     logger = logging.getLogger('comicbox.fetch_strips')
     for s in strips:
                 filename = None
                 if 'strip_referer' in dir(s):
-                    req = build_request(url, s.strip_referer())
+                    req = build_request(url, s.strip_referer(), useragent=useragent)
-                    req = build_request(url)
+                    req = build_request(url, useragent=useragent)
                 image = urllib2.urlopen(req).read()
                 backend.add_strip(s.name(), s.home(), image, url, filename)
             except urllib2.URLError, e:

File comicbox/globals.py

View file
 VERSION = '0.1'
 DEFAULT_BACKEND = 'file_archive'

File comicbox/request_builder.py

View file
 import globals
-def build_request(url, referer=None):
+def build_request(url, referer=None, useragent=None):
     """Builds a urllib2.Request object.
     Builds a urllib2.Request object for the given url and referer.  While
     if referer:
         req.add_header('Referer', referer)
-    req.add_header('User-agent', '%s/%s' % (globals.NAME, globals.VERSION))
+    if useragent:
+        req.add_header('User-agent', useragent)
+    else:
+        req.add_header('User-agent', globals.USERAGENT)
     return req