Esempio n. 1
0
def create_ticket():
    logger.log_access(request)
    message = messaging.get_request_data(request)
    encrypted_data = cryptography.strip_clear_signed_message(message)
    decrypted_data = cryptography.two_layer_sym_asym_decrypt(
        encrypted_data, data.private_key)

    client_identity, transactor_id, timestamp, symmetric_key = decrypted_data.split(
        SEP)

    if data.identifier != transactor_id:
        warn_message = 'transactor id does not match.'
        logger.log_warn(warn_message)
        return warn_message, 403

    client_access_symmetric_key = cryptography.generate_symmetric_key()
    client_address = request.remote_addr.encode('utf-8')
    ticket_start_timestamp, ticket_end_timestamp = generator.get_ticket_life_span(
    )
    ticket_unencrypted = SEP.join([
        client_access_symmetric_key, client_identity, client_address,
        ticket_start_timestamp, ticket_end_timestamp
    ])

    ticket = cryptography.encrypt_asym(ticket_unencrypted, data.public_key)

    response = cryptography.encrypt_sym(
        SEP.join([ticket, client_access_symmetric_key]), symmetric_key)

    logger.log_info(
        'generated access ticket and key for client id `{}`'.format(
            client_identity))

    return response, 200
Esempio n. 2
0
    def __close_channel(self):
        """Call to close the channel with RabbitMQ cleanly by issuing the
        Channel.Close RPC command.

        """
        log_info('Closing the channel')
        self._channel.close()
Esempio n. 3
0
    def __stop_consuming(self):
        """Tell RabbitMQ that you would like to stop consuming by sending the
        Basic.Cancel RPC command.

        """
        if self._channel:
            log_info('Sending a Basic.Cancel RPC command to RabbitMQ')
            self._channel.basic_cancel(self.__on_cancelok, self._consumer_tag)
Esempio n. 4
0
 def get_initial_sleep_time():
     '''get_initial_sleep_time'''
     time_now = datetime.datetime.now()
     logger.log_info('Time now: {}'.format(time_now))
     sleep_time = (60 - time_now.minute) * 60 - (time_now.second)
     logger.log_info(
         'Time until until next hour starts: {} seconds'.format(sleep_time))
     return sleep_time
Esempio n. 5
0
    def __on_bindok(self, unused_frame):
        """Invoked by pika when the Queue.Bind method has completed. At this
        point we will start consuming messages by calling start_consuming
        which will invoke the needed RPC commands to start the process.

        :param pika.frame.Method unused_frame: The Queue.BindOk response frame

        """
        log_info('Queue bound, Consumer are ready!!!')
Esempio n. 6
0
    def __acknowledge_message(self, delivery_tag):
        """Acknowledge the message delivery from RabbitMQ by sending a
        Basic.Ack RPC method for the delivery tag.

        :param int delivery_tag: The delivery tag from the Basic.Deliver frame

        """
        log_info('Acknowledging message {}'.format(delivery_tag))
        self._channel.basic_ack(delivery_tag)
Esempio n. 7
0
def get_corpus_data(path):
    sent_pairs = []
    for filename in os.listdir(path):
        log_info("reading from file {0} ...".format(path + '/' + filename))
        f = open(path + '/' + filename)
        soup = BeautifulSoup(f.read(), "lxml")
        for sentence in soup.findAll("original"):
            compressed = soup.findAll("compressed", id=sentence['id'])[0]
            sent_pairs.append((sentence.text, compressed.text))
    return sent_pairs
Esempio n. 8
0
    def __connect(self):
        """This method connects to RabbitMQ, returning the connection handle.
        When the connection is established, the on_connection_open method
        will be invoked by pika.

        :rtype: pika.SelectConnection

        """
        log_info('Connecting to {}'.format(self._url))
        return pika.SelectConnection(pika.URLParameters(self._url), self.__on_connection_open,
                                     stop_ioloop_on_close=False)
Esempio n. 9
0
    def __on_cancelok(self, unused_frame):
        """This method is invoked by pika when RabbitMQ acknowledges the
        cancellation of a consumer. At this point we will close the channel.
        This will invoke the on_channel_closed method once the channel has been
        closed, which will in-turn close the connection.

        :param pika.frame.Method unused_frame: The Basic.CancelOk frame

        """
        log_info('RabbitMQ acknowledged the cancellation of the consumer')
        self.__close_channel()
