2014-08-06 11:43:40 +02:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
"""
|
|
|
|
The ZMQ_PubSub_Categ Module
|
|
|
|
============================
|
|
|
|
|
2014-08-14 14:11:07 +02:00
|
|
|
This module is consuming the Redis-list created by the ZMQ_PubSub_Tokenize_Q
|
|
|
|
Module.
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
Each words files created under /files/ are representing categories.
|
|
|
|
This modules take these files and compare them to
|
|
|
|
the stream of data given by the ZMQ_PubSub_Tokenize_Q Module.
|
|
|
|
|
|
|
|
When a word from a paste match one or more of these words file, the filename of
|
|
|
|
the paste is published/forwarded to the next modules.
|
|
|
|
|
|
|
|
Each category (each files) are representing a dynamic channel.
|
|
|
|
This mean that if you create 1000 files under /files/ you'll have 1000 channels
|
|
|
|
where every time there is a matching word to a category, the paste containing
|
|
|
|
this word will be pushed to this specific channel.
|
|
|
|
|
|
|
|
..note:: The channel will have the name of the file created.
|
|
|
|
|
|
|
|
Implementing modules can start here, create your own category file,
|
2014-08-14 14:11:07 +02:00
|
|
|
and then create your own module to treat the specific paste matching this
|
|
|
|
category.
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
..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-19 19:07:07 +02:00
|
|
|
import os
|
2014-08-14 14:11:07 +02:00
|
|
|
import argparse
|
|
|
|
import time
|
2014-08-06 11:43:40 +02:00
|
|
|
from pubsublogger import publisher
|
2014-08-14 14:11:07 +02:00
|
|
|
from packages import Paste
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-19 19:07:07 +02:00
|
|
|
import Helper
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-19 19:07:07 +02:00
|
|
|
if __name__ == "__main__":
|
2014-08-22 17:35:40 +02:00
|
|
|
publisher.port = 6380
|
2014-08-19 19:07:07 +02:00
|
|
|
publisher.channel = "Script"
|
|
|
|
|
|
|
|
config_section = 'PubSub_Words'
|
|
|
|
config_channel = 'channel_0'
|
2014-08-20 15:54:21 +02:00
|
|
|
subscriber_name = 'categ'
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-19 19:07:07 +02:00
|
|
|
h = Helper.Redis_Queues(config_section, config_channel, subscriber_name)
|
|
|
|
|
2014-08-20 15:14:57 +02:00
|
|
|
# Publisher
|
|
|
|
pub_config_section = 'PubSub_Categ'
|
|
|
|
h.zmq_pub(pub_config_section, None)
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
# SCRIPT PARSER #
|
|
|
|
parser = argparse.ArgumentParser(
|
2014-08-19 19:07:07 +02:00
|
|
|
description='This script is a part of the Analysis Information \
|
|
|
|
Leak framework.')
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-14 14:11:07 +02:00
|
|
|
parser.add_argument(
|
2014-08-19 19:07:07 +02:00
|
|
|
'-d', type=str, default="../files/",
|
|
|
|
help='Path to the directory containing the category files.',
|
2014-08-14 14:11:07 +02:00
|
|
|
action='store')
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# FUNCTIONS #
|
2014-08-19 19:07:07 +02:00
|
|
|
publisher.info(
|
|
|
|
"Script Categ subscribed to channel {}".format(h.sub_channel))
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-19 19:07:07 +02:00
|
|
|
tmp_dict = {}
|
2014-08-22 17:35:40 +02:00
|
|
|
for filename in os.listdir(args.d):
|
2014-08-19 19:07:07 +02:00
|
|
|
bname = os.path.basename(filename)
|
|
|
|
tmp_dict[bname] = []
|
2014-08-22 17:35:40 +02:00
|
|
|
with open(os.path.join(args.d, filename), 'r') as f:
|
2014-08-19 19:07:07 +02:00
|
|
|
for l in f:
|
|
|
|
tmp_dict[bname].append(l.strip())
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
prec_filename = None
|
|
|
|
|
|
|
|
while True:
|
2014-08-20 15:14:57 +02:00
|
|
|
message = h.redis_rpop()
|
2014-08-14 14:11:07 +02:00
|
|
|
if message is not None:
|
2014-08-06 11:43:40 +02:00
|
|
|
channel, filename, word, score = message.split()
|
|
|
|
|
2014-08-14 14:11:07 +02:00
|
|
|
if prec_filename is None or filename != prec_filename:
|
|
|
|
PST = Paste.Paste(filename)
|
2014-08-19 19:07:07 +02:00
|
|
|
prec_filename = filename
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-19 19:07:07 +02:00
|
|
|
for categ, words_list in tmp_dict.items():
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-19 19:07:07 +02:00
|
|
|
if word.lower() in words_list:
|
2014-08-20 15:14:57 +02:00
|
|
|
h.pub_channel = categ
|
|
|
|
h.zmq_pub_send('{} {} {}'.format(PST.p_path, word, score))
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-14 14:11:07 +02:00
|
|
|
publisher.info(
|
|
|
|
'Categ;{};{};{};Detected {} "{}"'.format(
|
|
|
|
PST.p_source, PST.p_date, PST.p_name, score, word))
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
else:
|
2014-08-19 19:07:07 +02:00
|
|
|
if h.redis_queue_shutdown():
|
2014-08-06 11:43:40 +02:00
|
|
|
print "Shutdown Flag Up: Terminating"
|
|
|
|
publisher.warning("Shutdown Flag Up: Terminating.")
|
|
|
|
break
|
|
|
|
publisher.debug("Script Categ is Idling 10s")
|
|
|
|
time.sleep(10)
|