示例#1
0
def xhr_notify_message():
    label = str(request.form['label'])
    hostname = str(request.form['hostname'])
    message = str(request.form['message'])
    title = str(request.form['title'])

    if title == "Title":
        title = "Maraschino"

    if server_type() == "XBMC":
        port = 9777
        icon = os.path.join(RUNDIR, 'static', 'images', 'notifications', request.form['image'])

        if not os.path.exists(icon):
            icon = os.path.join(RUNDIR, 'static', 'images', 'maraschino_logo.png')


        if icon[-3:] == "png":
            icon_type = ICON_PNG
        elif icon[-3:] == "jpg":
            icon_type = ICON_JPEG
        elif icon[-4:] == "jpeg":
            icon_type = ICON_JPEG
        elif icon[-3:] == "gif":
            icon_type = ICON_GIF
        else:
            icon_type = ICON_NONE

        addr = (hostname, port)
        sock = socket(AF_INET,SOCK_DGRAM)

        try:
            logger.log('NOTIFY XBMC :: Sending message to %s' % label, 'INFO')
            packet = PacketNOTIFICATION(title, message, icon_type, icon)
            packet.send(sock, addr)
            return jsonify({ 'status': 'successful'})
        except:
            logger.log('NOTIFY XBMC :: Message failed to send', 'ERROR')
            return jsonify({ 'error': 'Message failed to send'})
    elif server_type() == "PLEX":

        try:
            logger.log('NOTIFY XBMC :: Sending message %s' % label, 'INFO')
            server = PLEXLibrary(server_address())
            for connectedclient in server.getClients():
                client = PLEXClient(connectedclient['host'], connectedclient['port'])
                client.sendmessage(title + "," + message)
                
            return jsonify({'status': 'successful'})
        except Exception, e:
            logger.log('NOTIFY XBMC :: Message failed to send', 'ERROR')
            logger.log('NOTIFY XBMC :: Error %s' % e, 'ERROR')
            return jsonify({'error': 'Message failed to send'})
示例#2
0
def xhr_currently_playing():
    try:
        api_address = server_api_address()

        if not api_address:
            raise Exception
        if (server_type()=="XBMC"):
            xbmc = jsonrpclib.Server(api_address)
            active_player = xbmc.Player.GetActivePlayers()
            playerid = active_player[0]['playerid']
            player_info = xbmc.Player.GetProperties(playerid=playerid, properties=['time', 'totaltime', 'position', 'percentage', 'repeat', 'shuffled'])
            volume = xbmc.Application.GetProperties(properties=['volume'])['volume']
    
            if active_player[0]['type'] == 'video':
                currently_playing = xbmc.Player.GetItem(playerid = 1, properties = ['title', 'season', 'episode', 'duration', 'showtitle', 'fanart', 'tvshowid', 'plot', 'thumbnail', 'year'])['item']
    
            if active_player[0]['type'] == 'audio':
                currently_playing = xbmc.Player.GetItem(playerid = 0, properties = ['title', 'duration', 'fanart', 'artist', 'albumartist', 'album', 'track', 'artistid', 'albumid', 'thumbnail', 'year'])['item']
    
            fanart = currently_playing['fanart']
            itemart = currently_playing['thumbnail']
        elif (server_type()=='PLEX'):
            plexlibrary=PLEXLibrary(server_address())
            
            currently_playing,player_info=plexlibrary.currently_playing()
            for currently_playingplayer in range (0,len(currently_playing)):
                current=currently_playing[currently_playingplayer]
                if current['playstatus'] in ['Playing','Paused']:
                    currently_playing=current
                    player_info=player_info[currently_playingplayer]
                    fanart = currently_playing['fanart']
                    itemart = currently_playing['thumbnail']
                    volume=100
                    break
            if (currently_playing['playstatus']=='Stopped'):
                raise Exception
    except:
        return jsonify({ 'playing': False })

    return render_template('currently_playing.html',
        servertype=server_type(),
        currently_playing = currently_playing,
        fanart = fanart,
        itemart = itemart,
        shuffled = player_info['shuffled'],
        repeat = player_info['repeat'],
        volume = volume,
        current_time = format_time(player_info['time']),
        total_time = format_time(player_info['totaltime']),
        percentage_progress = int(player_info['percentage']),
        total_time_seconds = player_info['totaltime']['hours']*3600+player_info['totaltime']['minutes']*60+player_info['totaltime']['seconds'],
    )
