2014-08-06 11:43:40 +02:00
|
|
|
#!/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-06 11:43:40 +02:00
|
|
|
|
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.
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
..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-06 11:43:40 +02:00
|
|
|
from pubsublogger import publisher
|
|
|
|
from packages import lib_words
|
2014-08-20 16:00:56 +02:00
|
|
|
import os
|
2014-09-02 18:20:28 +02:00
|
|
|
import datetime
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-29 19:37:56 +02:00
|
|
|
from Helper import Process
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-20 15:14:57 +02:00
|
|
|
if __name__ == "__main__":
|
2014-08-22 17:35:40 +02:00
|
|
|
publisher.port = 6380
|
2014-08-20 15:14:57 +02:00
|
|
|
publisher.channel = "Script"
|
2014-08-14 17:55:18 +02:00
|
|
|
|
2014-08-29 19:37:56 +02:00
|
|
|
config_section = 'Curve'
|
|
|
|
p = Process(config_section)
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-20 15:14:57 +02:00
|
|
|
# REDIS #
|
2014-08-06 11:43:40 +02:00
|
|
|
r_serv1 = redis.StrictRedis(
|
2016-08-09 11:59:36 +02:00
|
|
|
host=p.config.get("Redis_Level_DB_Curve", "host"),
|
|
|
|
port=p.config.get("Redis_Level_DB_Curve", "port"),
|
|
|
|
db=p.config.get("Redis_Level_DB_Curve", "db"))
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
# FUNCTIONS #
|
2014-08-29 19:37:56 +02:00
|
|
|
publisher.info("Script Curve started")
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-11 11:33:18 +02:00
|
|
|
# FILE CURVE SECTION #
|
2014-08-22 17:35:40 +02:00
|
|
|
csv_path = os.path.join(os.environ['AIL_HOME'],
|
2014-08-29 19:37:56 +02:00
|
|
|
p.config.get("Directories", "wordtrending_csv"))
|
2014-08-22 17:35:40 +02:00
|
|
|
wordfile_path = os.path.join(os.environ['AIL_HOME'],
|
2014-08-29 19:37:56 +02:00
|
|
|
p.config.get("Directories", "wordsfile"))
|
2014-08-11 11:33:18 +02:00
|
|
|
|
2014-08-29 19:37:56 +02:00
|
|
|
message = p.get_from_set()
|
2014-08-06 11:43:40 +02:00
|
|
|
prec_filename = None
|
2014-09-05 10:42:01 +02:00
|
|
|
generate_new_graph = False
|
2014-08-06 11:43:40 +02:00
|
|
|
while True:
|
2014-08-14 17:55:18 +02:00
|
|
|
if message is not None:
|
2014-09-02 18:20:28 +02:00
|
|
|
generate_new_graph = True
|
|
|
|
|
2014-08-29 19:37:56 +02:00
|
|
|
filename, word, score = message.split()
|
2014-09-02 18:20:28 +02:00
|
|
|
temp = filename.split('/')
|
|
|
|
date = temp[-4] + temp[-3] + temp[-2]
|
|
|
|
|
|
|
|
low_word = word.lower()
|
|
|
|
prev_score = r_serv1.hget(low_word, date)
|
2014-08-14 17:55:18 +02:00
|
|
|
if prev_score is not None:
|
2014-09-02 18:20:28 +02:00
|
|
|
r_serv1.hset(low_word, date, int(prev_score) + int(score))
|
2014-08-06 11:43:40 +02:00
|
|
|
else:
|
2014-09-02 18:20:28 +02:00
|
|
|
r_serv1.hset(low_word, date, score)
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
else:
|
2014-09-02 18:20:28 +02:00
|
|
|
if generate_new_graph:
|
|
|
|
generate_new_graph = False
|
|
|
|
print 'Building graph'
|
|
|
|
today = datetime.date.today()
|
|
|
|
year = today.year
|
|
|
|
month = today.month
|
|
|
|
lib_words.create_curve_with_word_file(r_serv1, csv_path,
|
|
|
|
wordfile_path, year,
|
|
|
|
month)
|
|
|
|
|
2014-08-06 11:43:40 +02:00
|
|
|
publisher.debug("Script Curve is Idling")
|
2014-08-29 19:37:56 +02:00
|
|
|
print "sleeping"
|
2014-09-02 18:20:28 +02:00
|
|
|
time.sleep(10)
|
2014-08-29 19:37:56 +02:00
|
|
|
message = p.get_from_set()
|