initial
This commit is contained in:
49
api.py
Normal file
49
api.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from datetime import datetime, timezone
|
||||
|
||||
import requests
|
||||
from tqdm import tqdm
|
||||
|
||||
from config import ClockifyConfig, config
|
||||
from exceptions import InvalidToken
|
||||
from utils import time_to_hour
|
||||
|
||||
|
||||
class ClockifyAPI(ClockifyConfig):
|
||||
def __init__(self, token):
|
||||
self._check_token(token)
|
||||
self.token = token
|
||||
|
||||
def get_activities(self, **kwargs):
|
||||
"""
|
||||
start: datetime - Начальная дата (обязательно)
|
||||
end: datetime - Конечная дата (обязательно)
|
||||
page: int - Номер страницы
|
||||
page-size: int - Размер страницы (по умолчанию 50)
|
||||
"""
|
||||
activities = requests.get(self.base_url + self.time_entries.format(**kwargs), params=kwargs, headers={"X-API-KEY": self.token}).json()
|
||||
time_worked_out_total = 0
|
||||
for activity in tqdm(activities, desc="Парсинг задач"):
|
||||
for tag in activity["tagIds"]:
|
||||
start = datetime.strptime(activity["timeInterval"]["start"], config.DATETIME_FORMAT)
|
||||
end = datetime.strptime(activity["timeInterval"]["end"] or str(datetime.now(timezone.utc).strftime(config.DATETIME_FORMAT)), config.DATETIME_FORMAT)
|
||||
time_worked_out_total += time_to_hour(end - start)
|
||||
|
||||
print(f"Получено задач: {len(activities)}")
|
||||
print(f"Отработано часов: {time_worked_out_total}")
|
||||
return activities
|
||||
|
||||
|
||||
def get_tags(self, **kwargs):
|
||||
"""
|
||||
workspace_id: str - required
|
||||
name: str - optional
|
||||
archived: bool - optional
|
||||
"""
|
||||
tags = requests.get(self.base_url + self.tags.format(**kwargs), params=kwargs, headers={"X-API-KEY": self.token}).json()
|
||||
return {tag["id"]: tag["name"] for tag in tags}
|
||||
|
||||
def _check_token(self, token):
|
||||
resp = requests.get(self.base_url + self.user_url, headers={"X-API-KEY": token})
|
||||
print(resp)
|
||||
if not resp.ok:
|
||||
raise InvalidToken
|
||||
Reference in New Issue
Block a user