Trivial upload scripts for parties
[clinton/unknownlamer-kodi-addons.git] / party-upload / upload.cgi
1 #!/usr/bin/python
2
3 # Simple PartyMode Upload Script
4 # Copyright (c) 2015 Clinton Ebadi <clinton@unknownlamer.org>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 # todo:
19 # fix weird characters (see erica's song)
20 # track uploads (overcomplicated version)
21 # - phone ip is hopefully good enough
22 # - track # of uses of asap/Next Please
23 # - track files we have queued (need at least next-please so we can queue those in order)
24 # track uploads (easy version)
25 # - dump timestamps into file (/tmp = cleared at reboot, no need to clear it manually probably)
26 # - sum up # of songs uploading in last N minutes
27 # - throttle asap position based on the number of songs being uploaded
28 # - e.g. no one in last PERIOD = go next, fuckton of people = asap just ain't working
29
30
31 # integrate better with native queue (if it existed...)
32
33 # upload.html
34 # - show playlist length, # of next/asap available, likely queue position
35 # - show upcoming songs (5? just next-please?)
36 # - - just use SSI + python virtual includes, seriously
37
38 import cgi, cgitb
39 import os, sys
40 import daemon
41 import subprocess
42 import random
43 from xbmcjson import XBMC
44
45 cgitb.enable()
46
47 print "content-type: text/html\n\n"
48
49 music_dir = "/srv/archive/incoming/stolen-moosic"
50 form = cgi.FieldStorage ()
51
52 # Evil: just run replaygain/mp3gain/metaflac on the file and hope one
53 # works instead of dealing with MIME. For now.
54 def attempt_rpgain (file_name):
55 subprocess.call (["/usr/bin/vorbisgain", "-q", file_name])
56 subprocess.call (["/usr/bin/mp3gain", "-q", "-s", "i", file_name])
57 subprocess.call (["/usr/bin/aacgain", "-q", "-s", "i", file_name])
58 subprocess.call (["/usr/bin/metaflac", "--add-replay-gain", file_name])
59
60 def save_uploaded_file (form_field, upload_dir):
61 if form_field not in form:
62 print "<p>No file provided.</p>"
63 sys.exit ()
64 fileitem = form[form_field]
65 fout = file (os.path.join(upload_dir, fileitem.filename), 'wb')
66 fout.write (fileitem.value)
67 fout.close()
68 return fileitem.filename
69
70 song_name = save_uploaded_file ("song", music_dir)
71
72 print '<p><a href="upload.html">Upload another song</a></p>'
73 sys.stdout.flush ()
74
75 #with daemon.DaemonContext ():
76 attempt_rpgain ("{0}/{1}".format (music_dir, song_name))
77
78 xbmc = XBMC ("http://localhost:8080/jsonrpc")
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": "{0}/{1}".format (music_dir, song_name)}, 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)
88
89