annotate data/sounds/get-sources @ 87:615396b21744

Improved login, added sampleswap-sourced public domain sound, handle aiff conversion, don't parse parameters in config files
author David Fraser <davidf@sjsoft.com>
date Wed, 02 Sep 2009 10:09:34 +0000
parents 782e45d70ea8
children a5ce010f9fb4
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
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
69 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
70 """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
71 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
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
72 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
73 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
74 options = dict(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
75 url = options.pop("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
76 params = urllib.urlencode(options)
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
77 logging.info("Logging in to %s", url)
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
78 f = opener.open(url, 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
79 contents = f.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
80 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
81 # params = urllib.urlencode({'username': 'davidfraser', 'password': 'QwpCAlZe', 'autologin': 'on', 'login': 'login', 'redirect': '../index.php'})
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
82 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
83
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 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
85 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
86 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
87 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
88 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
89 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
90 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
91 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
92 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
93 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
94 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
95 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
96 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
97 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
98 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
99 url = source_options["url"]
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
100 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
101 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
102 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
103 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
104 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
105 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
106 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
107 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
108 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
109 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
110 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
111 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
112 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
113