| 1 | #!/usr/bin/python3 |
| 2 | # Simple PartyMode Web Console |
| 3 | # Copyright (c) 2015,2016 Clinton Ebadi <clinton@unknownlamer.org> |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | # Trivial xbmc admin script to view active playlist, control volume, |
| 18 | # etc. |
| 19 | |
| 20 | # I would not recommend putting this online, no attempt is made at |
| 21 | # being even trivially secure (e.g. form values are passed directly to |
| 22 | # kodi with zero verification) |
| 23 | |
| 24 | # ADD COMMAND TO RESTART PARTY MODE |
| 25 | # (probably should require confirmation) |
| 26 | # also add undelete link to post-del link just in case (reinsert at old pos) |
| 27 | |
| 28 | # todo |
| 29 | # any kind of error checking |
| 30 | |
| 31 | import cgi, cgitb |
| 32 | from datetime import datetime |
| 33 | import hashlib |
| 34 | import html |
| 35 | import numbers |
| 36 | import os |
| 37 | import random |
| 38 | import subprocess |
| 39 | import urllib |
| 40 | from kodijson import Kodi |
| 41 | from yattag import Doc |
| 42 | |
| 43 | import partyparty |
| 44 | from partyparty import Song, SongControls, Search, Playlist, PartyManager |
| 45 | |
| 46 | cgitb.enable() |
| 47 | #PAGE_SELF = os.environ['SCRIPT_NAME'] if 'SCRIPT_NAME' in os.environ else '' |
| 48 | PAGE_SELF = os.environ['SCRIPT_NAME'].rsplit('/', 1)[-1] if 'SCRIPT_NAME' in os.environ else '' |
| 49 | #PAGE_SELF = 'admin.cgi' |
| 50 | |
| 51 | print ("content-type: text/html; charset=utf-8\n\n") |
| 52 | print ("<!DOCTYPE html>\n<html><head><title>partyparty beb</title></head><body>") |
| 53 | print (partyparty.css ()) |
| 54 | |
| 55 | #print (os.environ) |
| 56 | #print (os.environ['SCRIPT_NAME']) |
| 57 | |
| 58 | def show_menu (): |
| 59 | doc, tag, text = Doc().tagtext() |
| 60 | with tag ('ul', klass = 'horiz-menu flex_row'): |
| 61 | for target, description in [('#playlist', 'playlist'), |
| 62 | ('#controls', 'controls'), |
| 63 | ('javascript:document.getElementById("quicksearch").focus()', 'search'), |
| 64 | (PAGE_SELF, 'reload')]: |
| 65 | with tag ('li'): |
| 66 | with tag ('a', href = target): |
| 67 | text (description) |
| 68 | |
| 69 | print (doc.getvalue ()) |
| 70 | |
| 71 | xbmc = partyparty.connect (Kodi ("http://localhost:8080/jsonrpc")) |
| 72 | |
| 73 | def print_escaped (item): |
| 74 | print (u"<p>{}</p>".format (html.escape (u"{}".format (item)))) |
| 75 | |
| 76 | show_menu () |
| 77 | |
| 78 | manager = PartyManager (cgi.FieldStorage ()) |
| 79 | manager.process () |
| 80 | |
| 81 | playlist = Playlist() |
| 82 | #playpos = random.randint (1, totalitems / (1 if 'asap' not in form else 3)) |
| 83 | |
| 84 | class PlayerControls: |
| 85 | def __init__ (self, name='controls'): |
| 86 | self.name = name |
| 87 | |
| 88 | def info (self): |
| 89 | doc, tag, text = Doc().tagtext() |
| 90 | _playtime = xbmc.Player.GetProperties (playerid=0, properties = ['position', 'percentage', 'time', 'totaltime']) |
| 91 | pt = _playtime['result'] if 'result' in _playtime else None |
| 92 | with tag ('ul', klass = 'horiz-menu'): |
| 93 | for infotext in ['Volume {}%'.format(xbmc.Application.GetProperties (properties=['volume'])['result']['volume']), |
| 94 | 'Time {:02d}:{:02d} / {:02d}:{:02d} ({:.2f}%) @ {:%H:%M:%S}'.format (pt['time']['hours'] * 60 + pt['time']['minutes'], pt['time']['seconds'], pt['totaltime']['hours'] * 60 + pt['totaltime']['minutes'], pt['totaltime']['seconds'], pt['percentage'], datetime.now())]: |
| 95 | with tag ('li'): |
| 96 | text (infotext) |
| 97 | |
| 98 | return doc.getvalue () |
| 99 | |
| 100 | |
| 101 | |
| 102 | controls = PlayerControls () |
| 103 | print (controls.info ()) |
| 104 | |
| 105 | print ('<a name="controls"></a>') |
| 106 | print (''' |
| 107 | <form method="post" action="{}" style="display: inline-block"> |
| 108 | <button name="volchange" value="5" type="submit">+5</button> |
| 109 | <button name="volchange" value="-5" type="submit">-5</button> |
| 110 | |
| 111 | <button name="volchange" value="10" type="submit">+10</button> |
| 112 | <button name="volchange" value="-10" type="submit">-10</button> |
| 113 | |
| 114 | <button name="volmute" value="1">Toggle Mute</button> |
| 115 | |
| 116 | </form> |
| 117 | '''.format (html.escape (PAGE_SELF))) |
| 118 | |
| 119 | print (''' |
| 120 | <form method="post" action="{}" style="display: inline-block"> |
| 121 | <button name="navigate" value="prev" type="submit">⏮</button> |
| 122 | <button name="navigate" value="next" type="submit">⏭</button> |
| 123 | <button name="navigate" value="playpause" type="submit">⏯</button> |
| 124 | </form> |
| 125 | '''.format (html.escape (PAGE_SELF))) |
| 126 | |
| 127 | print ('<a name="playlist"></a><h1>Playlist</h1>') |
| 128 | print (playlist.show ()) |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | print ('<a name="search"></a>') |
| 134 | Search ().show_quick_search (thereal=True) |
| 135 | show_menu () |
| 136 | |
| 137 | print ('<form method="post" action="{}" style="display: inline-block">'.format (html.escape (PAGE_SELF))) |
| 138 | print ('<button name="partyon" value="true">re-enable party</button>') |
| 139 | #print ('<button name="lockon" value="true">lock em out</button>') |
| 140 | print ('<button name="lights" value="on">lights on</button>') |
| 141 | print ('<button name="lights" value="off">lights off</button>') |
| 142 | print ('</form>') |
| 143 | print ('</body></html>') |