Exemplo n.º 1
0
def getCachedArt(albumid):
    from headphones import cache

    c = cache.Cache()
    artwork_path = c.get_artwork_from_cache(AlbumID=albumid)

    if not artwork_path:
        return

    if artwork_path.startswith('http://'):
        artwork = request.request_content(artwork_path, timeout=20)

        if not artwork:
            logger.warn("Unable to open url: %s", artwork_path)
            return
    else:
        with open(artwork_path, "r") as fp:
            return fp.read()
Exemplo n.º 2
0
def getCachedArt(albumid):

    from headphones import cache

    c = cache.Cache()

    artwork_path = c.get_artwork_from_cache(AlbumID=albumid)

    if not artwork_path:
        return None

    if artwork_path.startswith('http://'):
        try:
            artwork = urllib2.urlopen(artwork_path, timeout=20).read()
            return artwork
        except:
            logger.warn("Unable to open url: " + artwork_path)
            return None
    else:
        artwork = open(artwork_path, "r").read()
        return artwork
Exemplo n.º 3
0
def switch(AlbumID, ReleaseID):
    """
    Takes the contents from allalbums & alltracks (based on ReleaseID) and switches them into
    the albums & tracks table.
    """
    logger.debug('Switching allalbums and alltracks')
    myDB = db.DBConnection()
    oldalbumdata = myDB.action('SELECT * from albums WHERE AlbumID=?',
                               [AlbumID]).fetchone()
    newalbumdata = myDB.action('SELECT * from allalbums WHERE ReleaseID=?',
                               [ReleaseID]).fetchone()
    newtrackdata = myDB.action('SELECT * from alltracks WHERE ReleaseID=?',
                               [ReleaseID]).fetchall()
    myDB.action('DELETE from tracks WHERE AlbumID=?', [AlbumID])

    controlValueDict = {"AlbumID": AlbumID}

    newValueDict = {
        "ArtistID": newalbumdata['ArtistID'],
        "ArtistName": newalbumdata['ArtistName'],
        "AlbumTitle": newalbumdata['AlbumTitle'],
        "ReleaseID": newalbumdata['ReleaseID'],
        "AlbumASIN": newalbumdata['AlbumASIN'],
        "ReleaseDate": newalbumdata['ReleaseDate'],
        "Type": newalbumdata['Type'],
        "ReleaseCountry": newalbumdata['ReleaseCountry'],
        "ReleaseFormat": newalbumdata['ReleaseFormat']
    }

    myDB.upsert("albums", newValueDict, controlValueDict)

    # Update cache
    c = cache.Cache()
    c.remove_from_cache(AlbumID=AlbumID)
    c.get_artwork_from_cache(AlbumID=AlbumID)

    for track in newtrackdata:
        controlValueDict = {"TrackID": track['TrackID'], "AlbumID": AlbumID}

        newValueDict = {
            "ArtistID": track['ArtistID'],
            "ArtistName": track['ArtistName'],
            "AlbumTitle": track['AlbumTitle'],
            "AlbumASIN": track['AlbumASIN'],
            "ReleaseID": track['ReleaseID'],
            "TrackTitle": track['TrackTitle'],
            "TrackDuration": track['TrackDuration'],
            "TrackNumber": track['TrackNumber'],
            "CleanName": track['CleanName'],
            "Location": track['Location'],
            "Format": track['Format'],
            "BitRate": track['BitRate']
        }

        myDB.upsert("tracks", newValueDict, controlValueDict)

    # Mark albums as downloaded if they have at least 80% (by default,
    # configurable) of the album
    total_track_count = len(newtrackdata)
    have_track_count = len(
        myDB.select(
            'SELECT * from tracks WHERE AlbumID=? AND Location IS NOT NULL',
            [AlbumID]))

    if oldalbumdata['Status'] == 'Skipped' and (
        (have_track_count / float(total_track_count)) >=
        (headphones.CONFIG.ALBUM_COMPLETION_PCT / 100.0)):
        myDB.action('UPDATE albums SET Status=? WHERE AlbumID=?',
                    ['Downloaded', AlbumID])

    # Update have track counts on index
    totaltracks = len(
        myDB.select(
            'SELECT TrackTitle from tracks AS tr INNER JOIN albums AS al ON al.AlbumID = tr.AlbumID WHERE al.ArtistID=? '
            'AND al.Status != "Ignored"', [newalbumdata['ArtistID']]))
    havetracks = len(
        myDB.select(
            'SELECT TrackTitle from tracks WHERE ArtistID=? AND Location IS NOT NULL',
            [newalbumdata['ArtistID']]))

    controlValueDict = {"ArtistID": newalbumdata['ArtistID']}

    newValueDict = {"TotalTracks": totaltracks, "HaveTracks": havetracks}

    myDB.upsert("artists", newValueDict, controlValueDict)