Add CLI interface, news_view, sql reqs fixes

This commit is contained in:
norohind 2021-10-26 00:36:31 +03:00
parent 98c19910f6
commit fe05148598
Signed by: norohind
GPG Key ID: 01C3BECC26FB59E1
3 changed files with 83 additions and 6 deletions

76
main.py
View File

@ -1,6 +1,6 @@
import sqlite3
import time
import sys
import sql_requests
import utils
from EDMCLogging import get_main_logger
@ -11,8 +11,6 @@ db = sqlite3.connect('squads.sqlite')
with open('sql_schema.sql', 'r', encoding='utf-8') as schema_file:
db.executescript(''.join(schema_file.readlines()))
ruTag: id = 32
"""
Two modes:
1. Discover new squads
@ -123,9 +121,77 @@ def update(squad_id: int = None, amount_to_update: int = 1):
for single_squad_to_update in squads_id_to_update: # if db is empty, then loop will not happen
id_to_update: int = single_squad_to_update[0]
logger.debug(f'Updating {id_to_update}')
logger.debug(f'Updating {id_to_update} ID')
utils.update_squad_info(id_to_update, db)
time.sleep(3)
discover()
if __name__ == '__main__':
def help_cli() -> str:
return """Possible arguments:
main.py discover
main.py update
main.py update amount <amount: int>
main.py update id <id: int>"""
logger.debug(f'argv: {sys.argv}')
if len(sys.argv) == 1:
print(help_cli())
exit(1)
elif len(sys.argv) == 2:
if sys.argv[1] == 'discover':
# main.py discover
logger.info(f'Entering discover mode')
discover()
exit(0)
elif sys.argv[1] == 'update':
# main.py update
logger.info(f'Entering common update mode')
update()
exit(0)
else:
print(help_cli())
exit(1)
elif len(sys.argv) == 4:
if sys.argv[1] == 'update':
if sys.argv[2] == 'amount':
# main.py update amount <amount: int>
try:
amount: int = int(sys.argv[3])
logger.info(f'Entering update amount mode, amount: {amount}')
update(amount_to_update=amount)
exit(0)
except ValueError:
print('Amount must be integer')
exit(1)
elif sys.argv[2] == 'id':
# main.py update id <id: int>
try:
id_for_update: int = int(sys.argv[3])
logger.info(f'Entering update specified squad: {id_for_update} ID')
update(squad_id=id_for_update)
exit(0)
except ValueError:
print('ID must be integer')
exit(1)
else:
logger.info(f'Unknown argument {sys.argv[2]}')
else:
print(help_cli())
exit(1)

View File

@ -69,5 +69,6 @@ limit 1;"""
select_squads_to_update: str = """select squad_id
from squads_states
where tag is not null
order by inserted_timestamp asc
limit = ?;"""
limit ?;"""

View File

@ -61,3 +61,13 @@ author text,
cmdr_id int,
user_id int,
inserted_timestamp datetime default current_timestamp);
create view if not exists news_view
as
select *
from news
where inserted_timestamp in (
select max(inserted_timestamp)
from news
group by squad_id)
group by squad_id;