From e12fabbf11aaac2074eae9caace2e480a7bb4dcf Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Fri, 15 Oct 2010 18:29:55 +0200 Subject: [PATCH] new application 'radioob' --- scripts/radioob | 25 ++++ weboob/applications/radioob/__init__.py | 21 ++++ weboob/applications/radioob/radioob.py | 153 ++++++++++++++++++++++++ 3 files changed, 199 insertions(+) create mode 100755 scripts/radioob create mode 100644 weboob/applications/radioob/__init__.py create mode 100644 weboob/applications/radioob/radioob.py diff --git a/scripts/radioob b/scripts/radioob new file mode 100755 index 0000000000..315680bb53 --- /dev/null +++ b/scripts/radioob @@ -0,0 +1,25 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# vim: ft=python et softtabstop=4 cinoptions=4 shiftwidth=4 ts=4 ai + +# Copyright(C) 2010 Romain Bignon +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +from weboob.applications.radioob import Radioob + + +if __name__ == '__main__': + Radioob.run() diff --git a/weboob/applications/radioob/__init__.py b/weboob/applications/radioob/__init__.py new file mode 100644 index 0000000000..9f7df168f5 --- /dev/null +++ b/weboob/applications/radioob/__init__.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Romain Bignon +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +from .radioob import Radioob + +__all__ = ['Radioob'] diff --git a/weboob/applications/radioob/radioob.py b/weboob/applications/radioob/radioob.py new file mode 100644 index 0000000000..da408a4dd0 --- /dev/null +++ b/weboob/applications/radioob/radioob.py @@ -0,0 +1,153 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Romain Bignon +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +from weboob.capabilities.radio import ICapRadio +from weboob.capabilities.base import NotLoaded +from weboob.tools.application.repl import ReplApplication +from weboob.tools.application.media_player import MediaPlayer +from weboob.tools.application.formatters.iformatter import IFormatter + + +__all__ = ['Radioob'] + + +class RadioListFormatter(IFormatter): + count = 0 + + def after_format(self, formatted): + print formatted.encode('utf-8') + + def flush(self): + self.count = 0 + pass + + def format_dict(self, item): + self.count += 1 + if self.interactive: + backend = item['id'].split('@', 1)[1] + result = u'%s* (%d) %s (%s)%s\n' % (ReplApplication.BOLD, self.count, item['title'], backend, ReplApplication.NC) + else: + result = u'%s* (%s) %s%s\n' % (ReplApplication.BOLD, item['id'], item['title'], ReplApplication.NC) + result += ' %-30s' % item['description'] + if item['current'] is not NotLoaded: + result += ' (Current: %s - %s)' % (item['current'].artist, item['current'].title) + return result + + def set_header(self, string): + if self.display_header: + print string.encode('utf-8') + +class Radioob(ReplApplication): + APPNAME = 'radioob' + VERSION = '0.3' + COPYRIGHT = 'Copyright(C) 2010 Romain Bignon' + CAPS = ICapRadio + EXTRA_FORMATTERS = {'radio_list': RadioListFormatter} + COMMANDS_FORMATTERS = {'list': 'radio_list'} + + radios = [] + + def __init__(self, *args, **kwargs): + ReplApplication.__init__(self, *args, **kwargs) + try: + self.player = MediaPlayer() + except OSError: + self.player = None + + def _get_radio(self, _id, fields=None): + if self.interactive: + try: + radio = self.radios[int(_id) - 1] + except (IndexError,ValueError): + pass + else: + for backend, radio in self.do('fillobj', radio, fields, backends=[radio.backend]): + if radio: + return radio + _id, backend_name = self.parse_id(_id) + backend_names = (backend_name,) if backend_name is not None else self.enabled_backends + for backend, radio in self.do('get_radio', _id, backends=backend_names): + if radio: + return radio + + def _complete_id(self): + return ['%s@%s' % (radio.id, radio.backend) for radio in self.radios] + + def complete_play(self, text, line, *ignored): + args = line.split(' ') + if len(args) == 2: + return self._complete_id() + + def do_play(self, _id): + """ + play ID + + Play a radio with a found player. + """ + if not _id: + print 'This command takes an argument: %s' % self.get_command_help('play', short=True) + return + + radio = self._get_radio(_id, ['streams']) + if not radio: + print 'Radio not found: ', _id + return + + if self.player: + self.player.play(radio.streams[0]) + else: + print 'No player has been found on this system.' + print 'The URL of this radio is:' + print ' %s' % radio.streams[0].url + + def complete_info(self, text, line, *ignored): + args = line.split(' ') + if len(args) == 2: + return self._complete_id() + + def do_info(self, _id): + """ + info ID + + Get information about a radio. + """ + if not _id: + print 'This command takes an argument: %s' % self.get_command_help('info', short=True) + return + + radio = self._get_radio(_id) + if not radio: + print 'radio not found:', _id + return + self.format(radio) + self.flush() + + def do_list(self, pattern=None): + """ + list [PATTERN] + + List radios matching a PATTERN. + + If PATTERN is not given, this command will list all the radios. + """ + self.set_formatter_header(u'Search pattern: %s' % pattern if pattern else u'All radios') + self.radios = [] + for backend, radio in self.do('iter_radios_search', pattern=pattern): + self.radios.append(radio) + self.format(radio) + self.flush() -- GitLab