97 lines
3.2 KiB
Python
97 lines
3.2 KiB
Python
import logging
|
||
import os.path
|
||
import sys
|
||
|
||
logging.basicConfig(
|
||
level=logging.INFO,
|
||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||
handlers=[
|
||
logging.FileHandler('app.log', encoding='utf-8'),
|
||
logging.StreamHandler(sys.stdout)
|
||
]
|
||
)
|
||
|
||
|
||
from colorama import Fore # noqa: E402
|
||
|
||
from api import ClockifyAPI, RedmineAPI # noqa: E402
|
||
from config import arg_parser, config, db # noqa: E402
|
||
from utils import format_date, str_to_date, today_start, today_end # noqa: E402
|
||
|
||
|
||
WORKSPACE_ID = config.WORKSPACE_ID
|
||
USER_ID = config.USER_ID
|
||
|
||
|
||
def main():
|
||
args = arg_parser.parse_args()
|
||
|
||
if args.debug:
|
||
logging.getLogger().setLevel(logging.DEBUG)
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
start, end = None, None
|
||
if args.start:
|
||
start = str_to_date(args.start)
|
||
if args.end:
|
||
end = str_to_date(args.end)
|
||
if args.start is None and args.end is None:
|
||
logger.info("Парсим задачи за сегодняшний день")
|
||
start = today_start()
|
||
end = today_end()
|
||
else:
|
||
if start:
|
||
from_ = f"{start.day} {config.MONTHS.get(start.month)} {start.year} г."
|
||
|
||
if end:
|
||
to_ = f"{end.day} {config.MONTHS.get(end.month)} {end.year} г."
|
||
|
||
logger.info(Fore.YELLOW + "Парсим задачи" + (f" с {from_} " if start else " ") + (f"до {to_}" if end else "") + Fore.RESET)
|
||
|
||
clock = ClockifyAPI(
|
||
token=config.CLOCKIFY_TOKEN,
|
||
workspace_id=WORKSPACE_ID,
|
||
user_id=USER_ID
|
||
)
|
||
if not WORKSPACE_ID or not USER_ID:
|
||
if not os.path.exists(config.ENV_TXT):
|
||
logging.info(f"{Fore.YELLOW}Необходимые переменные не были обнаружены. Попытка получения...{Fore.RESET}")
|
||
clock.getenvs()
|
||
else:
|
||
logger.info(f'{Fore.YELLOW}Проверьте файл "{config.ENV_TXT}". Если необходимых переменных в нём нет, то просто удалите его.{Fore.RESET}')
|
||
return
|
||
red = RedmineAPI(config.REDMINE_TOKEN)
|
||
|
||
if args.init_db:
|
||
logger.info(Fore.YELLOW + "Обновление базы данных..." + Fore.RESET)
|
||
try:
|
||
db.insert_tags(clock.get_tags())
|
||
db.insert_vetro_projects(clock.get_vetro_projects())
|
||
db.insert_activity_types(red.get_time_entry_activities())
|
||
except Exception as e:
|
||
logger.error(Fore.RED + f"Ошибка при обновлении базы данных: {e}" + Fore.RESET)
|
||
else:
|
||
logger.info(Fore.GREEN + "База данных теперь актуальна." + Fore.RESET)
|
||
|
||
activities = clock.get_activities(
|
||
**{
|
||
"start": format_date(start),
|
||
"end": format_date(end),
|
||
"page-size": args.page_size,
|
||
},
|
||
)
|
||
db.insert_activities(activities)
|
||
if args.debug:
|
||
logger.debug("Пропускаем этап загрузки трудочасов")
|
||
return
|
||
try:
|
||
tracked_activities = red.track_activities(activities)
|
||
clock.mark_tracked(tracked_activities)
|
||
db.insert_time_entries(tracked_activities)
|
||
except Exception as e:
|
||
logger.error(e)
|
||
|
||
if __name__ == "__main__":
|
||
main()
|