Commits

Byron Clark committed ea61fd7

Add a configurable random sleep time.

Comments (0)

Files changed (2)

                       help='alternate configuration FILE', metavar='FILE')
     parser.add_option('-u', '--user-agent', dest='useragent',
                       help='USERAGENT to use for http requests', metavar='USERAGENT')
+    parser.add_option('-s', '--sleep', dest='sleep',
+                      help='sleep up to SECONDS between requests', metavar='SECONDS',
+                      type='int', default='0')
     (options, args) = parser.parse_args()
 
     import comicbox.configuration
 
     today = datetime.datetime.now()
     b = backend(today, backend_args)
-    fetch_strips(strips, b, today)
+    fetch_strips(strips, b, today, sleep=options.sleep)
     b.finish()

comicbox/download.py

 import logging
+import random
 import time
 import urllib2
 
 from request_builder import build_request
 
-def fetch_strips(strips, backend, date, useragent=None):
+def fetch_strips(strips, backend, date, useragent=None, sleep=0):
     logger = logging.getLogger('comicbox.fetch_strips')
     for s in strips:
-        time.sleep(0.01)
+        if sleep:
+            sleep_time = random.random() * sleep
+            logger.debug('sleeping for %f seconds' % (sleep_time,))
+            time.sleep(sleep_time)
         url = s.strip_url(date)
         if len(url) > 0:
             logger.debug('fetching "%s"' % (url,))