def test_configuration(test_folder): config = Configuration(reset=True, folder=str(test_folder), destination=os.path.join(str(test_folder), 'demodata')) config2 = Configuration() assert config.__dict__ == config2.__dict__ assert config.date_pattern == '%d.%m.%Y' assert config.destination == os.path.join(str(test_folder), 'demodata') assert len(config.stations) == 3 for station_id, station in config.stations.items(): assert isinstance(station, Station) assert 'dlf' in config.stations.keys() assert config.stations['dlf'].stream_url == 'http://example.org/dlf' assert config.stations['dlf'].name == 'Deutschlandfunk' assert config.stations['dlf'].logo_url == 'http://example.org/dlf.png' assert config.stations['dlf'].link_url == 'http://example.org/dlf' assert config.stations['dlf'].date_pattern == '%d.%m.%Y %H:%M' assert 'dkultur' in config.stations.keys() assert config.stations[ 'dkultur'].stream_url == 'http://example.org/dkultur' assert config.stations['dkultur'].name == 'dkultur' assert config.stations[ 'dkultur'].logo_url == 'http://example.org/default.png' assert config.stations['dkultur'].link_url == 'http://my.example.org' assert config.stations['dkultur'].date_pattern == '%d.%m.%Y' assert 'wdr2' in config.stations.keys() assert config.stations['wdr2'].stream_url == 'http://example.org/wdr2' assert config.stations['wdr2'].name == 'wdr2' assert config.stations['wdr2'].logo_url == 'http://example.org/wdr2.png' assert config.stations['wdr2'].link_url == 'http://example.org/wdr2' assert config.stations['wdr2'].date_pattern == '%d.%m.%Y' assert len(config.shows) == 3 for show_id, show in config.shows.items(): assert isinstance(show, Show) assert 'nachtradio' in config.shows.keys() show = config.shows['nachtradio'] assert show.logo_url == 'http://example.org/nachtradio.png' assert show.link_url == 'http://example.org/nachtradio' assert show.duration == 3300 assert show.date_pattern == '%d.%m.%Y %H:%M' show = config.shows['news'] # assert show.logo_url == 'http://example.org/nachtradio.png' # assert show.link_url == 'http://example.org/nachtradio' assert show.duration == 300 assert show.date_pattern == '%Y-%m-%d'
def test_add_station(test_folder): configuration = Configuration(reset=True, folder=str(test_folder)) assert configuration.folder == test_folder station = configuration.add_station('me', 'http://example.org/stream', 'Me') assert ['me', 'dkultur', 'dlf', 'wdr2'].sort() == configuration.get_station_ids().sort() station = configuration.stations['me'] assert isinstance(station, Station) assert station.name == 'Me' assert station.id == 'me' assert station.logo_url == 'http://example.org/default.png' assert station.stream_url == 'http://example.org/stream' assert station.shows == []
def config_list(args): """Usage: recorder config list Show program settings. """ config = Configuration() for key in ['destination', 'date_pattern', 'comment_pattern', 'folder', 'filename', 'tempdir']: val = config._shared_state[key] if key == 'comment_pattern': val = val.replace('\n', '\n ') print u"%s: %s" % (key, val) for key, val in config._shared_state['feed'].items(): print u"feed.%s: %s" % (key, val) show_ids = map(lambda id: id.encode('ascii'), config.shows.keys()) station_ids = map(lambda id: id.encode('ascii'), config.stations.keys()) print('stations: %s' % ', '.join(station_ids) if len(station_ids) else 'No stations defined') print('shows: %s' % ', '.join(show_ids) if len(show_ids) else 'No shows defined')
def getrss(self, n=10): items = self.rssitems(n) config = Configuration() image = PyRSS2Gen.Image( url=config.feed['default_logo_url'], title=config.feed['title'], link=config.feed['about_url'], description=u"%s\n\nLogo: %s" % ( config.feed['description'], config.feed['logo_copyright'] ) ) channel = ItunesRSS( title=self.title, link=self.link, description=self.description, language=self.language, generator=self.generator, lastBuildDate=dt.datetime.now(), items=items, image=image ) return channel
def __init__(self, local_path=''): self.log = logging.getLogger('create_podcast_feed.Audiofiles') self.log.debug('Create Audiofiles(%s)' % local_path) self.config = Configuration() feed_title = self.config.feed['title'] if (local_path != ''): feed_title += " - " + local_path.replace('/', ' - ') self.path = local_path self.title = feed_title self.link = self.config.feed['about_url'] self.description = self.config.feed['description'] self.language = self.config.feed['language'] self.urlbase = self.config.feed['base_url'] if local_path is not '': self.urlbase += urllib.quote(local_path) if not self.urlbase.endswith('/'): self.urlbase += '/' self.data = [] self.generator = 'CaptuRadio v%s' % capturadio_version
def feed_update(args): """Usage: recorder feed update Generate rss feed files. """ config = Configuration() path = config.destination for dirname, dirnames, filenames in os.walk(path): if not ignore_folder(dirname): Audiofiles.process_folder(dirname, path)
def config_setup(args): """Usage: recorder config setup [ -u | -p ] Setup program settings. A new settings file is created. Options: -u Create user settings in ~/.capturadio -p Crete local settings in current work directory """ config = Configuration() bbcradio2 = config.add_station( "bbc2", "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio2_mf_p?s=1423688987&e=1423703387&h=1f97ff4f9b0e0f1ae988bdf684f233b3", "BBC Radio 2", "http://www.bbc.co.uk/radio2/images/homepage/bbcradio2.gif", ) bobharriscountry = config.add_show(bbcradio2, "bobharriscountry", "Bob Harris Country", parse_duration("58m")) bobharriscountry.link_url = "http://www.bbc.co.uk/programmes/b006x527" bobharrissunday = config.add_show(bbcradio2, "bobharrissunday", "Bob Harris Sunday", parse_duration("2h57m")) bobharrissunday.link_url = "http://www.bbc.co.uk/programmes/b006wqtf" config.write_config() print( """ Created a new configuration at %(filename)s. The URL is set to %(url)s and the files are stored at %(destination)s. You can change all settings by editing %(filename)s and you are engcouraged to do that. Use the command 'config list' to see all settings.""" % {"destination": config.destination, "filename": config.filename, "url": config.feed["base_url"]} )
def test_add_show_to_station(test_folder): config = Configuration(reset=True, folder=str(test_folder)) station = config.stations['dlf'] show = config.add_show(config, station, 'news', 'Latest News', 10) assert isinstance(show, Show) assert len(config.shows) == 3 for show_id, show in config.shows.items(): assert isinstance(show, Show) assert config.shows['news'].name == 'Latest News' assert config.shows['news'].logo_url == 'http://example.org/dlf.png' assert config.shows['news'].station == station assert config.shows['news'].duration == 10
def main(argv=None): """ capturadio - Capture internet radio broadcasts in mp3 encoding format. Usage: recorder.py help <command> <action> recorder.py show capture <show> recorder.py config list recorder.py config setup recorder.py feed update General Options: -h, --help show this screen and exit --version Show version and exit. Commands: show capture Capture an episode of a show config setup Create configuration file config list Show configuration values feed update Update rss feed files See 'recorder.py help <command>' for more information on a specific command.""" args = docopt( main.__doc__, version=version_string, options_first=True, argv=argv or sys.argv[1:] ) if len(sys.argv) == 1: sys.argv.append('--help') config_location = find_configuration() Configuration.folder = os.path.dirname(config_location) Configuration.filename = os.path.basename(config_location) if not os.path.exists(config_location): Configuration().write_config() try: cmd = find_command(args) method = globals()[cmd] assert callable(method) except (KeyError, AssertionError): exit("%r is not a valid command. See 'recorder help'." % cmd.replace('_', ' ')) try: method(args) except RuntimeError as e: exit("ERROR: {}".format(e.message))
def __init__(self, filename): self.filename = filename self.log = logging.getLogger('create_podcast_feed.Audiofile') self.config = Configuration() self.path = filename self.filesize = str(os.path.getsize(filename)) self.basename = os.path.basename(filename) local_path = self.path.replace(self.config.destination, '') self.url = self.config.feed['base_url'] + url_fix(local_path) try: audio = MP3(self.path) self.extract_metadata(audio) except HeaderNotFoundError: self.log.error("Couldn't find MPEG header in file %r" % self.path) self.size = os.path.getsize(self.path) self.pubdate = dt.datetime.fromtimestamp(os.path.getmtime(self.path))
def _create_image_tag(self, rssitem): logo_url = self._get_logo_url(rssitem.author) link_url = self._get_link_url(rssitem.author) config = Configuration() if logo_url is not None: return PyRSS2Gen.Image( url=logo_url, title=rssitem.author, link=link_url ) else: return PyRSS2Gen.Image( url=config.feed['default_logo_url'], title=rssitem.author, link=link_url )
def config_setup(args): """Usage: recorder config setup [ -u | -p ] Setup program settings. A new settings file is created. Options: -u Create user settings in ~/.capturadio -p Crete local settings in current work directory """ config = Configuration() bbcradio2 = config.add_station( 'bbc2', 'http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio2_mf_p?s=1423688987&e=1423703387&h=1f97ff4f9b0e0f1ae988bdf684f233b3', 'BBC Radio 2', 'http://www.bbc.co.uk/radio2/images/homepage/bbcradio2.gif' ) bobharriscountry = config.add_show( bbcradio2, 'bobharriscountry', 'Bob Harris Country', parse_duration('58m'), ) bobharriscountry.link_url = 'http://www.bbc.co.uk/programmes/b006x527' bobharrissunday = config.add_show( bbcradio2, 'bobharrissunday', 'Bob Harris Sunday', parse_duration('2h57m'), ) bobharrissunday.link_url = 'http://www.bbc.co.uk/programmes/b006wqtf' config.write_config() print """ Created a new configuration at %(filename)s. The URL is set to %(url)s and the files are stored at %(destination)s. You can change all settings by editing %(filename)s and you are engcouraged to do that. Use the command 'config list' to see all settings.""" % { 'destination': config.destination, 'filename': config.filename, 'url': config.feed['base_url'], }
def show_capture(*args): """Usage: recorder show capture [--duration=<duration>] [options] Capture a show. Options: --duration,-d=<duration> Set the duration, overrides show setting Examples: 1. Capture an episode of the show 'nighttalk' recorder show capture nighttalk 2. Capture an episode of the show 'nighttalk', but only 35 minutes recorder show capture nighttalk -d 35m """ config = Configuration() if len(config.stations) == 0: print('No stations defined, add stations at first!') sys.exit(0) if len(config.shows) == 0: print('No shows defined, add shows at first!') sys.exit(0) args = args[0] if args['<show>'] in config.shows: show = config.shows[args['<show>']] try: recorder = Recorder() recorder.capture(show) except Exception as e: print('Unable to capture recording: %s' % e) else: print('Unknown show %r' % args['<show>'])
def test_old_style_configuration(test_folder): test_folder.join('capturadiorc.oldstyle').write('''[settings] destination = {0}/demodata date_pattern = %d.%m.%Y %H:%M [stations] dlf = http://example.org/dlf dkultur = http://example.org/dkultur wdr2 = http://example.org/wdr2 ; settings for the Podcast feed [feed] url = http://my.example.org title = Internet Radio Recordings about_url = http://my.example.org/about.html description = Recordings language = en filename = rss.xml default_logo_url = http://example.org/default.png default_logo_copyright = A Creative Commons license ; additional settings for station 'dlf' [dlf] name = Deutschlandfunk link_url = http://example.org/dlf logo_url = http://example.org/dlf.png shows = nachtradio weather ; Settings for the show "Nachtradio" on station "dlf" [nachtradio] title = Nachtradio duration = 3300 link_url = http://example.org/nachtradio logo_url = http://example.org/nachtradio.png [weather] title = Weather forecast duration = 300 logo_url = http://example.org/weather.png [wdr2] title = WDR 2 link_url = http://example.org/wdr2 logo_url = http://example.org/wdr2.png shows = news [news] title = Latest news duration = 300 link_url = http://example.org/wdr2/news logo_url = http://example.org/wdr2/news.png '''.format(str(test_folder))) test_folder.join('capturadiorc.newstyle').write('''[settings] destination = {0}/demodata date_pattern = %d.%m.%Y %H:%M [stations] dlf = http://example.org/dlf dkultur = http://example.org/dkultur wdr2 = http://example.org/wdr2 [feed] title = Internet Radio Recordings about_url = http://my.example.org/about.html description = Recordings language = en filename = rss.xml default_logo_url = http://example.org/default.png default_logo_copyright = A Creative Commons license base_url = http://my.example.org [dlf] name = Deutschlandfunk link_url = http://example.org/dlf logo_url = http://example.org/dlf.png [nachtradio] title = Nachtradio duration = 3300 link_url = http://example.org/nachtradio logo_url = http://example.org/nachtradio.png station = dlf [weather] title = Weather forecast duration = 300 logo_url = http://example.org/weather.png station = dlf [wdr2] title = WDR 2 link_url = http://example.org/wdr2 logo_url = http://example.org/wdr2.png [news] title = Latest news duration = 300 link_url = http://example.org/wdr2/news logo_url = http://example.org/wdr2/news.png station = wdr2 '''.format(str(test_folder))) configuration = Configuration(folder=str(test_folder), filename='capturadiorc.oldstyle', reset=True) assert configuration.filename == os.path.join(configuration.folder, 'capturadiorc.oldstyle') assert Configuration.changed_settings is True assert os.path.exists(configuration.filename) assert os.path.exists(configuration.filename + '.bak') import filecmp assert filecmp.cmp( os.path.join(configuration.folder, 'capturadiorc.oldstyle'), os.path.join(configuration.folder, 'capturadiorc.newstyle'))
def test_station_ids(test_folder): config = Configuration(reset=True, folder=str(test_folder)) assert ['dkultur', 'dlf', 'wdr2'].sort() == list(config.get_station_ids()).sort()
def test_change_destination(test_folder): config = Configuration(reset=True, folder=str(test_folder)) new_folder = str(test_folder.mkdir('destination')) config.set_destination(new_folder) assert config.destination == new_folder
def config(request, test_folder): from capturadio import Configuration __config = Configuration(reset = True, folder=str(test_folder)) return __config