#!/usr/bin/python3
# -*- coding: utf-8 -*-
import time
import json
import os
import sys
import time
import urllib
import socket
import argparse
import requests
import lib.common as common

base_url = 'http://localhost:24879/player/'

#------------------------------------------------------------------------------#
#                         do something on startup                              #
#------------------------------------------------------------------------------#
def init():
    global port
    check_port()
    script_path = os.path.dirname(os.path.abspath(__file__))
    os.chdir(script_path)
    parser = argparse.ArgumentParser(description='media manager spotify connect service')
    parser.add_argument('-p', '--port', type=int, help='WEB server port', required=True)
    args = parser.parse_args()
    port = args.port

#------------------------------------------------------------------------------#
#                         check if librespot-java is running                   #
#------------------------------------------------------------------------------#
def check_port():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    result = sock.connect_ex(('localhost', 24879))
    if result == 0:
        sock.close()
        return
    print("Please check if SpoCon is configured correctly and running", file = sys.stderr )
    sock.close()
    exit(1)

#------------------------------------------------------------------------------#
#                  get metadata from spotify                                   #
#------------------------------------------------------------------------------#
def get_metadata():
    meta_data = {}
    global current_cover
    try:
        current_track = get_player()
        album = current_track['item']['album']
        current_cover = album['images'][0]['url']
        tmp_cover = current_cover
        tmp_cover=tmp_cover.replace('https://i.scdn.co/image/','')
        meta_data['track']  = current_track['item']['name']
        meta_data['album']  = album['name']
        meta_data['artist'] = album['artists'][0]['name']
        meta_data['cover']  = 'external_' + tmp_cover
        meta_data['playstatus'] = get_play_status()
        if meta_data['playstatus'] == False:
            meta_data['track']  = ''
            meta_data['album']  = ''
            meta_data['artist'] = ''
            meta_data['cover']  = 'images/pause.png'
        return(bytes(json.dumps(meta_data), 'utf-8'))
    except:
        meta_data['track']  = ''
        meta_data['album']  = ''
        meta_data['artist'] = ''
        meta_data['cover']  = 'images/pause.png'
        meta_data['playstatus'] = False
        return(bytes(json.dumps(meta_data), 'utf-8'))

#------------------------------------------------------------------------------#
#                  get play status                                             #
#------------------------------------------------------------------------------#
def get_play_status(mode=False):
    playing = False
    ret_val = False
    ret_str = 'NO'
    try:
        current_track = get_player()
        playing = current_track['is_playing']
    except:
        pass
    if playing == True:
        try:
            path = 'http://localhost:24879/player/current/'
            ret = requests.post(url = path)
            data = ret.json()
            if 'current' in data:
                ret_str = 'YES'
                ret_val = True
                get_player()
        except:
            pass
    if mode:
        return(bytes(ret_str, 'utf-8'))
    return(ret_val)

#------------------------------------------------------------------------------#
#                  get whats currently playing                                 #
#------------------------------------------------------------------------------#
def get_current():
    path = 'http://localhost:24879/player/current/'
    ret = requests.post(url = path)
    return ret.json()

#------------------------------------------------------------------------------#
#                  get player data from API                                    #
#------------------------------------------------------------------------------#
def get_player():
    path = 'http://localhost:24879/web-api/v1/me/player'
    ret = requests.get(url = path)
    return ret.json()

#------------------------------------------------------------------------------#
#          read cover image fom spotify connect web                            #
#------------------------------------------------------------------------------#
def read_cover_image():
    webURL = urllib.request.urlopen(current_cover)
    data = webURL.read()
    return(data)

#------------------------------------------------------------------------------#
#         play next song                                                       #
#------------------------------------------------------------------------------#
def next():
    requests.post(url = base_url + 'next')

#------------------------------------------------------------------------------#
#         play previuous song                                                  #
#------------------------------------------------------------------------------#
def prev():
    requests.post(url = base_url + 'prev')

#------------------------------------------------------------------------------#
#         start playing                                                        #
#------------------------------------------------------------------------------#
def play():
    requests.post(url = base_url + 'resume')

