Exemple #1
0
    def start(self, session=None):
        cache_key = LibraryUpload.CACHE_KEY % (cherrypy.request.user.id, session)

        all_tracks = []

        cache.set(cache_key, all_tracks)

        return b''
Exemple #2
0
    def _fetch_tag(self, tag_name):
        key = Remotes.TAG_KEY_FORMAT % tag_name

        tag = {
            'lastfm': lastfm.get_tag(tag_name, 1000),
        }

        cache.set(key, tag)

        ws.emit_all('remotes.tag.fetched', tag_name)
Exemple #3
0
    def transcoding_done(self, track):
        if hasattr(cherrypy.request, 'queue_current_id') and cherrypy.request.queue_current_id is not None:
            queue_current_id = cherrypy.request.queue_current_id

            queue_dao.update_queue(queue_current_id, played=True)

            cherrypy.request.queues_done = True

            ws_user = ws.get_ws_user()
            cache.set('queue.current_track_%d' % ws_user.id, None)
            cache.set('queue.current_progress_%d' % ws_user.id, None)
Exemple #4
0
    def transcoding_progress(self, progress, transcoder, track):
        ws_user = ws.get_ws_user()

        if ws_user is not None:
            cache.set('queue.current_progress_%d' % ws_user.id, progress)

        if 'User-Agent' in cherrypy.request.headers:
            user_agent = cherrypy.request.headers['User-Agent']
        else:
            user_agent = None

        format = transcoder.__class__.outputs()

        ws.emit('queue.progress', progress, self.serialize_track(track), user_agent, format)

        cherrypy.request.queue_progress = progress
Exemple #5
0
    def _fetch_user(self, id, lastfm_user, lastfm_session_key):
        key = Remotes.USER_KEY_FORMAT % id

        if lastfm_user is None or lastfm_session_key is None:
            return

        user_lastfm = lastfm.get_user(lastfm_user, lastfm_session_key)

        if user_lastfm is None:
            return

        cache.set(key, {
            'lastfm': user_lastfm
        })

        ws.emit_all('remotes.user.fetched', id)
Exemple #6
0
    def _add_listened_tracks(self, user_id, lastfm_user, lastfm_session_key):
        key = Remotes.USER_TRACKS_KEY_FORMAT % user_id

        if lastfm_user is None or lastfm_session_key is None:
            return

        count = library_dao.get_listened_tracks_count(user_id)

        max_timestamp = library_dao.get_listened_track_max_timestamp(user_id)

        index = 0

        for track in lastfm.get_user_tracks(lastfm_user, lastfm_session_key):
            # if we have less than 90% of what lastfm reports that the total
            # should be we assume something went wrong before and we redo
            # the whole thing from the start.
            #
            # this could occur if the user shuts down opmuse before this finishes
            # or if lastfm goes down or rate limits the hell out of us.
            #
            # the reason we only do it when it's less than 90% is because there's
            # a bug in the lastfm api where if the user has submitted non utf8 data
            # or otherwise "corrupt" data the api call will fail with an empty
            # response and we'll miss out on some tracks and there's nothing we
            # can do about that. http://www.last.fm/group/Last.fm+Web+Services/forum/21604/_/626352
            #
            # this could obviously also 'cause some unnecessary re-fetching of all
            # tracks on every run in some cases, but i'm not gonna fix that until
            # someone reports it as an actual issue...
            if index == 0 and count > 0 and count / track['total'] < .90:
                max_timestamp = None
                count = 0
                library_dao.delete_listened_tracks(user_id)

            if max_timestamp is not None and track['timestamp'] is not None and max_timestamp >= track['timestamp']:
                break

            library_dao.add_listened_track(user_id, track['name'], track['artist'], track['album'], track['timestamp'])

            index += 1

        cache.set(key, True)
Exemple #7
0
    def transcoding_start(self, transcoder, track):
        if hasattr(cherrypy.request, 'queue_current_id') and cherrypy.request.queue_current_id is not None:
            queue_current_id = cherrypy.request.queue_current_id
            queue_dao.update_queue(queue_current_id, current_seconds=None, playing=True)

        track = self.serialize_track(track)

        if 'User-Agent' in cherrypy.request.headers:
            user_agent = cherrypy.request.headers['User-Agent']
        else:
            user_agent = None

        format = transcoder.__class__.outputs()

        ws_user = ws.get_ws_user()

        if ws_user is not None:
            cache.set('queue.current_track_%d' % ws_user.id, track)

        ws.emit('queue.start', track, user_agent, format)
Exemple #8
0
    def _fetch_artist(self, id):
        key = Remotes.ARTIST_KEY_FORMAT % id

        try:
            artist_entity = get_database().query(Artist).filter(Artist.id == id).one()
        except NoResultFound:
            # artist removed, just ignore
            return

        artist = {
            'wikipedia': wikipedia.get_artist(artist_entity.name),
            'lastfm': lastfm.get_artist(artist_entity.name),
            'google': google.get_artist_search(artist_entity),
            'musicbrainz': musicbrainz.get_artist(artist_entity)
        }

        cache.set(key, artist)

        ws.emit_all('remotes.artist.fetched', id,
                    [album.id for album in artist_entity.albums],
                    [track.id for track in artist_entity.tracks])
