Check download folders when loading podcasts

This makes sure that externally-downloaded files
are marked as downloaded and externally-deleted
files are marked as deleted on startup in all UIs.
This commit is contained in:
Thomas Perl 2011-10-22 17:50:10 +02:00
parent f475ce6278
commit 6b31f3751e
3 changed files with 24 additions and 32 deletions

16
bin/gpo
View File

@ -45,8 +45,6 @@
pending [URL] List new episodes (all or only from URL)
episodes [URL] List episodes (all or only from URL)
importfiles Check download folders for external files
- Other commands -
youtube [URL] Resolve the YouTube URL to a download URL
@ -306,20 +304,6 @@ class gPodderCli(object):
print count, 'episodes pending.'
return True
def importfiles(self):
full_count = 0
for podcast in self.client.get_podcasts():
print 'Checking for files in', podcast.title
count = podcast._podcast.import_external_files()
if count:
print ' => found %d valid file(s)' % count
full_count += count
print '%d downloaded file(s) imported.' % full_count
self.client.commit()
return True
def _download_episode(self, episode):
self._start_action('Downloading %s', episode.title)
episode.download(self._update_action)

View File

@ -212,11 +212,6 @@ class gPodder(BuilderWidget, dbus.service.Object):
self.hooks_podcast_update_cb,
self.hooks_episode_download_cb)
# Check if the user has downloaded any podcast with an external program
# and mark episodes as downloaded / move them away (bug 902)
for podcast in self.channels:
podcast.import_external_files()
# load list of user applications for audio playback
self.user_apps_reader = UserAppsReader(['audio', 'video'])
threading.Thread(target=self.user_apps_reader.read).start()

View File

@ -750,7 +750,7 @@ class PodcastChannel(PodcastModelObject):
def db(self):
return self.parent.db
def import_external_files(self):
def check_download_folder(self):
"""Check the download folder for externally-downloaded files
This will try to assign downloaded files with episodes in the
@ -759,9 +759,23 @@ class PodcastChannel(PodcastModelObject):
the user knows that gPodder doesn't know to which episode the
file belongs (the "Unknown" folder may be used by external
tools or future gPodder versions for better import support).
This will also cause missing files to be marked as deleted.
"""
known_files = set(e.local_filename(create=False) \
for e in self.get_downloaded_episodes())
known_files = set()
for episode in self.get_downloaded_episodes():
if episode.was_downloaded():
filename = episode.local_filename(create=False)
if not os.path.exists(filename):
# File has been deleted by the user - simulate a
# delete event (also marks the episode as deleted)
logger.debug('Episode deleted: %s', filename)
episode.delete_from_disk()
continue
known_files.add(filename)
existing_files = set(filename for filename in \
glob.glob(os.path.join(self.save_dir, '*')) \
if not filename.endswith('.partial'))
@ -769,11 +783,10 @@ class PodcastChannel(PodcastModelObject):
[os.path.join(self.save_dir, x) \
for x in ('folder.jpg', 'Unknown')])
if not external_files:
return 0
return
all_episodes = self.get_all_episodes()
count = 0
for filename in external_files:
found = False
@ -783,7 +796,6 @@ class PodcastChannel(PodcastModelObject):
existing = existing[0]
logger.info('Importing external download: %s', filename)
existing.on_downloaded(filename)
count += 1
continue
for episode in all_episodes:
@ -793,7 +805,6 @@ class PodcastChannel(PodcastModelObject):
logger.info('Importing external download: %s', filename)
episode.download_filename = basename
episode.on_downloaded(filename)
count += 1
found = True
break
@ -813,7 +824,6 @@ class PodcastChannel(PodcastModelObject):
episode.download_filename = basename
episode.on_downloaded(filename)
found = True
count += 1
break
if not found:
@ -827,8 +837,6 @@ class PodcastChannel(PodcastModelObject):
except Exception, e:
logger.error('Could not move file: %s', e, exc_info=True)
return count
@classmethod
def sort_key(cls, podcast):
key = podcast.title.lower()
@ -874,7 +882,7 @@ class PodcastChannel(PodcastModelObject):
tmp.get_save_dir(force_new=True)
# Mark episodes as downloaded if files already exist (bug 902)
tmp.import_external_files()
tmp.check_download_folder()
tmp.save()
@ -1277,6 +1285,11 @@ class Model(object):
if self.children is None:
self.children = self.db.load_podcasts(podcast_factory)
# Check download folders for changes (bug 902)
for podcast in self.children:
podcast.check_download_folder()
return self.children
def load_podcast(self, url, create=True, authentication_tokens=None,