<commit_before># -*- coding: utf-8 -*-

"""
    zeit.recommend.outlet
    ~~~~~~~~~~~~~~~~~~~~~

    This module has no description.

    Copyright: (c) 2013 by <NAME>.
    License: BSD, see LICENSE for more details.
"""

from storm import Bolt
from storm import emitBolt
from storm import log
from threading import Thread
from urllib import urlencode
from urllib import urlopen
from ws4py.server.wsgirefserver import WebSocketWSGIRequestHandler
from ws4py.server.wsgirefserver import WSGIServer
from ws4py.server.wsgiutils import WebSocketWSGIApplication
from ws4py.websocket import WebSocket
from wsgiref.simple_server import make_server
import json

# TODO: Global variables shouldn't be the solution.
_clients = {}
_server = None


class OutletWebSocket(WebSocket):
    def received_message(self, message):
        uid = str(message)
        if len(uid) and _server:
            log('[OutletWebSocket] Client connected: %s' % uid)
            index = _server.manager.websockets.values().index(self)
            _clients[uid] = _server.manager.websockets.keys()[index]
            emitBolt(['connect', uid], stream='control')


class OutletBolt(Bolt):
    def initialize(self, conf, context):
        host = conf.get('zeit.recommend.zonapi.host', 'localhost')
        port = conf.get('zeit.recommend.zonapi.port', 9200)
        self.url = 'http://%s:%s/solr/select' % (host, port)

    def resolve_paths(self, paths):
        for path in paths:
            params = dict(
                wt='json',
                q='href:*%s' % path,
                fl=('title,teaser_text,keyword,release_date,author,department,'
                    'sub_department,href')
                )
            try:
                data = urlopen(self.url, urlencode(params)).read()
                yield json.loads(data)['response']['docs'][0]
            except Exception:
                yield dict(
                    title='zeit.de' + path,
                    teaser_text=('Lorem ipsum dolor sit amet, consetetur sadip'
                                 'scing elitr, sed diam nonumy eirmod tempor i'
                                 'nvidunt ut labore et dolore magna sed est.'),
                    href='http://www.zeit.de' + path
                    )

    def process(self, tup):
        user, requested, recommended = tup.values

        message = dict(
            recommended=list(self.resolve_paths(recommended)),
            requested=list(self.resolve_paths(requested))
            )

        # TODO: Disable broadcasting and whitelist user IDs.
        # if user in _clients:
        #     ws = _server.manager.websockets[_clients[user]]
        for ws in _server.manager.websockets.values():
            ws.send(json.dumps(message))<commit_msg>Remove broadcasting and enable msg routing<commit_after># -*- coding: utf-8 -*-

"""
    zeit.recommend.outlet
    ~~~~~~~~~~~~~~~~~~~~~

    This module has no description.

    Copyright: (c) 2013 by <NAME>.
    License: BSD, see LICENSE for more details.
"""

from storm import Bolt
from storm import emitBolt
from storm import log
from threading import Thread
from urllib import urlencode
from urllib import urlopen
from ws4py.server.wsgirefserver import WebSocketWSGIRequestHandler
from ws4py.server.wsgirefserver import WSGIServer
from ws4py.server.wsgiutils import WebSocketWSGIApplication
from ws4py.websocket import WebSocket
from wsgiref.simple_server import make_server
import json

# TODO: Global variables shouldn't be the solution.
_clients = {}
_server = None


class OutletWebSocket(WebSocket):
    def received_message(self, message):
        uid = str(message)
        if len(uid) and _server:
            log('[OutletWebSocket] Client connected: %s' % uid)
            index = _server.manager.websockets.values().index(self)
            _clients[uid] = _server.manager.websockets.keys()[index]
            emitBolt(['connect', uid], stream='control')


class OutletBolt(Bolt):
    def initialize(self, conf, context):
        host = conf.get('zeit.recommend.zonapi.host', 'localhost')
        port = conf.get('zeit.recommend.zonapi.port', 9200)
        self.url = 'http://%s:%s/solr/select' % (host, port)

    def resolve_paths(self, paths):
        for path in paths:
            params = dict(
                wt='json',
                q='href:*%s' % path,
                fl=('title,teaser_text,keyword,release_date,author,department,'
                    'sub_department,href')
                )
            try:
                data = urlopen(self.url, urlencode(params)).read()
                yield json.loads(data)['response']['docs'][0]
            except Exception:
                yield dict(
                    title='zeit.de' + path,
                    teaser_text=('Lorem ipsum dolor sit amet, consetetur sadip'
                                 'scing elitr, sed diam nonumy eirmod tempor i'
                                 'nvidunt ut labore et dolore magna sed est.'),
                    href='http://www.zeit.de' + path
                    )

    def process(self, tup):
        user, requested, recommended = tup.values

        message = dict(
            recommended=list(self.resolve_paths(recommended)),
            requested=list(self.resolve_paths(requested))
            )

        if user in _clients:
            ws = _server.manager.websockets[_clients[user]]
            ws.send(json.dumps(message))
{'fraction_non_alphanumeric': 0.11024643320363164, 'fraction_numerical': 0.005188067444876783, 'mean_word_length': 2.2518072289156628, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1343094', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1513, 'n_words': 385}