Esempio n. 10
0
    def start(self):
        '''start'''
        sleep_time = WeatherService.get_initial_sleep_time()
        logger.log_info('Sleeping for {} seconds'.format(sleep_time))
        time.sleep(sleep_time)

        wu_location = self.data_service_client.get_wu_location_default()
        if wu_location is None:
            raise Exception('Default location for WU not found')

        while True:
            self.run_owm_collect()
            self.run_wu_collect(wu_location)
            time.sleep(serviceconfig.UPDATE_INTERVAL)
Esempio n. 11
0
    def __on_queue_declareok(self, method_frame):
        """Method invoked by pika when the Queue.Declare RPC call made in
        setup_queue has completed. In this method we will bind the queue
        and exchange together with the routing key by issuing the Queue.Bind
        RPC command. When this command is complete, the on_bindok method will
        be invoked by pika.

        :param pika.frame.Method method_frame: The Queue.DeclareOk frame

        """
        log_info('Binding {} to {} with {}'.format(self.EXCHANGE,
                                                   self.QUEUE, self.ROUTING_KEY))
        for key in self.ROUTING_KEY:
            self._channel.queue_bind(
                self.__on_bindok, self.QUEUE, self.EXCHANGE, key)
Esempio n. 12
0
    def stop(self):
        """Cleanly shutdown the connection to RabbitMQ by stopping the consumer
        with RabbitMQ. When RabbitMQ confirms the cancellation, on_cancelok
        will be invoked by pika, which will then closing the channel and
        connection. The IOLoop is started again because this method is invoked
        when CTRL-C is pressed raising a KeyboardInterrupt exception. This
        exception stops the IOLoop which needs to be running for pika to
        communicate with RabbitMQ. All of the commands issued prior to starting
        the IOLoop will be buffered but not processed.

        """
        log_info('Stopping')
        self._closing = True
        self.__stop_consuming()
        self._connection.ioloop.start()
        log_info('Stopped')
Esempio n. 13
0
    def send_wu(data):
        '''send_wu'''
        # Skipping unique validation, let's always save the data
        #if not self.validate_unique_wu(data):
        #    return None

        dump = json.dumps(data)

        try:
            hdrs = {'Content-type': 'application/json'}
            response = requests.post(serviceconfig.URL_DATA_SERVICE_WEATHER2,
                                     headers=hdrs,
                                     data=dump)
            logger.log_info(response)
        except Exception as err:
            logger.log_error('Failed to send WU weather data', err)
Esempio n. 14
0
    def update_transaction_context(self, transaction_id, product_request_data,
                                   product_id, bid, price, peer_id):
        if transaction_id not in self.transaction_context:
            logger.log_info(
                'added context for transaction `{}`'.format(transaction_id))
        else:
            logger.log_info(
                'updated context for transaction `{}`'.format(transaction_id))

        self.transaction_context[transaction_id] = {
            'transaction_id': transaction_id,
            'product_request_data': product_request_data,
            'product_id': product_id,
            'bid': bid,
            'price': price,
            'peer_id': peer_id
        }
Esempio n. 15
0
    def __on_message(self, unused_channel, basic_deliver, properties, body):
        """Invoked by pika when a message is delivered from RabbitMQ. The
        channel is passed for your convenience. The basic_deliver object that
        is passed in carries the exchange, routing key, delivery tag and
        a redelivered flag for the message. The properties passed in is an
        instance of BasicProperties with the message properties and the body
        is the message that was sent.

        :param pika.channel.Channel unused_channel: The channel object
        :param pika.Spec.Basic.Deliver: basic_deliver method
        :param pika.Spec.BasicProperties: properties
        :param str|unicode body: The message body

        """
        log_info('Received message # {} from {}: {}'.format(basic_deliver.delivery_tag,
                                                            properties.app_id, body))
        self.chatbot.handle(data=json.loads(body))
        self.__acknowledge_message(basic_deliver.delivery_tag)
