Byron Clark avatar 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,))
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.