From a823df94489456e88feb3fd42055803d3ceb0877 Mon Sep 17 00:00:00 2001 From: Clinton Ebadi Date: Mon, 27 Apr 2015 01:30:52 -0400 Subject: [PATCH] Trivial upload scripts for parties If you have friends and they don't agree with your music choices all the time (or, hey, who doesn't like listening to their favorite music?), this script allows anyone to upload music from their phone to the active kodi playlist. Attempts to be minimally fair through randomness. Ugly, but more-or-less functional. --- party-upload/README | 10 +++++ party-upload/upload.cgi | 89 ++++++++++++++++++++++++++++++++++++++++ party-upload/upload.html | 26 ++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 party-upload/README create mode 100755 party-upload/upload.cgi create mode 100644 party-upload/upload.html diff --git a/party-upload/README b/party-upload/README new file mode 100644 index 0000000..5ad758c --- /dev/null +++ b/party-upload/README @@ -0,0 +1,10 @@ +Trivial Party Mode Upload Script + +Requires python-xbmc . + +Throw upload.cgi and upload.html into a directory together, generate a +QR code URL for friends to scan, and now anyone can upload music from +their phones to your party playlist. No configuration file, modify +upload.cgi directly if your user/pass differ from the default. + +Patches to add a config file or anything really are welcome. \ No newline at end of file diff --git a/party-upload/upload.cgi b/party-upload/upload.cgi new file mode 100755 index 0000000..8ac3918 --- /dev/null +++ b/party-upload/upload.cgi @@ -0,0 +1,89 @@ +#!/usr/bin/python + +# Simple PartyMode Upload Script +# Copyright (c) 2015 Clinton Ebadi +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# todo: +# fix weird characters (see erica's song) +# track uploads (overcomplicated version) +# - phone ip is hopefully good enough +# - track # of uses of asap/Next Please +# - track files we have queued (need at least next-please so we can queue those in order) +# track uploads (easy version) +# - dump timestamps into file (/tmp = cleared at reboot, no need to clear it manually probably) +# - sum up # of songs uploading in last N minutes +# - throttle asap position based on the number of songs being uploaded +# - e.g. no one in last PERIOD = go next, fuckton of people = asap just ain't working + + +# integrate better with native queue (if it existed...) + +# upload.html +# - show playlist length, # of next/asap available, likely queue position +# - show upcoming songs (5? just next-please?) +# - - just use SSI + python virtual includes, seriously + +import cgi, cgitb +import os, sys +import daemon +import subprocess +import random +from xbmcjson import XBMC + +cgitb.enable() + +print "content-type: text/html\n\n" + +music_dir = "/srv/archive/incoming/stolen-moosic" +form = cgi.FieldStorage () + +# Evil: just run replaygain/mp3gain/metaflac on the file and hope one +# works instead of dealing with MIME. For now. +def attempt_rpgain (file_name): + subprocess.call (["/usr/bin/vorbisgain", "-q", file_name]) + subprocess.call (["/usr/bin/mp3gain", "-q", "-s", "i", file_name]) + subprocess.call (["/usr/bin/aacgain", "-q", "-s", "i", file_name]) + subprocess.call (["/usr/bin/metaflac", "--add-replay-gain", file_name]) + +def save_uploaded_file (form_field, upload_dir): + if form_field not in form: + print "

No file provided.

" + sys.exit () + fileitem = form[form_field] + fout = file (os.path.join(upload_dir, fileitem.filename), 'wb') + fout.write (fileitem.value) + fout.close() + return fileitem.filename + +song_name = save_uploaded_file ("song", music_dir) + +print '

Upload another song

' +sys.stdout.flush () + +#with daemon.DaemonContext (): +attempt_rpgain ("{0}/{1}".format (music_dir, song_name)) + +xbmc = XBMC ("http://localhost:8080/jsonrpc") + +# todo: use REMOTE_ADDR to limit how many asap requests a person can +# make in a time period +totalitems = xbmc.Playlist.GetItems (playlistid=0)['result']['limits']['total'] +playpos = random.randint (1, totalitems / (1 if 'asap' not in form else 3)) + +print xbmc.Playlist.Insert (playlistid=0, item={"file": "{0}/{1}".format (music_dir, song_name)}, position=playpos) + +print '

Your song is number {0} in the queue ({1} songs in playlist).

'.format (playpos, totalitems+1) + + diff --git a/party-upload/upload.html b/party-upload/upload.html new file mode 100644 index 0000000..787a17e --- /dev/null +++ b/party-upload/upload.html @@ -0,0 +1,26 @@ + + + + + + +
+

+ + + + + +

+
+ + -- 2.20.1