mirror of
https://github.com/ail-project/ail-framework.git
synced 2024-11-30 01:37:17 +00:00
Add initial support for Webhook in Term Trackers
This commit is contained in:
parent
661bcf37f7
commit
30d66ef3a8
3 changed files with 19 additions and 5 deletions
|
@ -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)
|
||||||
|
|
|
@ -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'):
|
||||||
|
@ -88,7 +92,7 @@ class Tracker_Term(AbstractModule):
|
||||||
|
|
||||||
if dict_words_freq:
|
if dict_words_freq:
|
||||||
# create token statistics
|
# create token statistics
|
||||||
#for word in dict_words_freq:
|
# for word in dict_words_freq:
|
||||||
# Term.create_token_statistics(item_date, word, dict_words_freq[word])
|
# Term.create_token_statistics(item_date, word, dict_words_freq[word])
|
||||||
item_source = item.get_source()
|
item_source = item.get_source()
|
||||||
|
|
||||||
|
@ -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()
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue