<commit_before># stdlib
import os
from urllib import urlencode, unquote
from urllib2 import urlopen
import random
import time
from contextlib import closing
from Queue import Queue, Empty

# 3rd party
from geventwebsocket.handler import WebSocketHandler
from gevent.pywsgi import WSGIServer
from gevent import sleep
from flask import Flask, jsonify, request, Response, render_template, json, g, redirect

# radiocrepe
from radiocrepe.storage import DistributedStorage
from radiocrepe.util import load_config

app = Flask(__name__)


queue = []
playing = None
messages = {}


def broadcast(mtype, time, uid):
    global messages
    for queue in messages.itervalues():
        queue.put((mtype, time, uid))


def message(msg):
    mtype, ts, uid = msg

    storage = DistributedStorage.bind(app.config)
    meta = storage.get(uid, None)

    return json.dumps({'op': mtype, 'time_add': ts, 'data': meta})


@app.route('/song/<uid>/')
def song(uid):
    """
    retrieve the song from one of the storage nodes
    """
    storage = DistributedStorage.bind(app.config)
    meta = storage.get(uid, None)
    if meta is None:
        return 'song not found', 404
    else:
        address = storage.node_registry.get_address(meta['node_id'])
        return redirect('http://{0}/song/{1}/'.format(address, meta['uid']))


@app.route('/node/upload/', methods=['POST'])
def node_recv():
    """
    Receive metadata from the nodes
    """
    storage = DistributedStorage.bind(app.config)
    registry = storage.node_registry
    node = request.form.get('node_id')
    data = request.form.get('songs')
    server = request.form.get('server')

    if not registry.get(node):
        registry.attach(node, server)
    storage.update_node(node, json.loads(data))
    return ''


@app.route('/node/detach/', methods=['POST'])
def node_detach():
    """
    Detach from a node
    """
    registry = DistributedStorage.bind(app.config).node_registry
    registry.detach(request.form.get('node_id'))


@app.route('/enqueue/', methods=['POST'])
def enqueue():
    uid = request.form.get('uid')
    if uid in storage:
        ts = time.time()
        queue.append((ts, uid))
        broadcast('add', ts, uid)
        return jsonify({'id': uid})
    else:
        return Response(jsonify(result='ERR_NO_SUCH_SONG', id=uid).data,
                        mimetype='application/json', status=404)
        return jsonify(), 404


@app.route('/notify/start/', methods=['POST'])
def _notify_start():
    global playing
    try:
        playing = queue.pop(0)
        broadcast('play', playing[0], playing[1])
        return json.dumps(len(queue))
    except IndexError:
        return Response(jsonify(result='ERR_NO_NEXT').data,
                        mimetype='application/json', status=404)


@app.route('/notify/stop/', methods=['POST'])
def _notify_stop():
    global playing
    broadcast('stop', playing[0], playing[1])
    playing = None
    return ''


@app.route('/queue/')
def _queue():
    storage = DistributedStorage.bind(app.config)
    res = []
    for ts, uid in queue:
        elem = storage.get(uid, None)
        elem['uid'] = uid
        elem['time_add'] = ts
        res.append(elem)
    return json.dumps(res)


@app.route('/updates/')
def updates():
    global messages

    if request.environ.get('wsgi.websocket'):
        ws = request.environ['wsgi.websocket']
        queue = Queue()
        messages[id(request)] = queue
        try:
            while True:
                # receive stuff here
                try:
                    while True:
                        msg = message(queue.get_nowait())
                        ws.send(msg)
                except Empty:
                    sleep(1)
        finally:
            del messages[id(request)]
        return ''
    return 'I can haz websockets?'


@app.route('/playing/')
def _playing():
    if playing:
        storage = DistributedStorage.bind(app.config)
        meta = storage.get(playing[1], None)
        meta['time_add'] = playing[0]
    else:
        meta = None
    return Response(json.dumps(meta),
                    mimetype='application/json')


@app.route('/artist/<name>/')
def artist_info(name):
    if app.config.get('lastfm_key'):
        params = {<commit_msg>Fixed problem with broadcast<commit_after># stdlib
import os
from urllib import urlencode, unquote
from urllib2 import urlopen
import random
import time
from contextlib import closing
from Queue import Queue, Empty

# 3rd party
from geventwebsocket.handler import WebSocketHandler
from gevent.pywsgi import WSGIServer
from gevent import sleep, greenlet
from flask import Flask, jsonify, request, Response, render_template, json, g, redirect

# radiocrepe
from radiocrepe.storage import DistributedStorage
from radiocrepe.util import load_config

app = Flask(__name__)


queue = []
playing = None
messages = {}


def broadcast(mtype, time, uid):
    global messages
    for queue in messages.itervalues():
        queue.put((mtype, time, uid))


def message(msg):
    mtype, ts, uid = msg

    storage = DistributedStorage.bind(app.config)
    meta = storage.get(uid, None)

    return json.dumps({'op': mtype, 'time_add': ts, 'data': meta})


@app.route('/song/<uid>/')
def song(uid):
    """
    retrieve the song from one of the storage nodes
    """
    storage = DistributedStorage.bind(app.config)
    meta = storage.get(uid, None)
    if meta is None:
        return 'song not found', 404
    else:
        address = storage.node_registry.get_address(meta['node_id'])
        return redirect('http://{0}/song/{1}/'.format(address, meta['uid']))


@app.route('/node/upload/', methods=['POST'])
def node_recv():
    """
    Receive metadata from the nodes
    """
    storage = DistributedStorage.bind(app.config)
    registry = storage.node_registry
    node = request.form.get('node_id')
    data = request.form.get('songs')
    server = request.form.get('server')

    if not registry.get(node):
        registry.attach(node, server)
    storage.update_node(node, json.loads(data))
    return ''


@app.route('/node/detach/', methods=['POST'])
def node_detach():
    """
    Detach from a node
    """
    registry = DistributedStorage.bind(app.config).node_registry
    registry.detach(request.form.get('node_id'))


@app.route('/enqueue/', methods=['POST'])
def enqueue():
    uid = request.form.get('uid')
    if uid in storage:
        ts = time.time()
        queue.append((ts, uid))
        broadcast('add', ts, uid)
        return jsonify({'id': uid})
    else:
        return Response(jsonify(result='ERR_NO_SUCH_SONG', id=uid).data,
                        mimetype='application/json', status=404)
        return jsonify(), 404


@app.route('/notify/start/', methods=['POST'])
def _notify_start():
    global playing
    try:
        playing = queue.pop(0)
        broadcast('play', playing[0], playing[1])
        return json.dumps(len(queue))
    except IndexError:
        return Response(jsonify(result='ERR_NO_NEXT').data,
                        mimetype='application/json', status=404)


@app.route('/notify/stop/', methods=['POST'])
def _notify_stop():
    global playing
    broadcast('stop', playing[0], playing[1])
    playing = None
    return ''


@app.route('/queue/')
def _queue():
    storage = DistributedStorage.bind(app.config)
    res = []
    for ts, uid in queue:
        elem = storage.get(uid, None)
        elem['uid'] = uid
        elem['time_add'] = ts
        res.append(elem)
    return json.dumps(res)


@app.route('/updates/')
def updates():
    global messages
    guid = id(greenlet.getcurrent())

    if request.environ.get('wsgi.websocket'):
        ws = request.environ['wsgi.websocket']
        queue = Queue()
        messages[guid] = queue
        try:
            while True:
                # receive stuff here
                try:
                    while True:
                        msg = message(queue.get_nowait())
                        ws.send(msg)
                except Empty:
                    sleep(1)
        finally:
            del messages[guid]
        return ''
    return 'I can haz websockets?'


@app.route('/playing/')
def _playing():
    if playing:
        storage = DistributedStorage.bind(app.config)
        meta = storage.get(playing[1], None)
        meta['time_add'] = playing[0]
    else:
        meta = None
    return Response(json.dumps(meta),
                    mimetype='application/json')


@app.route('/artist/<name>/')
def artist_info(name):
    if app.config.get('lastfm_key'):
        params = {
{'fraction_non_alphanumeric': 0.13461988304093567, 'fraction_numerical': 0.005614035087719298, 'mean_word_length': 2.649594536918481, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7208188', 'n_tokens_mistral': 2675, 'n_tokens_neox': 2552, 'n_words': 626}