Exemplo n.º 1
0
    def refresh(self, type, slug):
        if type not in ["album", "artist"]:
            raise ValueError("Invalid type %s supplied" % type)

        entity = None

        if type == "album":
            entity = library_dao.get_album_by_slug(slug)
        elif type == "artist":
            entity = library_dao.get_artist_by_slug(slug)

        if entity is not None:
            if entity.cover_path is not None and os.path.exists(entity.cover_path):
                os.remove(entity.cover_path)

            entity.cover_path = None
            entity.cover_hash = None
            entity.cover = None
            entity.cover_large = None

        get_database().commit()

        if type == "album":
            ws.emit_all("covers.album.update", entity.id)
        elif type == "artist":
            ws.emit_all("covers.artist.update", entity.id)
Exemplo n.º 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)
Exemplo n.º 3
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)
Exemplo n.º 4
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])
Exemplo n.º 5
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)
Exemplo n.º 6
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])
Exemplo n.º 7
0
    def fetch_artist_cover(self, artist_id):
        try:
            artist = get_database().query(Artist).filter_by(id=artist_id).one()
        except NoResultFound:
            return

        remotes_artist = None
        tries = 0

        # try and sleep until we get the remotes_artist.
        while remotes_artist is None and tries < 8:
            remotes_artist = remotes.get_artist(artist)

            tries += 1

            if remotes_artist is None:
                # exponential backoff
                time.sleep(tries ** 2)

        lastfm_artist = None

        if remotes_artist is not None:
            lastfm_artist = remotes_artist["lastfm"]

        if lastfm_artist is None or lastfm_artist["cover"] is None:
            google_images = google.get_artist_images(artist)

            if google_images is not None:
                urls = google_images
            else:
                return
        else:
            urls = [lastfm_artist["cover"]]

        cover = None

        for url in urls:
            cover, resize_cover, resize_cover_large, cover_ext = self.retrieve_and_resize(url)

            if cover is None:
                continue

        if cover is None:
            return

        track_dirs = set()

        for track in artist.tracks:
            for path in track.paths:
                track_dirs.add(os.path.dirname(path.path))

        for track_dir in track_dirs:
            if not os.path.exists(track_dir):
                os.makedirs(track_dir)

            cover_dest = os.path.join(track_dir, ("%s%s" % (artist.slug, cover_ext)).encode("utf8"))

            if not os.path.exists(cover_dest):
                with open(cover_dest, "wb") as file:
                    file.write(cover)

            artist.cover_path = cover_dest

        import mmh3

        artist.cover = resize_cover
        artist.cover_large = resize_cover_large
        artist.cover_hash = base64.b64encode(mmh3.hash_bytes(artist.cover))

        try:
            get_database().commit()
        except StaleDataError:
            # artist was removed, ignore
            get_database().rollback()
            return

        ws.emit_all("covers.artist.update", artist.id)