Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 382 lines (296 sloc) 11.069 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
from flask import jsonify, render_template, json, send_file
from maraschino import app, logger, WEBROOT, RUNDIR
from maraschino.tools import requires_auth, get_setting_value
from threading import Thread
import StringIO
import urllib
import urllib2
import base64

def headphones_http():
    if get_setting_value('headphones_https') == '1':
        return 'https://'
    else:
        return 'http://'

def headphones_url():
    port = get_setting_value('headphones_port')
    url_base = get_setting_value('headphones_host')
    webroot = get_setting_value('headphones_webroot')

    if port:
        url_base = '%s:%s' % (url_base, port)

    if webroot:
        url_base = '%s/%s' % (url_base, webroot)

    return headphones_http() + url_base

def headphones_api(command, use_json=True, dev=False):
    username = get_setting_value('headphones_user')
    password = get_setting_value('headphones_password')
    apikey = get_setting_value('headphones_api')

    url = '%s/api?apikey=%s&cmd=%s' % (headphones_url(), apikey, command)
    
    request = urllib2.Request(url)
    base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
    request.add_header("Authorization", "Basic %s" % base64string)
    data = urllib2.urlopen(request).read()

    if use_json:
        data = json.JSONDecoder().decode(data)

    if dev:
        print 'DEVELOPER :: %s' % url
        print 'DEVELOPER :: %s' % data

    return data


def convert_track_duration(milliseconds):
    if milliseconds is None:
        return "00:00"
    seconds = milliseconds / 1000
    hours = seconds / 3600
    seconds -= 3600 * hours
    minutes = seconds / 60
    seconds -= 60 * minutes
    if hours == 0:
        return "%02d:%02d" % (minutes, seconds)
    return "%02d:%02d:%02d" % (hours, minutes, seconds)


def hp_compact():
    return get_setting_value('headphones_compact') == '1'

def headphones_exception(e):
    logger.log('HEADPHONES :: EXCEPTION -- %s' % e, 'DEBUG')
    return render_template('headphones-base.html', headphones=True, message=e)


def hp_artistart(id):
    return '%s/xhr/headphones/img/artist/%s' % (WEBROOT, id)


def hp_albumart(id):
    return '%s/xhr/headphones/img/album/%s' % (WEBROOT, id)

@app.route('/xhr/headphones/img/<type>/<id>/')
@requires_auth
def xhr_headphones_image(type, id):
    if type == 'artist':
        cache_url = headphones_api('getArtistThumb&id=' + id)
    else:
        cache_url = headphones_api('getAlbumThumb&id=' + id)

    if cache_url:
        url = '%s/%s' % (headphones_url(), cache_url)
    else:
        img = RUNDIR + '/static/images/applications/HeadPhones.png'
        return send_file(img, mimetype='image/jpeg')

    username = get_setting_value('headphones_user')
    password = get_setting_value('headphones_password')

    request = urllib2.Request(url)
    base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
    request.add_header("Authorization", "Basic %s" % base64string)

    img = StringIO.StringIO(urllib2.urlopen(request).read())
    return send_file(img, mimetype='image/jpeg')


@app.route('/xhr/headphones/')
@requires_auth
def xhr_headphones():
    return xhr_headphones_upcoming()


@app.route('/xhr/headphones/artists/')
@requires_auth
def xhr_headphones_artists():
    logger.log('HEADPHONES :: Fetching artists list', 'INFO')

    try:
        headphones = headphones_api('getIndex')
        updates = headphones_api('getVersion')
    except Exception as e:
        return headphones_exception(e)

    artists = []

    for artist in headphones:
        if not 'Fetch failed' in artist['ArtistName']:
            try:
                artist['Percent'] = int(100 * float(artist['HaveTracks']) / float(artist['TotalTracks']))
            except:
                artist['Percent'] = 0

            if not hp_compact():
                try:
                    artist['ThumbURL'] = hp_artistart(artist['ArtistID'])
                except:
                    pass
            artists.append(artist)

    return render_template('headphones-artists.html',
        headphones=True,
        artists=artists,
        updates=updates,
        compact=hp_compact(),
    )


@app.route('/xhr/headphones/artist/<artistid>/')
@requires_auth
def xhr_headphones_artist(artistid):
    logger.log('HEADPHONES :: Fetching artist', 'INFO')

    try:
        albums = headphones_api('getArtist&id=%s' % artistid)
    except Exception as e:
        return headphones_exception(e)

    if not hp_compact():
        for album in albums['albums']:
            try:
                album['ThumbURL'] = hp_albumart(album['AlbumID'])
            except:
                pass

    return render_template('headphones-artist.html',
        albums=albums,
        headphones=True,
        compact=hp_compact(),
    )


@app.route('/xhr/headphones/album/<albumid>/')
@requires_auth
def xhr_headphones_album(albumid):
    logger.log('HEADPHONES :: Fetching album', 'INFO')

    try:
        headphones = headphones_api('getAlbum&id=%s' % albumid)
    except Exception as e:
        return headphones_exception(e)

    album = headphones['album'][0]

    try:
        album['ThumbURL'] = hp_albumart(album['AlbumID'])
    except:
        pass

    album['TotalDuration'] = 0

    for track in headphones['tracks']:
        if track['TrackDuration'] == None:
            track['TrackDuration'] = 0
        album['TotalDuration'] = album['TotalDuration'] + int(track['TrackDuration'])
        track['TrackDuration'] = convert_track_duration(track['TrackDuration'])

    album['TotalDuration'] = convert_track_duration(album['TotalDuration'])
    album['Tracks'] = len(headphones['tracks'])

    return render_template('headphones-album.html',
        album=headphones,
        headphones=True,
        compact=hp_compact(),
    )


