mirror of
https://github.com/norohind/jubilant-system.git
synced 2025-04-20 16:07:36 +03:00
Insert hook done
This commit is contained in:
parent
69d81a05bb
commit
98c19910f6
4
doc.txt
4
doc.txt
@ -74,8 +74,10 @@ implementation notes:
|
||||
|
||||
hooks (don't mismatch with DB hooks)
|
||||
1. On properly_delete_squadron
|
||||
NB: calls before make delete record
|
||||
|
||||
2. On insertion new data to squads_states (don't forget handle news)
|
||||
3.
|
||||
calls after insertion
|
||||
|
||||
legacy:
|
||||
request bearer token from capi.demb.design
|
||||
|
36
main.py
36
main.py
@ -52,10 +52,6 @@ Two modes:
|
||||
"""
|
||||
|
||||
|
||||
def discover_triggers(squad_info: dict):
|
||||
print(squad_info.get('name'), squad_info.get('tag'), squad_info.get('ownerName'))
|
||||
|
||||
|
||||
def discover():
|
||||
"""Discover new squads
|
||||
|
||||
@ -65,26 +61,37 @@ def discover():
|
||||
id_to_try = utils.get_last_known_id(db)
|
||||
tries: int = 0
|
||||
failed: list = list()
|
||||
tries_limit: int = 5000
|
||||
TRIES_LIMIT_RETROSPECTIVELY: int = 5000
|
||||
TRIES_LIMIT_ON_THE_TIME: int = 5
|
||||
|
||||
def smart_tries_limit(squad_id: int) -> int: # something smarter but still have to be better
|
||||
|
||||
if id_to_try < 65000:
|
||||
return TRIES_LIMIT_RETROSPECTIVELY
|
||||
|
||||
else:
|
||||
return TRIES_LIMIT_ON_THE_TIME
|
||||
|
||||
"""
|
||||
tries_limit, probably, should be something more smart because on retrospectively scan we can
|
||||
have large spaces of dead squadrons but when we are discovering on real time, large value of tries_limit
|
||||
will just waste our time and, probable, confuses FDEV"""
|
||||
will just waste our time and, probable, confuses FDEV
|
||||
"""
|
||||
|
||||
while True:
|
||||
id_to_try = id_to_try + 1
|
||||
logger.debug(f'Starting discover loop iteration, tries: {tries} of {tries_limit}, id to try {id_to_try}, '
|
||||
f'failed list: {failed}')
|
||||
# logger.debug(f'Starting discover loop iteration, tries: {tries} of {tries_limit}, id to try {id_to_try}, '
|
||||
# f'failed list: {failed}')
|
||||
|
||||
if tries == tries_limit:
|
||||
if tries == smart_tries_limit(id_to_try):
|
||||
break
|
||||
|
||||
squad_info = utils.update_squad_info(id_to_try, db, suppress_absence=True)
|
||||
|
||||
if isinstance(squad_info, dict): # success
|
||||
logger.debug(f'Success discover for {id_to_try} ID')
|
||||
discover_triggers(squad_info)
|
||||
tries = 0 # reset tries counter
|
||||
|
||||
for failed_squad in failed: # since we found an exists squad, then all previous failed wasn't exists
|
||||
utils.properly_delete_squadron(failed_squad, db)
|
||||
|
||||
@ -95,10 +102,7 @@ def discover():
|
||||
failed.append(id_to_try)
|
||||
tries = tries + 1
|
||||
|
||||
time.sleep(2)
|
||||
|
||||
|
||||
discover()
|
||||
time.sleep(3)
|
||||
|
||||
|
||||
def update(squad_id: int = None, amount_to_update: int = 1):
|
||||
@ -121,3 +125,7 @@ def update(squad_id: int = None, amount_to_update: int = 1):
|
||||
id_to_update: int = single_squad_to_update[0]
|
||||
logger.debug(f'Updating {id_to_update}')
|
||||
utils.update_squad_info(id_to_update, db)
|
||||
time.sleep(3)
|
||||
|
||||
|
||||
discover()
|
||||
|
@ -70,4 +70,4 @@ limit 1;"""
|
||||
select_squads_to_update: str = """select squad_id
|
||||
from squads_states
|
||||
order by inserted_timestamp asc
|
||||
limit = ?;"""
|
||||
limit = ?;"""
|
||||
|
@ -44,7 +44,7 @@ from squads_states
|
||||
where inserted_timestamp in (
|
||||
select max(inserted_timestamp)
|
||||
from squads_states
|
||||
group by squad_id)
|
||||
group by squad_id) and tag is not null
|
||||
group by squad_id;
|
||||
|
||||
create table if not exists news (
|
||||
|
25
utils.py
25
utils.py
@ -3,6 +3,7 @@ from typing import Union
|
||||
import requests
|
||||
import json
|
||||
from EDMCLogging import get_main_logger
|
||||
import hooks
|
||||
import sql_requests
|
||||
|
||||
logger = get_main_logger()
|
||||
@ -21,7 +22,13 @@ class FAPIUnknownStatusCode(Exception):
|
||||
|
||||
|
||||
def authed_request(url: str, method: str = 'get', **kwargs) -> requests.Response:
|
||||
"""Makes request to any url with valid bearer token"""
|
||||
"""Makes request to any url with valid bearer token
|
||||
|
||||
:param url: url to make request
|
||||
:param method: method to make request, case insensitive, get by default
|
||||
:param kwargs: will be passed to requests.request
|
||||
:return: requests.Response object
|
||||
"""
|
||||
bearer: str = _get_bearer()
|
||||
|
||||
logger.debug(f'Requesting {method.upper()} {url!r}, kwargs: {kwargs}')
|
||||
@ -43,7 +50,10 @@ def authed_request(url: str, method: str = 'get', **kwargs) -> requests.Response
|
||||
|
||||
|
||||
def _get_bearer() -> str:
|
||||
"""Gets bearer token from capi.demb.design (companion-api project)"""
|
||||
"""Gets bearer token from capi.demb.design (companion-api project)
|
||||
|
||||
:return: bearer token as str
|
||||
"""
|
||||
bearer_request: requests.Response = requests.get(
|
||||
url='https://capi.demb.design/users/2yGDATq_zzfudaQ_8XnFVKtE80gco1q1-2AkSL9gxoI=')
|
||||
|
||||
@ -68,7 +78,7 @@ def notify_discord(message: str) -> None:
|
||||
logger.debug('Sending discord message')
|
||||
|
||||
# hookURL: str = 'https://discord.com/api/webhooks/896514472280211477/LIKgbgNIr9Nvuc-1-FfylAIY1YV-a7RMjBlyBsVDellMbnokXLYKyBztY1P9Q0mabI6o' # noqa: E501 # FBSC
|
||||
hookURL: str = 'https://discord.com/api/webhooks/901531763740913775/McqeW4eattrCktrUo2XWhwaJO3POSjWbTb8BHeFAKrsTHOwc-r9rQ2zkFxtGZ1eQ_Ifd' # noqa: E501 # dev
|
||||
hookURL: str = 'https://discord.com/api/webhooks/902216904507260958/EIUwZ05r0_U2oa_xz8aVVEJyTC6DVk4ENxGYSde8ZNU7aMWBsc3Bo_gBis1_yUxJc3CC' # noqa: E501 # dev FBSC
|
||||
content: bytes = f'content={requests.utils.quote(message)}'.encode('utf-8')
|
||||
|
||||
if len(content) >= 2000: # discord limitation
|
||||
@ -112,6 +122,7 @@ def _update_squad_news(squad_id: int, db_conn: sqlite3.Connection) -> Union[bool
|
||||
insert all news even if it already exist in DB
|
||||
return motd
|
||||
"""
|
||||
|
||||
news_request: requests.Response = authed_request(BASE_URL + NEWS_ENDPOINT, params={'squadronId': squad_id})
|
||||
if news_request.status_code != 200: # must not happen
|
||||
logger.warning(f'Got not 200 status code on requesting news, content: {news_request.content}')
|
||||
@ -119,6 +130,10 @@ def _update_squad_news(squad_id: int, db_conn: sqlite3.Connection) -> Union[bool
|
||||
|
||||
squad_news: dict = news_request.json()['squadron']
|
||||
|
||||
if isinstance(squad_news, list): # check squadron 2517 for example 0_0
|
||||
logger.info(f'squad_news is list for {squad_id}: {squad_news}')
|
||||
return False
|
||||
|
||||
if 'id' not in squad_news.keys(): # squadron doesn't FDEV
|
||||
return False
|
||||
|
||||
@ -246,6 +261,8 @@ def update_squad_info(squad_id: int, db_conn: sqlite3.Connection, suppress_absen
|
||||
motd: str = _update_squad_news(squad_id, db_conn) # yeah, it can return bool but never should does it
|
||||
squad_request_json.update(motd=motd)
|
||||
|
||||
hooks.notify_insert_data(squad_request_json, db_conn) # call hook
|
||||
|
||||
return squad_request_json
|
||||
|
||||
elif squad_request.status_code == 404: # squad doesn't exists FDEV
|
||||
@ -277,6 +294,8 @@ def properly_delete_squadron(squad_id: int, db_conn: sqlite3.Connection) -> None
|
||||
"""
|
||||
logger.debug(f'Properly deleting {squad_id}')
|
||||
|
||||
hooks.notify_properly_delete(squad_id, db_conn)
|
||||
|
||||
with db_conn:
|
||||
db_conn.execute(sql_requests.properly_delete_squad, (squad_id,))
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user