CLI: Add gpodder.net search and toplist support

Add two new commands to the CLI: "search" and "toplist"
This commit is contained in:
Thomas Perl 2012-01-22 16:35:33 +01:00
parent dbad0bf8b9
commit 75167cf0e1
2 changed files with 91 additions and 5 deletions

80
bin/gpo
View File

@ -30,6 +30,9 @@
- Subscription management -
subscribe URL [TITLE] Subscribe to a new feed at URL (as TITLE)
search QUERY Search the gpodder.net directory for QUERY
toplist Show the gpodder.net top-subscribe podcasts
rename URL TITLE Rename feed at URL to TITLE
unsubscribe URL Unsubscribe from feed at URL
enable URL Enable feed updates for the feed at URL
@ -47,8 +50,8 @@
- Other commands -
youtube [URL] Resolve the YouTube URL to a download URL
rewrite [OLDURL] [NEWURL] Change the feed URL of [OLDURL] to [NEWURL]
youtube URL Resolve the YouTube URL to a download URL
rewrite OLDURL NEWURL Change the feed URL of [OLDURL] to [NEWURL]
webui [public] Start gPodder's Web UI server
(public = listen on all network interfaces)
@ -112,11 +115,13 @@ _ = gpodder.gettext
# Platform detection (i.e. Maemo 5, etc..)
gpodder.detect_platform()
# Use only the gPodder API here, so this serves both as an example
# and as a motivation to provide all functionality in the API :)
from gpodder import api
from gpodder import my
from gpodder import util
have_ansi = sys.stdout.isatty() and not gpodder.win32
interactive_console = sys.stdin.isatty() and sys.stdout.isatty()
is_single_command = False
def inred(x):
if have_ansi:
@ -232,6 +237,11 @@ class gPodderCli(object):
# -------------------------------------------------------------------
def subscribe(self, url, title=None):
url = util.normalize_feed_url(url)
if url is None:
self._error(_('Invalid URL.'))
return True
if self.client.get_podcast(url) is not None:
self._info(_('You are already subscribed to %s.' % url))
return True
@ -430,6 +440,63 @@ class gPodderCli(object):
else:
webui.main()
def search(self, *terms):
query = ' '.join(terms)
if not query:
return
directory = my.Directory()
results = directory.search(query)
self._show_directory_results(results)
def toplist(self):
directory = my.Directory()
results = directory.toplist()
self._show_directory_results(results, True)
def _show_directory_results(self, results, multiple=False):
if not results:
self._error(_('No podcasts found.'))
return
if not interactive_console or is_single_command:
print '\n'.join(url for title, url in results)
return
def show_list():
self._pager('\n'.join(u'%3d: %s\n %s' %
(index+1, title, url if title != url else '')
for index, (title, url) in enumerate(results)))
show_list()
msg = _('Enter index to subscribe, ? for list')
while True:
index = raw_input(msg + ': ')
if not index:
return
if index == '?':
show_list()
continue
try:
index = int(index)
except ValueError:
self._error(_('Invalid value.'))
continue
if not (1 <= index <= len(results)):
self._error(_('Invalid value.'))
continue
title, url = results[index-1]
self._info(_('Adding %s...') % title)
self.subscribe(url)
if not multiple:
break
@FirstArgumentIsPodcastURL
def rewrite(self, old_url, new_url):
podcast = self.client.get_podcast(old_url)
@ -494,6 +561,8 @@ class gPodderCli(object):
self._parse(shlex.split(line))
except KeyboardInterrupt:
self._error('Keyboard interrupt.')
except EOFError:
self._error('EOF.')
self._atexit()
@ -595,8 +664,9 @@ if __name__ == '__main__':
cli = gPodderCli()
args = sys.argv[1:]
if args:
is_single_command = True
cli._parse_single(args)
elif sys.stdin.isatty() and sys.stdout.isatty():
elif interactive_console:
cli._shell()
else:
sys.stdout.write(__doc__)

View File

@ -59,6 +59,7 @@ if not hasattr(mygpoclient, 'require_version') or \
sys.exit(1)
from mygpoclient import api
from mygpoclient import public
from mygpoclient import util as mygpoutil
@ -588,3 +589,18 @@ class MygPoClient(object):
def open_website(self):
util.open_website('http://' + self._config.mygpo_server)
class Directory(object):
def __init__(self):
self.client = public.PublicClient()
def toplist(self):
return [(p.title or p.url, p.url)
for p in self.client.get_toplist()
if p.url]
def search(self, query):
return [(p.title or p.url, p.url)
for p in self.client.search_podcasts(query)
if p.url]