Esempio n. 16
0
    def validate_unique_owm(data_dict):
        '''Validates that data service doesn't already contain the OWM data'''
        try:
            filters = {'locid': data_dict['locid'], 'time': data_dict['time']}
            payload = {'where': json.dumps(filters)}
            response = requests.get(serviceconfig.URL_DATA_SERVICE_WEATHER,
                                    params=payload)

            if len(response.json()['_items']) == 0:
                return True
            else:
                logger.log_info(
                    'OWM values already stored - locid={}, time={}'.format(
                        filters['locid'], filters['time']))

        except Exception as err:
            logger.log_error('Failed to validate unique OWM weather data', err)

        return False
Esempio n. 17
0
    def __handle_response(self, message, data, type_reply, user_id, source):
        """
        handle response
        :param message: str
        :param data: list
        :param type_reply: str
        :param recipient_id: str
        :return:
        """
        log_info(
            '######## HANDLE RESPONSE FOR USER [{}] ########'.format(user_id))

        if type_reply == "text":
            self.response.response_text(user_id=user_id,
                                        content=message[:2000],
                                        obj=data,
                                        source=source)
        elif type_reply == "quick_replies":
            self.response.response_quick_reply(user_id=user_id,
                                               content=message[:2000],
                                               obj=data,
                                               source=source)
Esempio n. 18
0
    #get labels and features
    features = get_features_recursive(original_tree, original.split(' '))
    labels = get_labels_recursive(original_tree, compressed_tree)
    return (features, labels)


if __name__ == '__main__':
    training_features = []
    training_labels = []

    if USE_BROADCAST:
        training_sentences = []
        for folder in os.listdir(BROADCAST_CORPUS_PATH):
            training_sentences += get_corpus_data(BROADCAST_CORPUS_PATH +
                                                  folder)
        log_info("{0} sentences found".format(len(training_sentences)))
        for i in range(len(training_sentences)):
            if i >= MAX_SENTS:
                break
            log_info("processing sentence {0} of {1}".format(
                i, len(training_sentences)))
            feats, labels = extract_training_data(training_sentences[i])
            training_features += feats
            training_labels += labels
    if USE_WRITTEN:
        training_sentences = get_corpus_data(WRITTEN_CORPUS_PATH)
        log_info("{0} sentences found".format(len(training_sentences)))
        for i in range(len(training_sentences)):
            if i >= MAX_SENTS:
                break
            log_info("processing sentence {0} of {1}".format(
Esempio n. 19
0
        result, client_identity, price_to_pay, product_id, merchant_identity,
        product_key, epoid_plain
    ])

    transaction_receipt_dsa_signed = cryptography.dsa_sign(
        transaction_receipt_plain, data.dsa_private_key)

    flags = b''

    client_receipt_plain = SEP.join([
        epoid_plain, client_account_number,
        generator.get_price_bytes(data.credit_accounts[client_identity][1]),
        flags
    ])

    client_receipt = cryptography.encrypt_sym(client_receipt_plain,
                                              client_sym_key)

    response_plain = SEP.join([transaction_receipt_dsa_signed, client_receipt])
    response = cryptography.encrypt_sym(response_plain, merchant_sym_key)

    return response, 200


if __name__ == '__main__':
    try:
        run_server(config.ADDRESS_BOOK[config.TRANSACTOR_ID])
    except KeyboardInterrupt:
        logger.log_info('keyboard interrupt, exiting...')
        exit(0)
