GTK UI: Clean up episode_object_by_uri

This commit is contained in:
Thomas Perl 2011-07-26 12:17:17 +02:00
parent 0c22cbc482
commit 1894f0e999
2 changed files with 20 additions and 17 deletions

View File

@ -240,11 +240,6 @@ class Database(object):
else:
return row[0]
def get_podcast_id_from_episode_url(self, url):
"""Return the (first) associated podcast ID given an episode URL"""
assert url
return self.get('SELECT podcast_id FROM %s WHERE url = ? LIMIT 1' % (self.TABLE_EPISODE,), (url,))
def podcast_download_folder_exists(self, foldername):
"""
Returns True if a foldername for a channel exists.

View File

@ -340,23 +340,31 @@ class gPodder(BuilderWidget, dbus.service.Object):
prefix = 'file://' + urllib.quote(gpodder.downloads)
# By default, assume we can't pre-select any channel
# but can match episodes simply via the download URL
is_channel = lambda c: True
is_episode = lambda e: e.url == uri
if uri.startswith(prefix):
# File is on the local filesystem in the download folder
# Try to reduce search space by pre-selecting the channel
# based on the folder name of the local file
filename = urllib.unquote(uri[len(prefix):])
file_parts = [x for x in filename.split(os.sep) if x]
file_parts = filter(None, filename.split(os.sep))
if len(file_parts) == 2:
foldername, filename = file_parts
for channel in filter(lambda c: c.download_folder == foldername, self.channels):
for episode in filter(lambda e: e.download_filename == filename, channel.get_all_episodes()):
return episode
else:
# Possibly remote file - search the database for a podcast
channel_id = self.db.get_podcast_id_from_episode_url(uri)
if len(file_parts) != 2:
return None
for channel in filter(lambda c: c.id == channel_id, self.channels):
for episode in filter(lambda e: e.url == url, channel.get_all_episodes()):
return episode
foldername, filename = file_parts
is_channel = lambda c: c.download_folder == foldername
is_episode = lambda e: e.download_filename == filename
# Deep search through channels and episodes for a match
for channel in filter(is_channel, self.channels):
for episode in filter(is_episode, channel.get_all_episodes()):
return episode
return None