Exemplo n.º 1
0
    def get_album_images(self, album):
        # import here to avoid circular import
        from opmuse.remotes import remotes

        remotes_album = remotes.get_album(album)

        tags = self._tags(remotes_album)

        if len(album.artists) > 0:
            artist = album.artists[0]
            name = '"%s" AND "%s"' % (artist.name, album.name)
        else:
            name = '"%s"' % album.name

        query = self._query(name, tags)

        return self.images(query)
Exemplo n.º 2
0
    def track(self, slug):
        track = library_dao.get_track_by_slug(slug)

        if track is None:
            raise cherrypy.NotFound()

        remotes.update_track(track)

        remotes_album = remotes_artist = None

        if track.album is not None:
            remotes.update_album(track.album)
            remotes_album = remotes.get_album(track.album)

        if track.artist is not None:
            remotes.update_artist(track.artist)
            remotes_artist = remotes.get_artist(track.artist)

        remotes_track = remotes.get_track(track)

        if track.artist is not None:
            artist_listened_tuples = self.get_artist_listened_tuples(track.artist.name)

            if track.album is not None:
                album_listened_tuples = self.get_album_listened_tuples(track.artist.name, track.album.name)
            else:
                album_listened_tuples = None
        else:
            artist_listened_tuples = None
            album_listened_tuples = None

        return {
            'track': track,
            'remotes_artist': remotes_artist,
            'remotes_album': remotes_album,
            'remotes_track': remotes_track,
            'album_listened_tuples': album_listened_tuples,
            'artist_listened_tuples': artist_listened_tuples,
        }
Exemplo n.º 3
0
    def fetch_album_cover(self, album_id):
        try:
            album = get_database().query(Album).filter_by(id=album_id).one()
        except NoResultFound:
            return

        remotes_album = None
        tries = 0

        # try and sleep until we get the remotes_album.
        while remotes_album is None and tries < 8:
            remotes_album = remotes.get_album(album)

            tries += 1

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

        lastfm_album = None

        if remotes_album is not None:
            lastfm_album = remotes_album["lastfm"]

        if lastfm_album is None or lastfm_album["cover"] is None:
            google_images = google.get_album_images(album)

            if google_images is not None:
                urls = google_images
            else:
                return
        else:
            urls = [lastfm_album["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 album.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" % (album.slug, cover_ext)).encode("utf8"))

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

            album.cover_path = cover_dest

        import mmh3

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

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

        ws.emit_all("covers.album.update", album.id)
Exemplo n.º 4
0
    def album(self, artist_slug, album_slug):
        try:
            album = (get_database().query(Album)
                     # _dir_tracks() uses paths
                     .options(joinedload(Album.tracks, Track.paths))
                     .filter_by(slug=album_slug).one())
        except NoResultFound:
            raise cherrypy.NotFound()

        # always update seen, True means now
        album.seen = True

        remotes.update_album(album)

        remotes_artists = []

        for artist in album.artists:
            remotes.update_artist(artist)
            remotes_artist = remotes.get_artist(artist)

            if remotes_artist is not None:
                remotes_artists.append(remotes_artist)

        disc_nos = {}

        for track in album.tracks:
            remotes.update_track(track)

            disc = '' if track.disc is None else track.disc

            if disc not in disc_nos:
                disc_nos[disc] = []

            if track.number is not None:
                # extract max track no, '01' will become 1, '01/10' will become 10
                disc_nos[disc].append(int(re.search('\d+', track.number).group()))
            else:
                disc_nos[disc].append(None)

        album_disc_nos = []

        for disc, numbers in disc_nos.items():
            max_number = max([number for number in numbers if number is not None], default=None)

            if max_number is not None:
                album_disc_nos.append((disc, len(numbers), max_number))

        album_disc_nos = sorted(album_disc_nos, key=lambda album_disc_no: album_disc_no[0])

        remotes_album = remotes.get_album(album)

        dir_tracks = self._dir_tracks(album.tracks)

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

        album_listened_tuples = self.get_album_listened_tuples(artist_name, album.name)
        artist_listened_tuples = self.get_artist_listened_tuples(artist_name)

        return {
            'album_disc_nos': album_disc_nos,
            'album': album,
            'dir_tracks': dir_tracks,
            'remotes_artists': remotes_artists,
            'remotes_album': remotes_album,
            'album_listened_tuples': album_listened_tuples,
            'artist_listened_tuples': artist_listened_tuples,
        }