328 lines
10 KiB
Python
Executable file
328 lines
10 KiB
Python
Executable file
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
# gPodder - A media aggregator and podcast client
|
|
# Copyright (c) 2005-2010 Thomas Perl and the gPodder Team
|
|
#
|
|
# gPodder 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; either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# gPodder 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, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
|
|
|
|
# gpo - A better command-line interface to gPodder using the gPodder API
|
|
# by Thomas Perl <thp@gpodder.org>; 2009-05-07
|
|
|
|
|
|
"""
|
|
Usage: gpo [COMMAND] [params...]
|
|
|
|
Subscription management
|
|
-----------------------
|
|
|
|
subscribe URL [TITLE] Subscribe to a new feed at URL (as TITLE)
|
|
rename URL TITLE Rename feed at URL to TITLE
|
|
unsubscribe URL Unsubscribe from feed at URL
|
|
|
|
info URL Show information about feed at URL
|
|
list List all subscribed podcasts
|
|
update Refresh all feeds (check for new episodes)
|
|
update URL Refresh the feed at URL
|
|
|
|
Episode management
|
|
------------------
|
|
|
|
download [URL] Download all new (=pending) episodes; if
|
|
URL is an episode URL, download only one
|
|
episode; if it's a channel URL, download
|
|
all pending episodes from that channel
|
|
pending [URL] Show episodes that are marked as new; if
|
|
URL is given, show only pending downloads
|
|
from the channel at this URL
|
|
queue URL Add episode at URL to pending episodes
|
|
skip URL Remove episode at URL from pending episodes
|
|
|
|
details URL Show information about episode at URL
|
|
episodes [URL] Show a list of all episodes; if URL is given
|
|
it should be a channel URL and only episodes
|
|
from that channel will be displayed
|
|
|
|
delete URL Delete the downloaded episode at URL
|
|
|
|
disable URL Mark the feed as disabled
|
|
enable URL Mark the feed as enabled
|
|
|
|
Portable device synchronization
|
|
-------------------------------
|
|
|
|
device Show information about your device
|
|
sync Synchronize downloaded episodes to device
|
|
|
|
Helper commands
|
|
---------------
|
|
|
|
youtube resolve [URL] Resolve the YouTube URL to a download URL
|
|
youtube download [URL] Download a video from YouTube via its URL
|
|
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
import inspect
|
|
|
|
gpodder_script = sys.argv[0]
|
|
if os.path.islink(gpodder_script):
|
|
gpodder_script = os.readlink(gpodder_script)
|
|
gpodder_dir = os.path.join(os.path.dirname(gpodder_script), '..')
|
|
prefix = os.path.abspath(os.path.normpath(gpodder_dir))
|
|
|
|
src_dir = os.path.join(prefix, 'src')
|
|
data_dir = os.path.join(prefix, 'data')
|
|
|
|
if os.path.exists(src_dir) and os.path.exists(data_dir) and \
|
|
not prefix.startswith('/usr'):
|
|
# Run gPodder from local source folder (not installed)
|
|
sys.path.insert(0, src_dir)
|
|
|
|
|
|
import gpodder
|
|
_ = gpodder.gettext
|
|
|
|
# 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
|
|
|
|
|
|
|
|
class gPodderCli(object):
|
|
def __init__(self):
|
|
self.client = api.PodcastClient()
|
|
|
|
# -------------------------------------------------------------------
|
|
|
|
def subscribe(self, url, title=None):
|
|
if self.client.get_podcast(url) is not None:
|
|
self._info(_('You are already subscribed to %s.' % url))
|
|
return True
|
|
|
|
if self.client.create_podcast(url, title) is None:
|
|
self._error(_('Cannot download feed for %s.') % url)
|
|
return True
|
|
|
|
self.client.finish()
|
|
|
|
self._info(_('Successfully added %s.' % url))
|
|
return True
|
|
|
|
def rename(self, url, title):
|
|
podcast = self.client.get_podcast(url)
|
|
|
|
if podcast is None:
|
|
self._error(_('You are not subscribed to %s.') % url)
|
|
else:
|
|
old_title = podcast.title
|
|
podcast.rename(title)
|
|
self.client.finish()
|
|
self._info(_('Renamed %s to %s.') % (old_title, title))
|
|
|
|
return True
|
|
|
|
def unsubscribe(self, url):
|
|
podcast = self.client.get_podcast(url)
|
|
|
|
if podcast is None:
|
|
self._error(_('You are not subscribed to %s.') % url)
|
|
else:
|
|
podcast.delete()
|
|
self.client.finish()
|
|
self._error(_('Unsubscribed from %s.') % url)
|
|
|
|
return True
|
|
|
|
def _episodesList(self, podcast):
|
|
def status_str(episode):
|
|
if episode.is_new:
|
|
return ' * '
|
|
if episode.is_downloaded:
|
|
return ' ▉ '
|
|
if episode.is_deleted:
|
|
return ' ░ '
|
|
|
|
return ' '
|
|
|
|
episodes = ('%3d. %s %s' % (i+1, status_str(e), e.title) for i, e in enumerate(podcast.get_episodes()))
|
|
return episodes
|
|
|
|
def info(self, url):
|
|
podcast = self.client.get_podcast(url)
|
|
|
|
if podcast is None:
|
|
self._error(_('You are not subscribed to %s.') % url)
|
|
else:
|
|
title, url, status = podcast.title, podcast.url, podcast.feed_update_status_msg()
|
|
episodes = self._episodesList(podcast)
|
|
episodes = '\n '.join(episodes)
|
|
print >>sys.stdout, """
|
|
Title: %(title)s
|
|
URL: %(url)s
|
|
Feed update is %(status)s
|
|
|
|
Episodes:
|
|
%(episodes)s
|
|
""" % locals()
|
|
|
|
return True
|
|
|
|
def episodes(self, url=None):
|
|
for podcast in self.client.get_podcasts():
|
|
podcast_printed = False
|
|
if url is None or podcast.url == url:
|
|
episodes = self._episodesList(podcast)
|
|
episodes = '\n '.join(episodes)
|
|
print >>sys.stdout, """
|
|
Episodes from %s:
|
|
%s
|
|
""" % (podcast.url, episodes)
|
|
return True
|
|
|
|
def list(self):
|
|
for podcast in self.client.get_podcasts():
|
|
print podcast.url
|
|
|
|
return True
|
|
|
|
def update(self, url=None):
|
|
for podcast in self.client.get_podcasts():
|
|
if url is None and podcast.update_enabled():
|
|
print 'Updating', podcast.title
|
|
podcast.update()
|
|
print 'Done.'
|
|
elif podcast.url == url:
|
|
# Don't need to check for update_enabled()
|
|
print 'Updating', podcast.title
|
|
podcast.update()
|
|
print 'Done.'
|
|
|
|
return True
|
|
|
|
def pending(self, url=None):
|
|
count = 0
|
|
for podcast in self.client.get_podcasts():
|
|
podcast_printed = False
|
|
if url is None or podcast.url == url:
|
|
for episode in podcast.get_episodes():
|
|
if episode.is_new:
|
|
if not podcast_printed:
|
|
print podcast.title
|
|
podcast_printed = True
|
|
print ' ', episode.title
|
|
count += 1
|
|
|
|
print count, 'episodes pending.'
|
|
return True
|
|
|
|
def download(self, url=None):
|
|
count = 0
|
|
for podcast in self.client.get_podcasts():
|
|
podcast_printed = False
|
|
if url is None or podcast.url == url:
|
|
for episode in podcast.get_episodes():
|
|
if episode.is_new:
|
|
if not podcast_printed:
|
|
print podcast.title
|
|
podcast_printed = True
|
|
print ' ', episode.title
|
|
episode.download()
|
|
count += 1
|
|
|
|
print count, 'episodes downloaded.'
|
|
return True
|
|
|
|
def disable(self, url):
|
|
podcast = self.client.get_podcast(url)
|
|
|
|
if podcast is None:
|
|
self._error(_('You are not subscribed to %s.') % url)
|
|
else:
|
|
podcast.disable()
|
|
self.client.finish()
|
|
self._error(_('Disabling feed update from %s.') % url)
|
|
|
|
return True
|
|
|
|
def enable(self, url):
|
|
podcast = self.client.get_podcast(url)
|
|
|
|
if podcast is None:
|
|
self._error(_('You are not subscribed to %s.') % url)
|
|
else:
|
|
podcast.enable()
|
|
self.client.finish()
|
|
self._error(_('Enabling feed update from %s.') % url)
|
|
|
|
return True
|
|
|
|
|
|
|
|
def sync(self):
|
|
self.client.synchronize_device()
|
|
|
|
|
|
def youtube_resolve(self, url):
|
|
yurl = self.client.youtube_url_resolver(url)
|
|
print "Youtube URL %s resolved to %s" % (url, yurl)
|
|
return True
|
|
|
|
# -------------------------------------------------------------------
|
|
|
|
def _error(self, *args):
|
|
print >>sys.stderr, ' '.join(args)
|
|
|
|
def _info(self, *args):
|
|
print >>sys.stdout, ' '.join(args)
|
|
|
|
def _checkargs(self, func, command_line):
|
|
args, varargs, keywords, defaults = inspect.getargspec(func)
|
|
args.pop(0) # Remove "self" from args
|
|
defaults = defaults or ()
|
|
minarg, maxarg = len(args)-len(defaults), len(args)
|
|
|
|
if len(command_line) < minarg or len(command_line) > maxarg:
|
|
self._error('Wrong argument count for %s.' % func.__name__)
|
|
return False
|
|
|
|
return func(*command_line)
|
|
|
|
|
|
def _parse(self, command_line):
|
|
if not command_line:
|
|
return False
|
|
|
|
command = command_line.pop(0)
|
|
if command.startswith('_'):
|
|
self._error(_('This command is not available.'))
|
|
return False
|
|
|
|
for name, func in inspect.getmembers(self):
|
|
if inspect.ismethod(func) and name == command:
|
|
return self._checkargs(func, command_line)
|
|
|
|
self._error(_('The requested function is not available.'))
|
|
return False
|
|
|
|
|
|
if __name__ == '__main__':
|
|
cli = gPodderCli()
|
|
cli._parse(sys.argv[1:]) or sys.stderr.write(__doc__)
|
|
|
|
|