@app.route('/xhr/headphones/upcoming/')
@requires_auth
def xhr_headphones_upcoming():
    logger.log('HEADPHONES :: Fetching upcoming albums', 'INFO')

    try:
        upcoming = headphones_api('getUpcoming')
    except Exception as e:
        return headphones_exception(e)

    if upcoming == []:
        upcoming = 'empty'

    for album in upcoming:
        try:
            album['ThumbURL'] = hp_albumart(album['AlbumID'])
        except:
            pass

    try:
        wanted = headphones_api('getWanted')
    except Exception as e:
        return headphones_exception(e)

    if wanted == []:
        wanted = 'empty'

    for album in wanted:
        try:
            album['ThumbURL'] = hp_albumart(album['AlbumID'])
        except:
            pass

    return render_template('headphones.html',
        upcoming=upcoming,
        wanted=wanted,
        headphones=True,
        compact=hp_compact(),
    )


@app.route('/xhr/headphones/similar/')
@requires_auth
def xhr_headphones_similar():
    logger.log('HEADPHONES :: Fetching similar artists', 'INFO')

    try:
        headphones = headphones_api('getSimilar')
    except Exception as e:
        return headphones_exception(e)

    return render_template('headphones-similar.html',
        similar=headphones,
        headphones=True,
    )


@app.route('/xhr/headphones/history/')
@requires_auth
def xhr_headphones_history():
    logger.log('HEADPHONES :: Fetching history', 'INFO')

    try:
        headphones = headphones_api('getHistory')
    except Exception as e:
        return headphones_exception(e)

    return render_template('headphones-history.html',
        history=headphones,
        headphones=True,
    )


@app.route('/xhr/headphones/search/<type>/<query>/')
@requires_auth
def xhr_headphones_search(type, query):
    if type == 'artist':
        logger.log('HEADPHONES :: Searching for artist', 'INFO')
        command = 'findArtist&name=%s' % urllib.quote(query)
    else:
        logger.log('HEADPHONES :: Searching for album', 'INFO')
        command = 'findAlbum&name=%s' % urllib.quote(query)

    try:
        headphones = headphones_api(command)
    except Exception as e:
        return headphones_exception(e)

    for artist in headphones:
        artist['url'].replace('\/', '/')

    return render_template('headphones-search_dialog.html',
        headphones=True,
        search=headphones,
        query=query
    )


@app.route('/xhr/headphones/artist/<artistid>/<action>/')
@requires_auth
def xhr_headphones_artist_action(artistid, action):
    if action == 'pause':
        logger.log('HEADPHONES :: Pausing artist', 'INFO')
        command = 'pauseArtist&id=%s' % artistid
    elif action == 'resume':
        logger.log('HEADPHONES :: Resuming artist', 'INFO')
        command = 'resumeArtist&id=%s' % artistid
    elif action == 'refresh':
        logger.log('HEADPHONES :: Refreshing artist', 'INFO')
        command = 'refreshArtist&id=%s' % artistid
    elif action == 'remove':
        logger.log('HEADPHONES :: Removing artist', 'INFO')
        command = 'delArtist&id=%s' % artistid
    elif action == 'add':
        logger.log('HEADPHONES :: Adding artist', 'INFO')
        command = 'addArtist&id=%s' % artistid

    try:
        if command == 'remove':
            headphones_api(command, False)
        elif command == 'pause':
            headphones_api(command, False)
        elif command == 'resume':
            headphones_api(command, False)
        else:
            Thread(target=headphones_api, args=(command, False)).start()
    except Exception as e:
        return headphones_exception(e)

    return jsonify(status='successful')


@app.route('/xhr/headphones/album/<albumid>/<status>/')
@requires_auth
def xhr_headphones_album_status(albumid, status):
    if status == 'wanted':
        logger.log('HEADPHONES :: Marking album as wanted', 'INFO')
        command = 'queueAlbum&id=%s' % albumid
    if status == 'wanted_new':
        logger.log('HEADPHONES :: Marking album as wanted (new)', 'INFO')
        command = 'queueAlbum&new=True&id=%s' % albumid
    if status == 'skipped':
        logger.log('HEADPHONES :: Marking album as skipped', 'INFO')
        command = 'unqueueAlbum&id=%s' % albumid

    try:
        Thread(target=headphones_api, args=(command, False)).start()
    except Exception as e:
        return headphones_exception(e)

    return jsonify(status='successful')


@app.route('/xhr/headphones/control/<command>/')
@requires_auth
def xhr_headphones_control(command):
    if command == 'shutdown':
        logger.log('HEADPHONES :: Shutting down', 'INFO')

    elif command == 'restart':
        logger.log('HEADPHONES :: Restarting', 'INFO')

    elif command == 'update':
        logger.log('HEADPHONES :: Updating', 'INFO')

    elif command == 'force_search':
        logger.log('HEADPHONES :: Forcing wanted album search', 'INFO')
        command = 'forceSearch'

    elif command == 'force_process':
        logger.log('HEADPHONES :: Forcing post process', 'INFO')
        command = 'forceProcess'

    try:
        Thread(target=headphones_api, args=(command, False)).start()
    except Exception as e:
        return headphones_exception(e)

    return jsonify(status='successful')
Something went wrong with that request. Please try again.