Esempio n. 20
0
    def handle_message(self, user_id=0, input_text="", page_id=0, source=""):
        """
        Handle input is text of user
        :param recipient_id: int
        :param input_text: str
        :param page_id: int
        :param source: str
        """

        data = list()
        log_info('######## USER [{} - {}] SEND NEW MESSAGE ########'.format(
            user_id, page_id))
        check_exists_create(fb_id=user_id, page_id=page_id)
        snips = self.nlp.get_snips(input_text)
        if source == 'facebook':
            if snips.get("intent") == 'define':
                message = get_response_random(STR_DEFINE)
                type_reply = "quick_replies"
                data = [{
                    "content_type": "text",
                    "title": "Linh Vật Seagames",
                    "payload": "<POSTBACK_PAYLOAD>",
                }, {
                    "content_type": "text",
                    "title": "Quốc gia chủ nhà",
                    "payload": "<POSTBACK_PAYLOAD>",
                }, {
                    "content_type": "text",
                    "title": "Danh sách quốc gia",
                    "payload": "<POSTBACK_PAYLOAD>",
                }]
            elif snips.get("intent") == 'host':
                message = get_response_random(STR_HOST)
                type_reply = "text"
            elif snips.get("intent") == 'list_sports':
                message = get_response_random(STR_LST_SPORTS)
                type_reply = "text"
            elif snips.get("intent") == 'list_countries':
                message = get_response_random(STR_LST_NUMBER_CONTRIES)
                type_reply = "text"
            elif snips.get("intent") == 'mascot':
                message = get_response_random(STR_MASCOT)
                type_reply = "text"
            elif snips.get("intent") == 'rank':
                message = get_ranking()
                type_reply = "text"
            elif snips.get("intent") == 'athele':
                message = get_athlete(snips.get("entity").get("is_name")[0])
                data = [{
                    "content_type": "text",
                    "title": "Nguyễn Công Phượng",
                    "payload": "<POSTBACK_PAYLOAD>",
                }, {
                    "content_type": "text",
                    "title": "Nguyễn Quang Hải",
                    "payload": "<POSTBACK_PAYLOAD>",
                }]
                type_reply = "quick_replies"
            elif snips.get("intent") == 'schedule':
                data = [{
                    "content_type": "text",
                    "title": "Lịch thi đấu hôm nay",
                    "payload": "<POSTBACK_PAYLOAD>",
                }, {
                    "content_type": "text",
                    "title": "Kết quả thi đấu hôm qua",
                    "payload": "<POSTBACK_PAYLOAD>",
                }, {
                    "content_type": "text",
                    "title": "Bảng xếp hạng",
                    "payload": "<POSTBACK_PAYLOAD>",
                }]
                message = get_schedule(
                    snips.get("entity").get("date_start"),
                    snips.get("entity").get("date_stop"), snips.get("entity"))
                type_reply = "quick_replies"
            elif snips.get("intent") == 'result':
                data = [{
                    "content_type": "text",
                    "title": "Lịch thi đấu hôm nay",
                    "payload": "<POSTBACK_PAYLOAD>",
                }, {
                    "content_type": "text",
                    "title": "Kết quả thi đấu hôm qua",
                    "payload": "<POSTBACK_PAYLOAD>",
                }, {
                    "content_type": "text",
                    "title": "Bảng xếp hạng",
                    "payload": "<POSTBACK_PAYLOAD>",
                }]
                message = get_result(
                    snips.get("entity").get("date_start"),
                    snips.get("entity").get("date_stop"), snips.get("entity"))
                type_reply = "quick_replies"
            else:
                type_reply = "text"
                message = "Mời bạn nhập lại câu hỏi"
        if source == 'zalo':
            if snips.get("intent") == 'define':
                message = get_response_random(STR_DEFINE)
                type_reply = "quick_replies"
            elif snips.get("intent") == 'host':
                message = get_response_random(STR_HOST)
                type_reply = "text"
            elif snips.get("intent") == 'list_sports':
                message = get_response_random(STR_LST_SPORTS)
                type_reply = "text"
            elif snips.get("intent") == 'list_countries':
                message = get_response_random(STR_LST_NUMBER_CONTRIES)
                type_reply = "text"
            elif snips.get("intent") == 'mascot':
                message = get_response_random(STR_MASCOT)
                type_reply = "text"
            elif snips.get("intent") == 'rank':
                message = get_ranking()
                type_reply = "text"
            elif snips.get("intent") == 'athele':
                data = [{
                    "title": "Nguyễn Quang Hải",
                    "type": "oa.query.show",
                    "payload": "Nguyễn Quang Hải"
                }, {
                    "title": "Nguyễn Công Phượng",
                    "type": "oa.query.show",
                    "payload": "Nguyễn Công Phượng"
                }, {
                    "title": "Lương Xuân Trường",
                    "type": "oa.query.show",
                    "payload": "Lương Xuân Trường"
                }]
                message = get_athlete(snips.get("entity").get("is_name")[0])
                type_reply = "quick_replies"
            elif snips.get("intent") == 'schedule':
                data = [{
                    "title": "Lịch thi đấu hôm nay",
                    "type": "oa.query.show",
                    "payload": "Lịch thi đấu hôm nay"
                }, {
                    "title": "Kết quả thi đấu hôm nay",
                    "type": "oa.query.show",
                    "payload": "Kết quả thi đấu hôm nay"
                }, {
                    "title": "Bảng xếp hạng",
                    "type": "oa.query.show",
                    "payload": "Bảng xếp hạng"
                }]
                message = get_schedule(
                    snips.get("entity").get("date_start"),
                    snips.get("entity").get("date_stop"), snips.get("entity"))
                type_reply = "quick_replies"
            elif snips.get("intent") == 'result':
                data = [{
                    "title": "Lịch thi đấu hôm nay",
                    "type": "oa.query.show",
                    "payload": "Lịch thi đấu hôm nay"
                }, {
                    "title": "Kết quả thi đấu hôm nay",
                    "type": "oa.query.show",
                    "payload": "Kết quả thi đấu hôm nay"
                }, {
                    "title": "Bảng xếp hạng",
                    "type": "oa.query.show",
                    "payload": "Bảng xếp hạng"
                }]
                message = get_result(
                    snips.get("entity").get("date_start"),
                    snips.get("entity").get("date_stop"), snips.get("entity"))
                type_reply = "quick_replies"
            else:
                type_reply = "text"
                message = "Mời bạn nhập lại câu hỏi"

        self.__handle_response(message, data, type_reply, user_id, source)