示例#3
0
def xhr_notify_message():
    label = str(request.form["label"])
    hostname = str(request.form["hostname"])
    message = str(request.form["message"])
    title = str(request.form["title"])
    if title == "Title":
        title = "Maraschino"
    if server_type() == "XBMC":
        port = 9777
        icon = os.path.join(RUNDIR, "static", "images", "notifications", request.form["image"])

        if not os.path.exists(icon):
            icon = os.path.join(RUNDIR, "static", "images", "maraschino_logo.png")

        if icon[-3:] == "png":
            icon_type = ICON_PNG
        elif icon[-3:] == "jpg":
            icon_type = ICON_JPEG
        elif icon[-4:] == "jpeg":
            icon_type = ICON_JPEG
        elif icon[-3:] == "gif":
            icon_type = ICON_GIF
        else:
            icon_type = ICON_NONE

        addr = (hostname, port)
        sock = socket(AF_INET, SOCK_DGRAM)

        try:
            logger.log("NOTIFY  :: Sending message to %s" % label, "INFO")
            packet = PacketNOTIFICATION(title, message, icon_type, icon)
            packet.send(sock, addr)
            return jsonify({"status": "successful"})
        except:
            logger.log("NOTIFY  :: Message failed to send", "ERROR")
            return jsonify({"error": "Message failed to send"})
    else:
        try:
            logger.log("NOTIFY  :: Sending message to %s" % label, "INFO")
            server = PLEXLibrary(server_address())
            for connectedclient in server.getclients():
                client = PLEXClient(connectedclient.host, connectedclient.port)
                client.sendmessage(title + "," + message)
            return jsonify({"status": "successful"})
        except:
            logger.log("NOTIFY  :: Message failed to send", "ERROR")
            return jsonify({"error": "Message failed to send"})