#------------------------------------------------------------------------------#
#         stop playing                                                         #
#------------------------------------------------------------------------------#
def pause():
    requests.post(url = base_url + 'pause')

#------------------------------------------------------------------------------#
#                       handle http get request                                #
#------------------------------------------------------------------------------#
def respond_to_get_request(data):
    if 'action' not in data:
        return(bytes('failed', 'utf-8'))
    if data['action'] == 'play':
        play()
    elif data['action'] == 'pause':
        pause()
    elif data['action'] == 'prev':
        get_metadata()
        prev()
    elif data['action'] == 'next':
        get_metadata()
        next()
    elif data['action'] == 'metadata':
        return(get_metadata())
    elif data['action'] == 'coverimage':
        return(read_cover_image())
    elif data['action'] == 'getplaystatus':
        return(get_play_status(True))
    return(bytes('OK', 'utf-8'))

#------------------------------------------------------------------------------#
#           main program                                                       #
#------------------------------------------------------------------------------#
init()
common.http_get_handler = respond_to_get_request
common.run_http(port)
while True:
    time.sleep(2000)

[((7049, 7070), 'lib.common.run_http', 'common.run_http', (['port'], {}), '(port)\n', (7064, 7070), True, 'import lib.common as common\n'), ((587, 608), 'os.chdir', 'os.chdir', (['script_path'], {}), '(script_path)\n', (595, 608), False, 'import os\n'), ((622, 698), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ([], {'description': '"""media manager spotify connect service"""'}), "(description='media manager spotify connect service')\n", (645, 698), False, 'import argparse\n'), ((1113, 1162), 'socket.socket', 'socket.socket', (['socket.AF_INET', 'socket.SOCK_STREAM'], {}), '(socket.AF_INET, socket.SOCK_STREAM)\n', (1126, 1162), False, 'import socket\n'), ((3909, 3932), 'requests.post', 'requests.post', ([], {'url': 'path'}), '(url=path)\n', (3922, 3932), False, 'import requests\n'), ((4286, 4308), 'requests.get', 'requests.get', ([], {'url': 'path'}), '(url=path)\n', (4298, 4308), False, 'import requests\n'), ((4614, 4651), 'urllib.request.urlopen', 'urllib.request.urlopen', (['current_cover'], {}), '(current_cover)\n', (4636, 4651), False, 'import urllib\n'), ((4954, 4990), 'requests.post', 'requests.post', ([], {'url': "(base_url + 'next')"}), "(url=base_url + 'next')\n", (4967, 4990), False, 'import requests\n'), ((5253, 5289), 'requests.post', 'requests.post', ([], {'url': "(base_url + 'prev')"}), "(url=base_url + 'prev')\n", (5266, 5289), False, 'import requests\n'), ((5552, 5590), 'requests.post', 'requests.post', ([], {'url': "(base_url + 'resume')"}), "(url=base_url + 'resume')\n", (5565, 5590), False, 'import requests\n'), ((5854, 5891), 'requests.post', 'requests.post', ([], {'url': "(base_url + 'pause')"}), "(url=base_url + 'pause')\n", (5867, 5891), False, 'import requests\n'), ((7087, 7103), 'time.sleep', 'time.sleep', (['(2000)'], {}), '(2000)\n', (7097, 7103), False, 'import time\n'), ((556, 581), 'os.path.abspath', 'os.path.abspath', (['__file__'], {}), '(__file__)\n', (571, 581), False, 'import os\n'), ((2441, 2462), 'json.dumps', 'json.dumps', (['meta_data'], {}), '(meta_data)\n', (2451, 2462), False, 'import json\n'), ((3296, 3319), 'requests.post', 'requests.post', ([], {'url': 'path'}), '(url=path)\n', (3309, 3319), False, 'import requests\n'), ((2695, 2716), 'json.dumps', 'json.dumps', (['meta_data'], {}), '(meta_data)\n', (2705, 2716), False, 'import json\n')]