comparison 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
comparison
equal deleted inserted replaced
86:86d964916fe5 87:615396b21744
10 import urllib2 10 import urllib2
11 try: 11 try:
12 import pymedia.audio.acodec as acodec 12 import pymedia.audio.acodec as acodec
13 import pymedia.muxer as muxer 13 import pymedia.muxer as muxer
14 except ImportError, e: 14 except ImportError, e:
15 logging.warning("pymedia not installed, will use transcode to convert files: %s", e) 15 logging.info("pymedia not installed, will use transcode to convert files: %s", e)
16 acodec = None 16 acodec = None
17 muxer = None 17 muxer = None
18 18
19 def iter_sources(filename=None): 19 def iter_sources(filename=None):
20 """fetches a bunch of sound sources from the descriptions in the given filename""" 20 """fetches a bunch of sound sources from the descriptions in the given filename"""
21 if filename is None: 21 if filename is None:
22 filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), "sources.txt") 22 filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), "sources.txt")
23 source_config = ConfigParser.SafeConfigParser() 23 source_config = ConfigParser.RawConfigParser()
24 source_config.read(filename) 24 source_config.read(filename)
25 for section in source_config.sections(): 25 for section in source_config.sections():
26 yield section, dict(source_config.items(section)) 26 yield section, dict(source_config.items(section))
27 27
28 def convert_audio(source_filename, target_filename, source_ext, target_ext): 28 def convert_audio(source_filename, target_filename, source_format, target_format):
29 """converts audio between files""" 29 """converts audio between files"""
30 logging.info("Converting %s (format %s) to %s (format %s)", source_filename, source_format, target_filename, target_format)
30 if not acodec or not muxer: 31 if not acodec or not muxer:
31 logging.info("Could not convert audio files: will try use transcode") 32 logging.debug("pymedia not present: will try use transcode")
32 subprocess.call(["transcode", "-y", "null,%s" % target_ext, "-i", source_filename, "-o", target_filename]) 33 if source_format == "aiff":
34 source_format = "mplayer"
35 options = ["-y", "null,%s" % target_format, "-i", source_filename, "-o", target_filename]
36 if source_format not in ["wav", "mp3", "ogg"]:
37 options += ["-x", "null,%s" % source_format]
38 subprocess.call(["transcode"] + options)
33 return 39 return
34 logging.info("Converting %s (format %s) to %s (format %s)", source_filename, source_ext, target_filename, target_ext)
35 source_file = open(source_filename, 'rb') 40 source_file = open(source_filename, 'rb')
36 s = source_file.read(8192) 41 s = source_file.read(8192)
37 dm = muxer.Demuxer(source_ext) 42 dm = muxer.Demuxer(source_format)
38 frames = dm.parse(s) 43 frames = dm.parse(s)
39 print dm.hasHeader(), dm.getInfo() 44 print dm.hasHeader(), dm.getInfo()
40 dec = acodec.Decoder(dm.streams[0]) 45 dec = acodec.Decoder(dm.streams[0])
41 frame = r[0] 46 frame = r[0]
42 r = dec.decode(frame[1]) 47 r = dec.decode(frame[1])
43 print r.sample_rate, r.channels, r.bitrate, r.sample_length 48 print r.sample_rate, r.channels, r.bitrate, r.sample_length
44 49
45 params = { 50 params = {
46 'id': acodec.getCodecId(target_ext), 51 'id': acodec.getCodecId(target_format),
47 'bitrate': r.bitrate, 52 'bitrate': r.bitrate,
48 'sample_rate': r.sample_rate, 53 'sample_rate': r.sample_rate,
49 'ext': target_ext, 54 'ext': target_format,
50 'channels': r.channels } 55 'channels': r.channels }
51 enc = acodec.Encoder(params) 56 enc = acodec.Encoder(params)
52 enc.setInfo(dec.getInfo()) 57 enc.setInfo(dec.getInfo())
53 58
54 target_file = open(target_filename, 'wb') 59 target_file = open(target_filename, 'wb')
67 urllib2.install_opener(opener) 72 urllib2.install_opener(opener)
68 for section in config.sections(): 73 for section in config.sections():
69 options = dict(config.items(section)) 74 options = dict(config.items(section))
70 url = options.pop("url") 75 url = options.pop("url")
71 params = urllib.urlencode(options) 76 params = urllib.urlencode(options)
77 logging.info("Logging in to %s", url)
72 f = opener.open(url, params) 78 f = opener.open(url, params)
73 contents = f.read() 79 contents = f.read()
74 f.close() 80 f.close()
75 # params = urllib.urlencode({'username': 'davidfraser', 'password': 'QwpCAlZe', 'autologin': 'on', 'login': 'login', 'redirect': '../index.php'}) 81 # params = urllib.urlencode({'username': 'davidfraser', 'password': 'QwpCAlZe', 'autologin': 'on', 'login': 'login', 'redirect': '../index.php'})
76 return opener 82 return opener
77 83
78 if __name__ == "__main__": 84 if __name__ == "__main__":
85 logging.getLogger().setLevel(logging.INFO)
79 target_dir = os.path.dirname(os.path.abspath(__file__)) 86 target_dir = os.path.dirname(os.path.abspath(__file__))
80 web_config = ConfigParser.SafeConfigParser() 87 web_config = ConfigParser.RawConfigParser()
81 web_config.read(os.path.join(target_dir, "web.ini")) 88 web_config.read(os.path.join(target_dir, "web.ini"))
82 opener = handle_logins(web_config) 89 opener = handle_logins(web_config)
83 for filename, source_options in iter_sources(os.path.join(target_dir, "sources.txt")): 90 for filename, source_options in iter_sources(os.path.join(target_dir, "sources.txt")):
84 download_filename = filename 91 download_filename = filename
85 orig_ext = source_options.get("originalextension", None) 92 orig_ext = source_options.get("originalextension", None)
88 download_filename = target_name + "." + orig_ext 95 download_filename = target_name + "." + orig_ext
89 target_ext = target_ext.lstrip(".").lower() 96 target_ext = target_ext.lstrip(".").lower()
90 download_filename = os.path.join(target_dir, download_filename) 97 download_filename = os.path.join(target_dir, download_filename)
91 if not os.path.exists(download_filename): 98 if not os.path.exists(download_filename):
92 url = source_options["url"] 99 url = source_options["url"]
100 logging.info("Downloading %s to %s", url, download_filename)
93 contents = opener.open(url).read() 101 contents = opener.open(url).read()
94 if "<html" in contents[:1024].lower(): 102 if "<html" in contents[:1024].lower():
95 logging.error("%s returned HTML rather than file contents", url) 103 logging.error("%s returned HTML rather than file contents", url)
96 continue 104 continue
97 f = open(download_filename, "wb") 105 f = open(download_filename, "wb")
98 f.write(contents) 106 f.write(contents)
99 f.close() 107 f.close()
100 filename = os.path.join(target_dir, filename) 108 filename = os.path.join(target_dir, filename)
101 if not os.path.exists(filename): 109 if not os.path.exists(filename):
102 convert_audio(download_filename, filename, orig_ext, target_ext) 110 orig_format = source_options.get("originalformat", orig_ext)
111 target_format = source_options.get("targetformat", target_ext)
112 convert_audio(download_filename, filename, orig_format, target_format)
103 113