示例#4
0
def xhr_controls(command):
    if (server_type()=="XBMC"):
        serversettings = server_settings()
        xbmc = jsonrpclib.Server(server_api_address())
    
        try:
            active_player = xbmc.Player.GetActivePlayers()
            if active_player[0]['type'] == 'video':
                playerid = 1
            elif active_player[0]['type'] == 'audio':
                playerid = 0
        except:
            active_player = None
    
        if command == 'play_pause':
            logger.log('CONTROLS :: Play/Pause', 'INFO')
            try:
                xbmc.Player.PlayPause(playerid=playerid)
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        elif command == 'stop':
            logger.log('CONTROLS :: Stop', 'INFO')
            try:
                xbmc.Player.Stop(playerid=playerid)
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        elif 'volume' in command:
            logger.log('CONTROLS :: Volume', 'INFO')
            try:
                volume = command.split('_')
                volume = int(volume[1])
                xbmc.Application.SetVolume(volume=volume)
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        elif command == 'next':
            logger.log('CONTROLS :: Next', 'INFO')
            try:
                xbmc.Player.GoNext(playerid=playerid)
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        elif command == 'previous':
            logger.log('CONTROLS :: Previous', 'INFO')
            try:
                xbmc.Player.GoPrevious(playerid=playerid)
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        elif command == 'fast_forward':
            logger.log('CONTROLS :: Fast forward', 'INFO')
            try:
                xbmc.Player.SetSpeed(playerid=playerid, speed='increment')
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        elif command == 'rewind':
            logger.log('CONTROLS :: Rewind', 'INFO')
            try:
                xbmc.Player.SetSpeed(playerid=playerid, speed='decrement')
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        elif 'seek' in command:
            logger.log('CONTROLS :: Seek', 'INFO')
            try:
                percentage = command.split('_')
                percentage = int(percentage[1])
                xbmc.Player.Seek(playerid=playerid, value=percentage)
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        elif command == 'shuffle':
            logger.log('CONTROLS :: Shuffle', 'INFO')
            try:
                shuffled = xbmc.Player.GetProperties(playerid=playerid, properties=['shuffled'])['shuffled']
                if shuffled == True:
                    xbmc.Player.UnShuffle(playerid=playerid)
                else:
                    xbmc.Player.Shuffle(playerid=playerid)
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        elif command == 'repeat':
            logger.log('CONTROLS :: Repeat', 'INFO')
            try:
                states = ['off', 'one', 'all']
                repeat = xbmc.Player.GetProperties(playerid=playerid, properties=['repeat'])['repeat']
                state = states.index(repeat)
                if state <= 1:
                    state = state + 1
                else:
                    state = 0
    
                state = states[state]
                xbmc.Player.Repeat(playerid=playerid, state=state)
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        elif command == 'update_video':
            logger.log('CONTROLS :: Updating video library', 'INFO')
            try:
                xbmc.VideoLibrary.Scan()
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        elif command == 'clean_video':
            logger.log('CONTROLS :: Cleaning video library', 'INFO')
            try:
                xbmc.VideoLibrary.Clean()
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        elif command == 'update_audio':
            logger.log('CONTROLS :: Updating audio library', 'INFO')
            try:
                xbmc.AudioLibrary.Scan()
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        elif command == 'clean_audio':
            logger.log('CONTROLS :: Cleaning audio library', 'INFO')
            try:
                xbmc.AudioLibrary.Clean()
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        elif command == 'poweroff':
            logger.log('CONTROLS :: Shutting down XBMC machine', 'INFO')
            try:
                xbmc.System.Shutdown()
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        elif command == 'suspend':
            logger.log('CONTROLS :: Suspending XBMC machine', 'INFO')
            try:
                xbmc.System.Suspend()
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        elif command == 'reboot':
            logger.log('CONTROLS :: Rebooting XBMC machine', 'INFO')
            try:
                xbmc.System.Reboot()
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        elif command == 'poweron':
            logger.log('CONTROLS :: Powering on XBMC machine', 'INFO')
            server_macaddress = serversettings['mac_address']
    
            if not server_macaddress:
                logger.log('CONTROLS :: No XBMC machine MAC address defined', 'ERROR')
                return jsonify({ 'failed': True })
    
            else:
                try:
                    addr_byte = server_macaddress.split(':')
                    hw_addr = struct.pack('BBBBBB',
                    int(addr_byte[0], 16),
                    int(addr_byte[1], 16),
                    int(addr_byte[2], 16),
                    int(addr_byte[3], 16),
                    int(addr_byte[4], 16),
                    int(addr_byte[5], 16))
    
                    msg = '\xff' * 6 + hw_addr * 16
                    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                    s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
                    s.sendto(msg, ("255.255.255.255", 9))
                    return_response = 'success'
    
                except:
                    logger.log('CONTROLS :: Failed to send WOL packet', 'ERROR')
                    return_response = 'failed'
                    
    elif (server_type()=="PLEX"):
        mediaplayer = PLEXLibrary(server_address())
        active_players=mediaplayer.active_players()
        active_player=active_players[0]
        if command == 'play_pause':
            logger.log('CONTROLS :: Play/Pause', 'INFO')
            try:
                mediaplayer.do_action (active_player['host'], 'playback/pause')
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
                
        elif command == 'stop':
            logger.log('CONTROLS :: Stop', 'INFO')
            try:
                mediaplayer.do_action (active_player['host'], 'playback/stop')
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
        elif command == 'fast_forward':
            logger.log('CONTROLS :: Fast forward', 'INFO')
            try:
                mediaplayer.do_action (active_player['host'], 'playback/fastForward')
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
                
        elif command == 'rewind':
            logger.log('CONTROLS :: Rewind', 'INFO')
            try:
                mediaplayer.do_action (active_player['host'], 'playback/rewind')
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
                
        elif command == 'next':
            logger.log('CONTROLS :: Next', 'INFO')
            try:
                mediaplayer.do_action (active_player['host'], 'playback/skipNext')
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        elif command == 'previous':
            logger.log('CONTROLS :: Previous', 'INFO')
            try:
                mediaplayer.do_action (active_player['host'], 'playback/skipPrevious')
                return_response = 'success'
            except:
                logger.log('CONTROLS :: %s' % xbmc_error, 'ERROR')
                return_response = 'failed'
    
        return_response="success"
        
        
    if return_response == 'success':
        return jsonify({ 'success': True })
    else:
        return jsonify({ 'failed': True })
