party-upload: port to python 3
[clinton/unknownlamer-kodi-addons.git] / party-upload / upload.cgi
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 # todo:
18 # fix weird characters (see erica's song)
19 # use shutil.copyfileobj to possibly speed up copying
20 # - check if it actually causes chunked reads from the browser
21 # - LOL WHAT uploads are like 10s don't bother
22 # support multiple files
23 # - daemonize replaygain? NOPE it runs in like no time thanks to moar power
24 # track uploads (overcomplicated version)
25 # - phone ip is hopefully good enough
26 # - track # of uses of asap/Next Please
27 # - track files we have queued (need at least next-please so we can queue those in order)
28 # track uploads (easy version)
29 # - dump timestamps into file (/tmp = cleared at reboot, no need to clear it manually probably)
30 # - sum up # of songs uploading in last N minutes
31 # - throttle asap position based on the number of songs being uploaded
32 # - e.g. no one in last PERIOD = go next, fuckton of people = asap just ain't working
33
34
35 # integrate better with native queue
36
37
38 ## crazy idea: if the playlist can be watched with a script... (maybe
39 ## a json rpc notification? but ... reliability, and upload.cgi runs
40 ## infrequently enough that it cannot record the playlist and extract
41 ## a meaningful image of the queue ...).
42
43 # diff playlist whenever item is inserted (or song changes, or whatever we can hook on).
44 # scan for common items (discarding the head of the old snapshot, since we could just be advancing)
45 # when the first different item is found... if it ain't the end of the list it's been queued
46 # keep going until we match up again and record the end of the queue position in a file
47 # what about when we delete in the list?
48 #
49
50 # upload.html
51 # - show playlist length, # of next/asap available, likely queue position
52 # - show upcoming songs (5? just next-please?)
53 # - - just use SSI + python virtual includes, seriously
54
55 import cgi, cgitb
56 import os, sys
57 import daemon
58 import subprocess
59 import random
60 from kodijson import Kodi
61
62 import partyparty
63 from partyparty import Upload, PartyManager
64
65 cgitb.enable()
66
67 print "content-type: text/html\n\n"
68 sys.stdout.flush ()
69
70 form = cgi.FieldStorage ()
71
72 manager = PartyManager (form)
73
74 print '<p><a href="upload.html">Upload another song</a></p>'
75 sys.stdout.flush ()
76
77 xbmc = partyparty.connect (Kodi ("http://localhost:8080/jsonrpc"))
78 manager.randomqueue ({"file": filename}, 1 if 'asap' not in form else 3)
79
80 # todo: use REMOTE_ADDR to limit how many asap requests a person can
81 # make in a time period
82 #totalitems = xbmc.Playlist.GetItems (playlistid=0)['result']['limits']['total']
83 #playpos = random.randint (1, totalitems / (1 if 'asap' not in form else 3))
84
85 #print xbmc.Playlist.Insert (playlistid=0, item={"file": filename }, position=playpos)
86
87 #print '<p style="font-size: x-large">Your song is number {0} in the queue ({1} songs in playlist).</p>'.format (playpos, totalitems+1)