Add initial support for Webhook in Term Trackers

pull/586/head
TonyJabbour 2021-09-28 21:11:20 +02:00
parent 661bcf37f7
commit 30d66ef3a8
3 changed files with 19 additions and 5 deletions

View File

@ -374,6 +374,9 @@ def get_term_tags(term_uuid):
def get_term_mails(term_uuid): def get_term_mails(term_uuid):
return list(r_serv_term.smembers('tracker:mail:{}'.format(term_uuid))) return list(r_serv_term.smembers('tracker:mail:{}'.format(term_uuid)))
def get_term_webhook(term_uuid):
return list(r_serv_term.hget('tracker:webhook:{}'.format(term_uuid)))
def add_tracked_item(term_uuid, item_id, item_date): def add_tracked_item(term_uuid, item_id, item_date):
# track item # track item
r_serv_term.sadd('tracker:item:{}:{}'.format(term_uuid, item_date), item_id) r_serv_term.sadd('tracker:item:{}:{}'.format(term_uuid, item_date), item_id)

View File

@ -13,6 +13,8 @@ import os
import sys import sys
import time import time
import signal import signal
import requests
sys.path.append(os.environ['AIL_BIN']) sys.path.append(os.environ['AIL_BIN'])
################################## ##################################
@ -24,21 +26,24 @@ from packages.Item import Item
from packages import Term from packages import Term
from lib import Tracker from lib import Tracker
class TimeoutException(Exception): class TimeoutException(Exception):
pass pass
def timeout_handler(signum, frame): def timeout_handler(signum, frame):
raise TimeoutException raise TimeoutException
signal.signal(signal.SIGALRM, timeout_handler) signal.signal(signal.SIGALRM, timeout_handler)
class Tracker_Term(AbstractModule): class Tracker_Term(AbstractModule):
mail_body_template = "AIL Framework,\nNew occurrence for tracked term: {}\nitem id: {}\nurl: {}{}" mail_body_template = "AIL Framework,\nNew occurrence for tracked term: {}\nitem id: {}\nurl: {}{}"
""" """
Tracker_Term module for AIL framework Tracker_Term module for AIL framework
""" """
def __init__(self): def __init__(self):
super(Tracker_Term, self).__init__() super(Tracker_Term, self).__init__()
@ -56,7 +61,6 @@ class Tracker_Term(AbstractModule):
self.redis_logger.info(f"Module: {self.module_name} Launched") self.redis_logger.info(f"Module: {self.module_name} Launched")
def compute(self, item_id): def compute(self, item_id):
# refresh Tracked term # refresh Tracked term
if self.last_refresh_word < Term.get_tracked_term_last_updated_by_type('word'): if self.last_refresh_word < Term.get_tracked_term_last_updated_by_type('word'):
@ -135,8 +139,14 @@ class Tracker_Term(AbstractModule):
print(f'S print(item_content)end Mail {mail_subject}') print(f'S print(item_content)end Mail {mail_subject}')
NotificationHelper.sendEmailNotification(mail, mail_subject, mail_body) NotificationHelper.sendEmailNotification(mail, mail_subject, mail_body)
webhook_to_post = Term.get_term_webhook(term_uuid)
if webhook_to_post:
request_body = dict({"itemId": item_id, "url": self.full_item_url, "type": "Term", "term": term})
r = requests.post(webhook_to_post, data=request_body)
if (r.status_code >= 400):
raise Exception(f"Webhook request failed for {webhook_to_post}\nReason: {r.reason}")
if __name__ == '__main__': if __name__ == '__main__':
module = Tracker_Term() module = Tracker_Term()
module.run() module.run()

View File

@ -77,6 +77,7 @@ PySocks>=1.7.1
pycountry>=20.7.3 pycountry>=20.7.3
https://github.com/saffsd/langid.py/archive/master.zip https://github.com/saffsd/langid.py/archive/master.zip
requests
##### Old packages ##### Old packages