mirror of
https://github.com/vale981/clay
synced 2025-03-05 09:31:40 -05:00
Merge pull request #17 from and3rson/cache-indicator
Added cached tracks indicators.
This commit is contained in:
commit
504362c47e
4 changed files with 43 additions and 6 deletions
|
@ -117,6 +117,13 @@ class Track(object):
|
|||
return self.library_id
|
||||
return self.store_id
|
||||
|
||||
@property
|
||||
def filename(self):
|
||||
"""
|
||||
Return a filename for this track.
|
||||
"""
|
||||
return self.store_id + '.mp3'
|
||||
|
||||
def __eq__(self, other):
|
||||
return (
|
||||
(self.library_id and self.library_id == other.library_id) or
|
||||
|
|
|
@ -347,8 +347,8 @@ class Player(object):
|
|||
self.broadcast_state()
|
||||
self.track_changed.fire(track)
|
||||
|
||||
if settings.get('download_tracks', False):
|
||||
path = settings.get_cached_file_path(track.store_id + '.mp3')
|
||||
if settings.get('download_tracks', False) or settings.get_is_file_cached(track.filename):
|
||||
path = settings.get_cached_file_path(track.filename)
|
||||
if path is None:
|
||||
self.logger.debug('Track %s not in cache, downloading...', track.store_id)
|
||||
track.get_url(callback=self._download_track)
|
||||
|
@ -369,7 +369,7 @@ class Player(object):
|
|||
)
|
||||
return
|
||||
response = urlopen(url)
|
||||
path = settings.save_file_to_cache(track.store_id + '.mp3', response.read())
|
||||
path = settings.save_file_to_cache(track.filename, response.read())
|
||||
self._play_ready(path, None, track)
|
||||
|
||||
def _play_ready(self, url, error, track):
|
||||
|
|
|
@ -42,9 +42,15 @@ class _Settings(object):
|
|||
"""
|
||||
def __init__(self):
|
||||
self._config = {}
|
||||
self._cached_files = set()
|
||||
|
||||
self._config_dir = None
|
||||
self._config_file_path = None
|
||||
self._cache_dir = None
|
||||
|
||||
self._ensure_directories()
|
||||
self._load_config()
|
||||
self._load_cache()
|
||||
|
||||
def _ensure_directories(self):
|
||||
"""
|
||||
|
@ -77,6 +83,12 @@ class _Settings(object):
|
|||
with open(self._config_file_path, 'r') as settings_file:
|
||||
self._config = yaml.load(settings_file.read())
|
||||
|
||||
def _load_cache(self):
|
||||
"""
|
||||
Load cached files.
|
||||
"""
|
||||
self._cached_files = set(os.listdir(self._cache_dir))
|
||||
|
||||
def _commit_edits(self, config):
|
||||
"""
|
||||
Write config to file.
|
||||
|
@ -120,6 +132,12 @@ class _Settings(object):
|
|||
return path
|
||||
return None
|
||||
|
||||
def get_is_file_cached(self, filename):
|
||||
"""
|
||||
Return ``True`` if *filename* is present in cache.
|
||||
"""
|
||||
return filename in self._cached_files
|
||||
|
||||
def save_file_to_cache(self, filename, content):
|
||||
"""
|
||||
Save content into file in cache.
|
||||
|
@ -127,6 +145,7 @@ class _Settings(object):
|
|||
path = os.path.join(self._cache_dir, filename)
|
||||
with open(path, 'wb') as cachefile:
|
||||
cachefile.write(content)
|
||||
self._cached_files.add(filename)
|
||||
return path
|
||||
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ from clay.notifications import NotificationArea
|
|||
from clay.player import Player
|
||||
from clay.gp import GP
|
||||
from clay.clipboard import copy
|
||||
from clay.settings import settings
|
||||
|
||||
|
||||
class SongListItem(urwid.Pile):
|
||||
|
@ -46,8 +47,14 @@ class SongListItem(urwid.Pile):
|
|||
self.track = track
|
||||
self.index = 0
|
||||
self.state = SongListItem.STATE_IDLE
|
||||
self.line1 = urwid.SelectableIcon('', cursor_position=1000)
|
||||
self.line1.set_layout('left', 'clip', None)
|
||||
self.line1_left = urwid.SelectableIcon('', cursor_position=1000)
|
||||
self.line1_left.set_layout('left', 'clip', None)
|
||||
self.line1_right = urwid.Text('x')
|
||||
self.line1 = urwid.Columns([
|
||||
self.line1_left,
|
||||
('pack', self.line1_right),
|
||||
('pack', urwid.Text(' '))
|
||||
])
|
||||
self.line2 = urwid.Text('', wrap='clip')
|
||||
|
||||
self.line1_wrap = urwid.AttrWrap(self.line1, 'line1')
|
||||
|
@ -96,7 +103,7 @@ class SongListItem(urwid.Pile):
|
|||
title_attr = 'line1_active_focus' if self.is_focused else 'line1_active'
|
||||
artist_attr = 'line2_focus' if self.is_focused else 'line2'
|
||||
|
||||
self.line1.set_text(
|
||||
self.line1_left.set_text(
|
||||
u'{index:3d} {icon} {title} [{minutes:02d}:{seconds:02d}]'.format(
|
||||
index=self.index + 1,
|
||||
icon=self.get_state_icon(self.state),
|
||||
|
@ -105,6 +112,10 @@ class SongListItem(urwid.Pile):
|
|||
seconds=(self.track.duration // 1000) % 60
|
||||
)
|
||||
)
|
||||
if settings.get_is_file_cached(self.track.filename):
|
||||
self.line1_right.set_text(u'\u25bc Cached')
|
||||
else:
|
||||
self.line1_right.set_text(u'')
|
||||
self.line2.set_text(
|
||||
u' {} \u2015 {}'.format(self.track.artist, self.track.album_name)
|
||||
)
|
||||
|
|
Loading…
Add table
Reference in a new issue