annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
85
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
1 #!/usr/bin/env python
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
2
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
3 """Script to fetch sources defined in sources.txt"""
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
4
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
5 import ConfigParser
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
6 import logging
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
7 import os
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
8 import subprocess
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
9 import urllib
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
10 import urllib2
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
11 try:
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
12 import pymedia.audio.acodec as acodec
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
13 import pymedia.muxer as muxer
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
14 except ImportError, e:
87
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
15 logging.info("pymedia not installed, will use transcode to convert files: %s", e)
85
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
16 acodec = None
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
17 muxer = None
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
18
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
19 def iter_sources(filename=None):
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
20 """fetches a bunch of sound sources from the descriptions in the given filename"""
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
21 if filename is None:
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
22 filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), "sources.txt")
87
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
23 source_config = ConfigParser.RawConfigParser()
85
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
24 source_config.read(filename)
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
25 for section in source_config.sections():
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
26 yield section, dict(source_config.items(section))
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
27
87
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
28 def convert_audio(source_filename, target_filename, source_format, target_format):
85
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
29 """converts audio between files"""
87
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
30 logging.info("Converting %s (format %s) to %s (format %s)", source_filename, source_format, target_filename, target_format)
85
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
31 if not acodec or not muxer:
87
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
32 logging.debug("pymedia not present: will try use transcode")
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
33 if source_format == "aiff":
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
34 source_format = "mplayer"
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
35 options = ["-y", "null,%s" % target_format, "-i", source_filename, "-o", target_filename]
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
36 if source_format not in ["wav", "mp3", "ogg"]:
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
37 options += ["-x", "null,%s" % source_format]
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
38 subprocess.call(["transcode"] + options)
85
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
39 return
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
40 source_file = open(source_filename, 'rb')
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
41 s = source_file.read(8192)
87
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
42 dm = muxer.Demuxer(source_format)
85
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
43 frames = dm.parse(s)
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
44 print dm.hasHeader(), dm.getInfo()
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
45 dec = acodec.Decoder(dm.streams[0])
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
46 frame = r[0]
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
47 r = dec.decode(frame[1])
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
48 print r.sample_rate, r.channels, r.bitrate, r.sample_length
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
49
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
50 params = {
87
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
51 'id': acodec.getCodecId(target_format),
85
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
52 'bitrate': r.bitrate,
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
53 'sample_rate': r.sample_rate,
87
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
54 'ext': target_format,
85
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
55 'channels': r.channels }
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
56 enc = acodec.Encoder(params)
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
57 enc.setInfo(dec.getInfo())
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
58
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
59 target_file = open(target_filename, 'wb')
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
60 while len(s):
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
61 frames = enc.encode(r.data)
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
62 target_file.write(enc.mux(frames))
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
63 s = source_file.read(1024)
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
64
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
65 r = dec.decode(s)
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
66 target_file.write(enc.flush())
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
67 target_file.close()
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
68
88
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
69 def lazy_login(options):
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
70 """performs a lazy login for the given options"""
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
71 if not options.get("lazy", False):
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
72 # this login has already happened
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
73 return
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
74 options["lazy"] = False
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
75 options = options.copy()
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
76 url = options.pop("url")
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
77 params = urllib.urlencode(options)
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
78 logging.info("Logging in to %s", url)
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
79 f = opener.open(url, params)
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
80 contents = f.read()
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
81 f.close()
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
82
85
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
83 def handle_logins(config):
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
84 """logs in to necessary sites and returns urllib2 opener with cookies set up"""
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
85 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
88
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
86 opener.weblogin = {}
85
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
87 urllib2.install_opener(opener)
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
88 for section in config.sections():
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
89 options = dict(config.items(section))
88
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
90 opener.weblogin[section] = options
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
91 opener.weblogin[section]["lazy"] = True
85
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
92 return opener
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
93
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
94 if __name__ == "__main__":
87
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
95 logging.getLogger().setLevel(logging.INFO)
85
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
96 target_dir = os.path.dirname(os.path.abspath(__file__))
87
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
97 web_config = ConfigParser.RawConfigParser()
85
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
98 web_config.read(os.path.join(target_dir, "web.ini"))
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
99 opener = handle_logins(web_config)
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
100 for filename, source_options in iter_sources(os.path.join(target_dir, "sources.txt")):
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
101 download_filename = filename
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
102 orig_ext = source_options.get("originalextension", None)
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
103 if orig_ext:
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
104 target_name, target_ext = os.path.splitext(download_filename)
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
105 download_filename = target_name + "." + orig_ext
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
106 target_ext = target_ext.lstrip(".").lower()
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
107 download_filename = os.path.join(target_dir, download_filename)
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
108 if not os.path.exists(download_filename):
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
109 url = source_options["url"]
88
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
110 if "weblogin" in source_options:
a5ce010f9fb4 Added lazy login capability so we don't login unless downloading
David Fraser <davidf@sjsoft.com>
parents: 87
diff changeset
111 lazy_login(opener.weblogin[source_options["weblogin"]])
87
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
112 logging.info("Downloading %s to %s", url, download_filename)
85
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
113 contents = opener.open(url).read()
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
114 if "<html" in contents[:1024].lower():
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
115 logging.error("%s returned HTML rather than file contents", url)
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
116 continue
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
117 f = open(download_filename, "wb")
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
118 f.write(contents)
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
119 f.close()
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
120 filename = os.path.join(target_dir, filename)
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
121 if not os.path.exists(filename):
87
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
122 orig_format = source_options.get("originalformat", orig_ext)
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
123 target_format = source_options.get("targetformat", target_ext)
615396b21744 Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
David Fraser <davidf@sjsoft.com>
parents: 85
diff changeset
124 convert_audio(download_filename, filename, orig_format, target_format)
85
782e45d70ea8 Adds directory for sounds, and script to download them based on sources.txt (and web.ini, which contains password info for freesound.org) despite licensing disputes
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
125