示例#5
0
def xhr_play_media(file_type, media_type, media_id):
    logger.log('CONTROLS :: Playing %s' % media_type, 'INFO')
    position = 0

    if file_type == 'video':
        id = 1
    else:
        id = 0

    try:
        xhr_clear_playlist(id)
    except:
        logger.log('CONTROLS :: Failed to clear %s playlist' % file_type, 'DEBUG')
        return jsonify({ 'failed': True })

    if server_type()=="XBMC":
        xbmc = jsonrpclib.Server(server_api_address())
    elif server_type()=="PLEX":
        plexlibrary=PLEXLibrary(server_address(),TVLibID=get_setting_value('plex_tvlib_id'),MovieLibID=get_setting_value('plex_movielib_id'))

    if file_type == 'video':

        if media_type == 'tvshow':
            try:
                tvshow_episodes = xbmc.VideoLibrary.GetEpisodes(tvshowid=media_id, sort={ 'method': 'episode' })['episodes']
                for episode in tvshow_episodes:
                    episodeid = episode['episodeid']
                    item = {'episodeid': episodeid}
                    xbmc.Playlist.Add(playlistid=1, item=item)

            except:
                logger.log('CONTROLS :: Failed to retrieve episodes', 'DEBUG')
                return jsonify({'failed': True})

        elif 'season' in media_type:
            if server_type()=="XBMC":
                media_type = media_type.split('_')
                season = int(media_type[1])

                try:
                    tvshow_episodes = xbmc.VideoLibrary.GetEpisodes(tvshowid=media_id, season=season, sort={ 'method': 'episode' })['episodes']
                    for episode in tvshow_episodes:
                        episodeid = episode['episodeid']
                        item = {'episodeid': episodeid}
                        xbmc.Playlist.Add(playlistid=1, item=item)

                except:
                    logger.log('CONTROLS :: Failed to retrieve episodes', 'DEBUG')
                    return jsonify({'failed': True})
            elif server_type()=="PLEX":
                pass   
        else:
            try:
                if server_type()=="XMBC":
                    item = { media_type + 'id': media_id }
                    xbmc.Playlist.Add(playlistid=1, item=item)
                elif server_type()=="PLEX":
                    item=str(media_id)
            except:
                logger.log('CONTROLS :: Failed to add %s to playlist' % media_type, 'DEBUG')
                return jsonify({'failed': True})

    else:

        try:
            if media_type == 'song' and get_setting_value('xbmc_songs_play_album') == '1':
                song = xbmc.AudioLibrary.GetSongDetails(songid=media_id, properties=['albumid', 'track'])['songdetails']
                item = {'albumid': song['albumid']}
                position = song['track'] -1
            else:
                item = {media_type+'id': media_id}
            xbmc.Playlist.Add(playlistid=0, item=item)
        except:
            logger.log('CONTROLS :: Failed to add %s to playlist' % media_type, 'DEBUG')
            return jsonify({'failed': True})

    try:
        if server_type()=="XBMC":
            item = {'playlistid': id, 'position': position}
            xbmc.Player.Open(item)
        elif server_type()=="PLEX":
            plexlibrary.playFile (item,plexlibrary.getClients()[0]['host'])
    except:
        logger.log('CONTROLS :: Failed to open %s playlist' % file_type, 'DEBUG')
        return jsonify({'failed': True})

    return jsonify({'success': True})