Skip to content

Commit

Permalink
Merge pull request #47 from tsukanov/live
Browse files Browse the repository at this point in the history
Live submission feed
  • Loading branch information
mayhem committed Nov 29, 2014
2 parents 5139c0c + 9b21762 commit 9f70ae2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
27 changes: 26 additions & 1 deletion acousticbrainz/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from hashlib import sha256
from urllib import quote_plus
import argparse
from flask_socketio import SocketIO, emit


SANITY_CHECK_KEYS = [
[ 'metadata', 'version', 'essentia' ],
Expand Down Expand Up @@ -46,6 +48,8 @@
static_folder = STATIC_FOLDER,
template_folder = TEMPLATE_FOLDER)

socketio = SocketIO(app)

whitelist_file = os.path.join(os.path.dirname(__file__), "tagwhitelist.json")
whitelist_tags = set(json.load(open(whitelist_file)))

Expand Down Expand Up @@ -258,6 +262,10 @@ def faq():
def data():
return render_template("data.html")

@socketio.on('connect')
def websocket_connect():
return "Connected."

@app.route("/<mbid>/low-level", methods=["POST"])
def submit_low_level(mbid):
"""Endpoint for submitting low-level information to AcousticBrainz"""
Expand Down Expand Up @@ -320,6 +328,17 @@ def submit_low_level(mbid):
"VALUES (%s, %s, %s, %s, %s)",
(mbid, build_sha1, data_sha256, is_lossless_submit, data_json))
conn.commit()

# Sending broadcast via WebSocket
if 'artist' not in data['metadata']['tags']:
data['metadata']['tags']['artist'] = ["[unknown]"]
if 'title' not in data['metadata']['tags']:
data['metadata']['tags']['title'] = ["[unknown]"]
socketio.emit('new submission', {
'mbid': mbid,
'artist': data['metadata']['tags']['artist'],
'title': data['metadata']['tags']['title'],
})
return ""

app.logger.info("Already have %s" % data_sha256)
Expand Down Expand Up @@ -480,5 +499,11 @@ def get_summary(mbid):
parser.add_argument("-d", "--debug", help="Turn on debugging mode to see stack traces in the error pages", default=True, action='store_true')
parser.add_argument("-t", "--host", help="Which interfaces to listen on. Default: 127.0.0.1", default="127.0.0.1", type=str)
parser.add_argument("-p", "--port", help="Which port to listen on. Default: 8080", default="8080", type=int)

args = parser.parse_args()
app.run(debug=True, host=args.host, port=args.port)
app.config["DEBUG"] = args.debug

from gevent import monkey
monkey.patch_all()

socketio.run(app, host=args.host, port=args.port)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ Flask == 0.10.1
psycopg2 == 2.5.3
Jinja2 == 2.7.2
Werkzeug == 0.9.4
Flask-SocketIO == 0.4.1
python-memcached
pyyaml
2 changes: 2 additions & 0 deletions static/js/socket.io.min.js

Large diffs are not rendered by default.

22 changes: 20 additions & 2 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ <h5>Examples</h5>
<p>
If you're wondering what this collected data actually looks like, have a look at the last 5 tracks that have been submitted:
</p>
<ol class="list" style="width: 100%">
<ol id="live-feed" class="list" style="width: 100%">
{% for mbid, artist, recording in last_submitted_data %}
<li><a href="/{{ mbid }}"><strong>{{ artist }} / {{ recording }}</strong></a></li>
<li><a href="/{{ mbid }}"><strong>{{ recording }} by {{ artist }}</strong></a></li>
{% endfor %}
</ol>
</div>
Expand Down Expand Up @@ -77,3 +77,21 @@ <h5>Examples</h5>
</div>
</div>
{%- endblock -%}

{%- block scripts -%}
{{ super() }}
<script type="text/javascript" src="{{ url_for('static', filename='js/socket.io.min.js') }}"></script>
<script type="text/javascript" charset="utf-8">
var socket = io.connect('http://' + document.domain + ':' + location.port);
var feed = $('#live-feed');
socket.emit('connect');
socket.on('new submission', function(msg) {
$('<li><a href="/' + msg.mbid +'"><strong>' + msg.title + ' by ' + msg.artist + '</strong></a></li>')
.hide().prependTo(feed).fadeIn("slow");
var items = feed.children();
if (items.length > 5) {
items.last().fadeOut("slow", function() { $(this).remove(); });
}
});
</script>
{%- endblock -%}

0 comments on commit 9f70ae2

Please sign in to comment.