2021-06-02 16:53:17 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
"""
|
|
|
|
The Tracker_Regex trackers module
|
|
|
|
===================
|
|
|
|
|
|
|
|
This Module is used for regex tracking.
|
2021-09-30 14:20:08 +02:00
|
|
|
It processes every item coming from the global module and test the regex
|
2021-06-02 16:53:17 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import time
|
2021-09-28 22:53:01 +02:00
|
|
|
import requests
|
2021-06-02 16:53:17 +02:00
|
|
|
|
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from modules.abstract_module import AbstractModule
|
|
|
|
from packages.Item import Item
|
|
|
|
from packages import Term
|
|
|
|
from lib import Tracker
|
|
|
|
from lib import regex_helper
|
|
|
|
|
|
|
|
import NotificationHelper
|
|
|
|
|
|
|
|
class Tracker_Regex(AbstractModule):
|
|
|
|
|
2021-06-17 14:48:26 +02:00
|
|
|
mail_body_template = "AIL Framework,\nNew occurrence for tracked regex: {}\nitem id: {}\nurl: {}{}"
|
2021-06-02 16:53:17 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
Tracker_Regex module for AIL framework
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
super(Tracker_Regex, self).__init__()
|
|
|
|
|
|
|
|
self.pending_seconds = 5
|
|
|
|
|
|
|
|
self.max_execution_time = self.process.config.getint(self.module_name, "max_execution_time")
|
|
|
|
|
|
|
|
self.full_item_url = self.process.config.get("Notifications", "ail_domain") + "/object/item?id="
|
|
|
|
|
|
|
|
self.redis_cache_key = regex_helper.generate_redis_cache_key(self.module_name)
|
|
|
|
|
2021-06-17 14:48:26 +02:00
|
|
|
# refresh Tracked Regex
|
2021-06-02 16:53:17 +02:00
|
|
|
self.dict_regex_tracked = Term.get_regex_tracked_words_dict()
|
|
|
|
self.last_refresh = time.time()
|
|
|
|
|
|
|
|
self.redis_logger.info(f"Module: {self.module_name} Launched")
|
|
|
|
|
|
|
|
def compute(self, item_id):
|
|
|
|
# refresh Tracked regex
|
2021-06-17 14:48:26 +02:00
|
|
|
if self.last_refresh < Tracker.get_tracker_last_updated_by_type('regex'):
|
2021-06-02 16:53:17 +02:00
|
|
|
self.dict_regex_tracked = Term.get_regex_tracked_words_dict()
|
|
|
|
self.last_refresh = time.time()
|
2021-06-17 14:48:26 +02:00
|
|
|
self.redis_logger.debug('Tracked regex refreshed')
|
|
|
|
print('Tracked regex refreshed')
|
2021-06-02 16:53:17 +02:00
|
|
|
|
|
|
|
item = Item(item_id)
|
|
|
|
item_id = item.get_id()
|
|
|
|
item_content = item.get_content()
|
|
|
|
|
|
|
|
for regex in self.dict_regex_tracked:
|
|
|
|
matched = regex_helper.regex_search(self.module_name, self.redis_cache_key, self.dict_regex_tracked[regex], item_id, item_content, max_time=self.max_execution_time)
|
|
|
|
if matched:
|
2021-06-17 14:48:26 +02:00
|
|
|
self.new_tracker_found(regex, 'regex', item)
|
2021-06-02 16:53:17 +02:00
|
|
|
|
2021-06-17 14:48:26 +02:00
|
|
|
def new_tracker_found(self, tracker, tracker_type, item):
|
|
|
|
uuid_list = Tracker.get_tracker_uuid_list(tracker, tracker_type)
|
|
|
|
|
|
|
|
item_id = item.get_id()
|
2022-02-16 10:19:53 +01:00
|
|
|
item_date = item.get_date()
|
|
|
|
item_source = item.get_source()
|
2021-06-17 14:48:26 +02:00
|
|
|
print(f'new tracked regex found: {tracker} in {item_id}')
|
2021-06-02 16:53:17 +02:00
|
|
|
|
|
|
|
for tracker_uuid in uuid_list:
|
2021-06-17 14:48:26 +02:00
|
|
|
# Source Filtering
|
|
|
|
tracker_sources = Tracker.get_tracker_uuid_sources(tracker_uuid)
|
|
|
|
if tracker_sources and item_source not in tracker_sources:
|
|
|
|
continue
|
|
|
|
|
2021-07-14 13:58:00 +02:00
|
|
|
Tracker.add_tracked_item(tracker_uuid, item_id)
|
2021-06-17 14:48:26 +02:00
|
|
|
|
|
|
|
tags_to_add = Tracker.get_tracker_tags(tracker_uuid)
|
2021-06-02 16:53:17 +02:00
|
|
|
for tag in tags_to_add:
|
2021-06-17 14:48:26 +02:00
|
|
|
msg = f'{tag};{item_id}'
|
2021-06-02 16:53:17 +02:00
|
|
|
self.send_message_to_queue(msg, 'Tags')
|
|
|
|
|
2021-06-17 14:48:26 +02:00
|
|
|
mail_to_notify = Tracker.get_tracker_mails(tracker_uuid)
|
2021-06-02 16:53:17 +02:00
|
|
|
if mail_to_notify:
|
|
|
|
mail_subject = Tracker.get_email_subject(tracker_uuid)
|
2021-06-17 14:48:26 +02:00
|
|
|
mail_body = Tracker_Regex.mail_body_template.format(tracker, item_id, self.full_item_url, item_id)
|
2021-06-02 16:53:17 +02:00
|
|
|
for mail in mail_to_notify:
|
|
|
|
NotificationHelper.sendEmailNotification(mail, mail_subject, mail_body)
|
2021-09-30 14:20:08 +02:00
|
|
|
|
|
|
|
# Webhook
|
2021-09-28 22:53:01 +02:00
|
|
|
webhook_to_post = Term.get_term_webhook(tracker_uuid)
|
|
|
|
if webhook_to_post:
|
2021-09-30 14:20:08 +02:00
|
|
|
json_request = {"trackerId": tracker_uuid,
|
|
|
|
"itemId": item_id,
|
|
|
|
"itemURL": self.full_item_url + item_id,
|
|
|
|
"tracker": tracker,
|
|
|
|
"itemSource": item_source,
|
|
|
|
"itemDate": item_date,
|
|
|
|
"tags": tags_to_add,
|
|
|
|
"emailNotification": f'{mail_to_notify}',
|
|
|
|
"trackerType": tracker_type
|
|
|
|
}
|
2021-10-04 12:55:40 +02:00
|
|
|
try:
|
|
|
|
response = requests.post(webhook_to_post, json=json_request)
|
|
|
|
if response.status_code >= 400:
|
|
|
|
self.redis_logger.error(f"Webhook request failed for {webhook_to_post}\nReason: {response.reason}")
|
|
|
|
except:
|
|
|
|
self.redis_logger.error(f"Webhook request failed for {webhook_to_post}\nReason: Something went wrong")
|
2021-06-02 16:53:17 +02:00
|
|
|
|
2021-09-30 14:20:08 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-06-02 16:53:17 +02:00
|
|
|
module = Tracker_Regex()
|
2021-07-20 14:55:45 +02:00
|
|
|
module.run()
|