Support for YouTube in CoverDownloader.
[gpodder.git] / src / gpodder / console.py
bloba646b77955763473fcfe881eab4f3e9e081f9048
1 # -*- coding: utf-8 -*-
3 # gPodder - A media aggregator and podcast client
4 # Copyright (c) 2005-2008 Thomas Perl and the gPodder Team
6 # gPodder is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # gPodder is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from gpodder import util
21 from gpodder import download
22 from gpodder import sync
23 from gpodder.libgpodder import gl
24 from gpodder.liblogger import msg
26 from libpodcasts import load_channels
27 from libpodcasts import update_channels
28 from libpodcasts import save_channels
29 from libpodcasts import podcastChannel
31 import time
33 import popen2
34 import urllib
35 import sys
38 def list_channels():
39 for channel in load_channels():
40 msg('podcast', urllib.unquote(channel.url))
43 def add_channel( url):
44 callback_error = lambda s: msg( 'error', s)
46 url = util.normalize_feed_url( url)
47 try:
48 channel = podcastChannel.load(url, create=True)
49 except:
50 msg( 'error', _('Could not load feed from URL: %s'), urllib.unquote( url))
51 return
53 if channel:
54 channels = load_channels()
55 if channel.url in ( c.url for c in channels ):
56 msg( 'error', _('Already added: %s'), urllib.unquote( url))
57 return
58 channels.append( channel)
59 save_channels( channels)
60 msg( 'add', urllib.unquote( url))
61 else:
62 msg('error', _('Could not add podcast.'))
65 def del_channel( url):
66 url = util.normalize_feed_url( url)
68 channels = load_channels()
69 keep_channels = []
70 for channel in channels:
71 if channel.url == url:
72 msg( 'delete', urllib.unquote( channel.url))
73 else:
74 keep_channels.append( channel)
76 if len(keep_channels) < len(channels):
77 save_channels( keep_channels)
78 else:
79 msg('error', _('Could not remove podcast.'))
82 def update():
83 callback_error = lambda s: msg('error', s)
84 sys.stdout.write(_('Updating podcast feeds...'))
85 sys.stdout.flush()
86 result = update_channels(callback_error=callback_error)
87 print _('done.')
88 return result
90 def run():
91 channels = update()
92 new_episodes = 0
94 for channel in channels:
95 for episode in channel.get_new_episodes():
96 msg( 'downloading', urllib.unquote( episode.url))
97 # Calling run() calls the code in the current thread
98 download.DownloadThread( channel, episode).run()
99 new_episodes += 1
101 if new_episodes == 0:
102 print _('No new episodes to download.')
103 elif new_episodes == 1:
104 print _('Downloaded one new episode.')
105 else:
106 print _('Downloaded %d new episodes.') % new_episodes
108 def sync_device():
109 device = sync.open_device()
110 if device is None:
111 msg('error', _('No device configured. Please use the GUI.'))
112 return False
114 callback_status = lambda s: msg('status', s)
115 device.register('status', callback_status)
116 callback_done = lambda: msg('done', _('Synchronization finished.'))
117 device.register('done', callback_done)
118 callback_progress = lambda i, n: msg('progress', _('Synchronizing: %d of %d') % (i, n))
119 device.register('progress', callback_progress)
121 if not device.open():
122 msg('error', _('Cannot open device.'))
123 return False
125 for channel in load_channels():
126 if not channel.sync_to_devices:
127 msg('info', _('Skipping podcast: %s') % channel.title)
128 continue
130 episodes_to_sync = []
131 for episode in channel.get_all_episodes():
132 if episode.was_downloaded(and_exists=True):
133 episodes_to_sync.append(episode)
134 device.add_tracks(episodes_to_sync)
136 if not device.close():
137 msg('error', _('Cannot close device.'))
138 return False
140 def sync_stats():
141 size = 0
142 device = sync.open_device()
143 if device is None:
144 msg('error', _('No device configured. Please use the GUI.'))
145 return False
147 for channel in load_channels():
148 if not channel.sync_to_devices:
149 continue
150 for episode in channel.get_all_episodes():
151 if episode.was_downloaded(and_exists=True):
152 episode.calculate_filesize()
153 size += episode.length
154 msg('info', _('Free space on device %s: %s') % (gl.get_device_name(), gl.format_filesize(device.get_free_space())))
155 msg('info', _('Size of episodes to sync: %s') % gl.format_filesize(size))
157 difference = device.get_free_space() - size
158 if difference < 0:
159 msg('error', _('Need to free at least %s more') % gl.format_filesize(abs(difference)))
160 return False
161 else:
162 msg('info', _('Free space after sync: %s') % gl.format_filesize(abs(difference)))
164 if not device.close():
165 msg('error', _('Cannot close device.'))
166 return False
168 return True