Byron Clark avatar Byron Clark committed e83283c

Add a useragent option.

Comments (0)

Files changed (4)

     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,))
         sys.exit(1)
 
+    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

comicbox/download.py

 
 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:
         time.sleep(0.01)
                 filename = None
             try:
                 if 'strip_referer' in dir(s):
-                    req = build_request(url, s.strip_referer())
+                    req = build_request(url, s.strip_referer(), useragent=useragent)
                 else:
-                    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:

comicbox/globals.py

 VERSION = '0.1'
 
 DEFAULT_BACKEND = 'file_archive'
+
+USERAGENT = '%s/%s' % (NAME, VERSION)

comicbox/request_builder.py

 
 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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.