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
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
)
48 channel
= podcastChannel
.load(url
, create
=True)
50 msg( 'error', _('Could not load feed from URL: %s'), urllib
.unquote( url
))
54 channels
= load_channels()
55 if channel
.url
in ( c
.url
for c
in channels
):
56 msg( 'error', _('Already added: %s'), urllib
.unquote( url
))
58 channels
.append( channel
)
59 save_channels( channels
)
60 msg( 'add', urllib
.unquote( url
))
62 msg('error', _('Could not add podcast.'))
65 def del_channel( url
):
66 url
= util
.normalize_feed_url( url
)
68 channels
= load_channels()
70 for channel
in channels
:
71 if channel
.url
== url
:
72 msg( 'delete', urllib
.unquote( channel
.url
))
74 keep_channels
.append( channel
)
76 if len(keep_channels
) < len(channels
):
77 save_channels( keep_channels
)
79 msg('error', _('Could not remove podcast.'))
83 callback_error
= lambda s
: msg('error', s
)
84 sys
.stdout
.write(_('Updating podcast feeds...'))
86 result
= update_channels(callback_error
=callback_error
)
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()
101 if new_episodes
== 0:
102 print _('No new episodes to download.')
103 elif new_episodes
== 1:
104 print _('Downloaded one new episode.')
106 print _('Downloaded %d new episodes.') % new_episodes
109 device
= sync
.open_device()
111 msg('error', _('No device configured. Please use the GUI.'))
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.'))
125 for channel
in load_channels():
126 if not channel
.sync_to_devices
:
127 msg('info', _('Skipping podcast: %s') % channel
.title
)
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.'))
142 device
= sync
.open_device()
144 msg('error', _('No device configured. Please use the GUI.'))
147 for channel
in load_channels():
148 if not channel
.sync_to_devices
:
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
159 msg('error', _('Need to free at least %s more') % gl
.format_filesize(abs(difference
)))
162 msg('info', _('Free space after sync: %s') % gl
.format_filesize(abs(difference
)))
164 if not device
.close():
165 msg('error', _('Cannot close device.'))