changeset 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 c0455e6c99f4
files data/sounds/get-sources data/sounds/sources.txt
diffstat 2 files changed, 23 insertions(+), 7 deletions(-) [+]
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():
--- a/data/sounds/sources.txt	Wed Sep 02 10:09:34 2009 +0000
+++ b/data/sounds/sources.txt	Wed Sep 02 10:14:51 2009 +0000
@@ -5,6 +5,7 @@
 Credit: Robinhood76 (http://www.freesound.org/usersViewSingle.php?id=321967)
 Date: 2009-08-02
 OriginalExtension: wav
+WebLogin: freesound
 
 [chicken2.ogg]
 URL: http://www.freesound.org/download/75726/75726_Robinhood76_01130_chicken_hen_2.wav
@@ -13,10 +14,13 @@
 Credit: Robinhood76 (http://www.freesound.org/usersViewSingle.php?id=321967)
 Date: 2009-07-17
 OriginalExtension: wav
+WebLogin: freesound
 
 [chicken3.ogg]
 URL: http://www.sampleswap.org/SAMPLESWAP/SFX%20AND%20UNUSUAL%20SOUNDS/sfx%20cheesy%20lo-fi/Chicken.aif
 Source: http://www.sampleswap.org/filebrowser-new.php?d=SFX+AND+UNUSUAL+SOUNDS%2Fsfx+cheesy+lo-fi%2F
 License: Public Domain
 OriginalExtension: aiff
+WebLogin: sampleswap
 
+