diff data/sounds/get-sources @ 88:a5ce010f9fb4

Added lazy login capability so we don't login unless downloading
author David Fraser <davidf@sjsoft.com>
date Wed, 02 Sep 2009 10:14:51 +0000
parents 615396b21744
children 1fd56b625b24
line wrap: on
line diff
--- a/data/sounds/get-sources	Wed Sep 02 10:09:34 2009 +0000
+++ b/data/sounds/get-sources	Wed Sep 02 10:14:51 2009 +0000
@@ -66,19 +66,29 @@
     target_file.write(enc.flush())
     target_file.close()
 
+def lazy_login(options):
+    """performs a lazy login for the given options"""
+    if not options.get("lazy", False):
+        # this login has already happened
+        return
+    options["lazy"] = False
+    options = options.copy()
+    url = options.pop("url")
+    params = urllib.urlencode(options)
+    logging.info("Logging in to %s", url)
+    f = opener.open(url, params)
+    contents = f.read()
+    f.close()
+
 def handle_logins(config):
     """logs in to necessary sites and returns urllib2 opener with cookies set up"""
     opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
+    opener.weblogin = {}
     urllib2.install_opener(opener)
     for section in config.sections():
         options = dict(config.items(section))
-        url = options.pop("url")
-        params = urllib.urlencode(options)
-        logging.info("Logging in to %s", url)
-        f = opener.open(url, params)
-        contents = f.read()
-        f.close()
-    # params = urllib.urlencode({'username': 'davidfraser', 'password': 'QwpCAlZe', 'autologin': 'on', 'login': 'login', 'redirect': '../index.php'})
+        opener.weblogin[section] = options
+        opener.weblogin[section]["lazy"] = True
     return opener
 
 if __name__ == "__main__":
@@ -97,6 +107,8 @@
         download_filename = os.path.join(target_dir, download_filename)
         if not os.path.exists(download_filename):
             url = source_options["url"]
+            if "weblogin" in source_options:
+                lazy_login(opener.weblogin[source_options["weblogin"]])
             logging.info("Downloading %s to %s", url, download_filename)
             contents = opener.open(url).read()
             if "<html" in contents[:1024].lower():