view 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
line wrap: on
line source

#!/usr/bin/env python

"""Script to fetch sources defined in sources.txt"""

import ConfigParser
import logging
import os
import subprocess
import urllib
import urllib2
try:
    import pymedia.audio.acodec as acodec
    import pymedia.muxer as muxer
except ImportError, e:
    logging.info("pymedia not installed, will use transcode to convert files: %s", e)
    acodec = None
    muxer = None

def iter_sources(filename=None):
    """fetches a bunch of sound sources from the descriptions in the given filename"""
    if filename is None:
        filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), "sources.txt")
    source_config = ConfigParser.RawConfigParser()
    source_config.read(filename)
    for section in source_config.sections():
        yield section, dict(source_config.items(section))

def convert_audio(source_filename, target_filename, source_format, target_format):
    """converts audio between files"""
    logging.info("Converting %s (format %s) to %s (format %s)", source_filename, source_format, target_filename, target_format)
    if not acodec or not muxer:
        logging.debug("pymedia not present: will try use transcode")
        if source_format == "aiff":
            source_format = "mplayer"
        options = ["-y", "null,%s" % target_format, "-i", source_filename, "-o", target_filename]
        if source_format not in ["wav", "mp3", "ogg"]:
            options += ["-x", "null,%s" % source_format]
        subprocess.call(["transcode"] + options)
        return
    source_file = open(source_filename, 'rb')
    s = source_file.read(8192)
    dm = muxer.Demuxer(source_format)
    frames = dm.parse(s)
    print dm.hasHeader(), dm.getInfo()
    dec = acodec.Decoder(dm.streams[0])
    frame = r[0]
    r = dec.decode(frame[1])
    print r.sample_rate, r.channels, r.bitrate, r.sample_length
    
    params = {
    'id': acodec.getCodecId(target_format),
    'bitrate': r.bitrate,
    'sample_rate': r.sample_rate,
    'ext': target_format,
    'channels': r.channels }
    enc = acodec.Encoder(params)
    enc.setInfo(dec.getInfo())
    
    target_file = open(target_filename, 'wb')
    while len(s):
        frames = enc.encode(r.data)
        target_file.write(enc.mux(frames))
        s = source_file.read(1024)
    
    r = dec.decode(s)
    target_file.write(enc.flush())
    target_file.close()

def handle_logins(config):
    """logs in to necessary sites and returns urllib2 opener with cookies set up"""
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
    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'})
    return opener

if __name__ == "__main__":
    logging.getLogger().setLevel(logging.INFO)
    target_dir = os.path.dirname(os.path.abspath(__file__))
    web_config = ConfigParser.RawConfigParser()
    web_config.read(os.path.join(target_dir, "web.ini"))
    opener = handle_logins(web_config)
    for filename, source_options in iter_sources(os.path.join(target_dir, "sources.txt")):
        download_filename = filename
        orig_ext = source_options.get("originalextension", None)
        if orig_ext:
            target_name, target_ext = os.path.splitext(download_filename)
            download_filename = target_name + "." + orig_ext
            target_ext = target_ext.lstrip(".").lower()
        download_filename = os.path.join(target_dir, download_filename)
        if not os.path.exists(download_filename):
            url = source_options["url"]
            logging.info("Downloading %s to %s", url, download_filename)
            contents = opener.open(url).read()
            if "<html" in contents[:1024].lower():
                logging.error("%s returned HTML rather than file contents", url)
                continue
            f = open(download_filename, "wb")
            f.write(contents)
            f.close()
        filename = os.path.join(target_dir, filename)
        if not os.path.exists(filename):
            orig_format = source_options.get("originalformat", orig_ext)
            target_format = source_options.get("targetformat", target_ext)
            convert_audio(download_filename, filename, orig_format, target_format)