Exemple #9
0
    def _fetch_track(self, id):
        key = Remotes.TRACK_KEY_FORMAT % id

        try:
            track_entity = get_database().query(Track).filter(Track.id == id).one()
        except NoResultFound:
            # track was probably removed, edited and therefor recreated. either
            # way we just ignore it
            return

        album_name = track_entity.album.name if track_entity.album is not None else None
        artist_name = track_entity.artist.name if track_entity.artist is not None else None

        track = {
            'wikipedia': wikipedia.get_track(artist_name, album_name, track_entity.name),
            'lastfm': lastfm.get_track(artist_name, track_entity.name)
        }

        cache.set(key, track)

        ws.emit_all('remotes.track.fetched', id)
Exemple #10
0
    def _fetch_album(self, id):
        key = Remotes.ALBUM_KEY_FORMAT % id

        try:
            album_entity = get_database().query(Album).filter(Album.id == id).one()
        except NoResultFound:
            # album removed, just ignore
            return

        if len(album_entity.artists) > 0:
            artist_name = album_entity.artists[0].name
        else:
            artist_name = None

        album = {
            'wikipedia': wikipedia.get_album(artist_name, album_entity.name),
            'lastfm': lastfm.get_album(artist_name, album_entity.name),
            'musicbrainz': musicbrainz.get_album(album_entity)
        }

        cache.set(key, album)

        ws.emit_all('remotes.album.fetched', id, [track.id for track in album_entity.tracks])
Exemple #11
0
    def default(self):
        user = cherrypy.request.user

        auth_url = new_auth = None
        need_config = False

        cache_key = 'settings_lastfm_session_key_%d' % cherrypy.request.user.id

        if user.lastfm_session_key is None:
            session_key = cache.get(cache_key)

            if session_key is not None:
                auth_url = session_key.get_auth_url()
                key = session_key.get_session_key()

                if key is not None:
                    cache.set(cache_key, None)
                    user.lastfm_session_key = key
                    user.lastfm_user = lastfm.get_authenticated_user_name(key)
                    auth_url = None
                    new_auth = True
            else:
                try:
                    session_key = SessionKey()
                    auth_url = session_key.get_auth_url()
                    cache.set(cache_key, session_key)
                except LastfmError:
                    need_config = True

        remotes.update_user(user)

        return {
            "user": user,
            'need_config': need_config,
            'auth_url': auth_url,
            'new_auth': new_auth
        }
Exemple #12
0
    def _reset_current(self):
        ws_user = ws.get_ws_user()
        cache.set('queue.current_track_%d' % ws_user.id, None)
        cache.set('queue.current_progress_%d' % ws_user.id, None)

        ws.emit('queue.reset')
Exemple #13
0
    def default(self, query=None, type=None):
        artists = []
        albums = []
        tracks = []
        track_ids = []

        hierarchy = None

        album_track_ids = set()

        recent_searches = []

        if query is not None:
            albums = None
            tracks = None

            # only search for artists
            if type == 'artist':
                artists = search.query_artist(query, exact=True)
                albums = []
                tracks = []
            else:
                artists = search.query_artist(query)

            if albums is None:
                albums = search.query_album(query)

            if tracks is None:
                tracks = search.query_track(query)

            for artist in artists:
                remotes.update_artist(artist)

            for album in albums:
                remotes.update_album(album)

            for track in tracks:
                track_ids.append(track.id)
                remotes.update_track(track)

            entities = artists + albums + tracks

            if len(entities) == 1:
                for artist in artists:
                    raise HTTPRedirect('/%s' % artist.slug)
                for album in albums:
                    raise HTTPRedirect('/%s/%s' % (album.artists[0].slug, album.slug))
                for track in tracks:
                    raise HTTPRedirect('/library/track/%s' % track.slug)

            if cache.has(Search.CACHE_RECENT_KEY):
                recent_searches = cache.get(Search.CACHE_RECENT_KEY)
            else:
                cache.set(Search.CACHE_RECENT_KEY, recent_searches)

            if type is None and len(entities) > 0:
                if len(recent_searches) == 0 or query != recent_searches[0][0]:
                    recent_searches.insert(0, (query, datetime.datetime.now(), cherrypy.request.user.login))

                    if len(recent_searches) > Search.MAX_RECENT:
                        recent_searches.pop()

            entities = sorted(entities, key=lambda entity: entity._SEARCH_SCORE, reverse=True)

            hierarchy = Library._produce_track_hierarchy(entities)

            for key, result_artist in hierarchy['artists'].items():
                for key, result_album in result_artist['albums'].items():
                    for track_id in library_dao.get_track_ids_by_album_id(result_album['entity'].id):
                        album_track_ids.add(track_id)

        return {
            'query': query,
            'hierarchy': hierarchy,
            'tracks': tracks,
            'albums': albums,
            'artists': artists,
            'track_ids': track_ids,
            'album_track_ids': list(album_track_ids),
            'recent_searches': recent_searches
        }