Esempio n. 21
0
def main():
    log_info("Finding document clusters for %s mode..." % args.mode)
    topic_clusters = find_topic_clusters(
        args.schema, (args.aquaint, args.aquaint2, args.gigaword), args.mode)
    log_info("Found %d document clusters." % len(topic_clusters))

    log_info("Summarizing...")

    if not os.path.exists(args.output_dir):
        os.makedirs(args.output_dir)

    if args.load:
        read_file = open(args.load, "rb")
        data = pickle.load(read_file)
        read_file.close()
    else:
        data = {}

    if args.maxent:
        init_maxent_model(args.maxent)

    #put clusters in specific order for consistent behavior across environments
    topic_clusters = sorted(topic_clusters.items(), key=operator.itemgetter(0))

    for index, (topic, docs) in enumerate(topic_clusters):
        log_info("Processing cluster %s..." % topic)

        if args.load:
            sentences, feature_vectors = data[index]
        else:
            if args.aquaint in docs[0][0]:
                sentences, feature_vectors = get_features(docs, 1)
            elif args.aquaint2 in docs[0][0]:
                sentences, feature_vectors = get_features(docs, 2)
            else:
                sentences, feature_vectors = get_features(docs, 3)
            data[index] = (sentences, feature_vectors)

        log_info("textrank starting...")
        sents = [r[1] for r in textrank(feature_vectors, sentences)]

        #compress
        log_info("compress sentences ...")
        sents = compress_sents(sents)

        #truncate
        sents = truncate(sents)

        #order
        log_info("get_ordered_sentences starting...")
        if args.entity_grid == 1:
            sents = get_ordered_sentences(sents)
        else:
            sents = ts_order(sents)
        print("ranked_sentences\n", sents)

        #simplify person names
        log_info("simplify names ...")
        sents = simplify_names(sents)

        log_info("realize starting...")
        summary = realize2(sents)
        print("summary: \n", summary)

        summary_file = "{0}/{1}-A.M.100.{2}.0".format(args.output_dir,
                                                      topic[:-1], topic[-1])

        log_info("creating summary file {0}".format(summary_file))

        f = open(summary_file, "w+")
        f.write(summary)
        f.close()

    if args.store:
        write_file = open(args.store, "wb+")
        pickle.dump(data, write_file)
        write_file.close()
Esempio n. 22
0
 def close_connection(self):
     """This method closes the connection to RabbitMQ."""
     log_info('Closing connection')
     self._connection.close()
Esempio n. 23
0
def testload(filename="../remoteDriverConf.yaml"):
    """docstring for testload"""
    global conf
    conf = load_file(filename)
    logger.log_info(json.dumps(conf, indent=4, sort_keys=True))
    logger.log_info(str(conf["remoteDriverURL"]))
    logger.log_info(str(conf["PC"][0]["browerType"]))
    logger.log_info(str(conf["PC"][0]["version"]))
    logger.log_info(str(conf["PC"][1]["browerType"]))
    logger.log_info(str(conf["PC"][1]["version"]))
    logger.log_info('-----end------')