AIL-framework/bin/ZMQ_Sub_Curve.py

92 lines
2.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python2
# -*-coding:UTF-8 -*
"""
The ZMQ_Sub_Curve Module
============================
2014-08-07 14:46:43 +02:00
This module is consuming the Redis-list created by the ZMQ_Sub_Curve_Q Module.
2014-08-20 15:14:57 +02:00
This modules update a .csv file used to draw curves representing selected
words and their occurency per day.
..note:: The channel will have the name of the file created.
..note:: Module ZMQ_Something_Q and ZMQ_Something are closely bound, always put
the same Subscriber name in both of them.
Requirements
------------
*Need running Redis instances. (Redis)
*Categories files of words in /files/ need to be created
*Need the ZMQ_PubSub_Tokenize_Q Module running to be able to work properly.
"""
2014-08-14 17:55:18 +02:00
import redis
import time
2014-08-20 15:14:57 +02:00
from packages import Paste
from pubsublogger import publisher
from packages import lib_words
import os
2014-08-20 15:14:57 +02:00
import Helper
2014-08-20 15:14:57 +02:00
if __name__ == "__main__":
publisher.port = 6380
2014-08-20 15:14:57 +02:00
publisher.channel = "Script"
2014-08-14 17:55:18 +02:00
2014-08-20 15:14:57 +02:00
config_section = 'PubSub_Words'
config_channel = 'channel_0'
subscriber_name = "curve"
2014-08-20 15:14:57 +02:00
h = Helper.Redis_Queues(config_section, config_channel, subscriber_name)
2014-08-20 15:14:57 +02:00
# Subscriber
h.zmq_sub(config_section)
2014-08-20 15:14:57 +02:00
# REDIS #
r_serv1 = redis.StrictRedis(
2014-08-20 15:14:57 +02:00
host=h.config.get("Redis_Level_DB", "host"),
port=h.config.get("Redis_Level_DB", "port"),
db=h.config.get("Redis_Level_DB", "db"))
# FUNCTIONS #
2014-08-20 15:14:57 +02:00
publisher.info("Script Curve subscribed to {}".format(h.sub_channel))
# FILE CURVE SECTION #
csv_path = os.path.join(os.environ['AIL_HOME'],
h.config.get("Directories", "wordtrending_csv"))
wordfile_path = os.path.join(os.environ['AIL_HOME'],
h.config.get("Directories", "wordsfile"))
2014-08-20 15:14:57 +02:00
message = h.redis_rpop()
prec_filename = None
while True:
2014-08-14 17:55:18 +02:00
if message is not None:
channel, filename, word, score = message.split()
2014-08-14 17:55:18 +02:00
if prec_filename is None or filename != prec_filename:
2014-08-20 15:14:57 +02:00
PST = Paste.Paste(filename)
lib_words.create_curve_with_word_file(
r_serv1, csv_path, wordfile_path, int(PST.p_date.year),
int(PST.p_date.month))
prec_filename = filename
prev_score = r_serv1.hget(word.lower(), PST.p_date)
print prev_score
2014-08-14 17:55:18 +02:00
if prev_score is not None:
2014-08-20 15:14:57 +02:00
r_serv1.hset(word.lower(), PST.p_date,
int(prev_score) + int(score))
else:
r_serv1.hset(word.lower(), PST.p_date, score)
else:
2014-08-20 15:14:57 +02:00
if h.redis_queue_shutdown():
print "Shutdown Flag Up: Terminating"
publisher.warning("Shutdown Flag Up: Terminating.")
break
publisher.debug("Script Curve is Idling")
print "sleepin"
time.sleep(1)
2014-08-20 15:14:57 +02:00
message = h.redis_rpop()