0
This commit is contained in:
51
api.py
51
api.py
@@ -1,6 +1,7 @@
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from datetime import datetime
|
||||
import logging
|
||||
|
||||
from colorama import Fore
|
||||
import requests
|
||||
from tqdm import tqdm
|
||||
|
||||
@@ -34,20 +35,23 @@ class ClockifyAPI(ClockifyConfig):
|
||||
result = []
|
||||
for activity in tqdm(activities, desc="Парсинг задач"):
|
||||
for tag_id in activity["tagIds"]:
|
||||
start = datetime.strptime(activity["timeInterval"]["start"], config.DATETIME_FORMAT) + timedelta(hours=3)
|
||||
end = datetime.strptime(activity["timeInterval"]["end"] or str(datetime.now(timezone.utc).strftime(config.DATETIME_FORMAT)), config.DATETIME_FORMAT) + timedelta(hours=3)
|
||||
time_worked_out_total += time_to_hour(end - start)
|
||||
start = datetime.strptime(activity["timeInterval"]["start"], config.DATETIME_FORMAT)
|
||||
end = datetime.strptime(activity["timeInterval"]["end"], config.DATETIME_FORMAT) if activity["timeInterval"]["end"] else None
|
||||
time_worked_out_total += time_to_hour((end if end else datetime.now()) - start)
|
||||
tag = [tag for tag in self.get_tags(workspace_id=WORKSPACE_ID) if tag.id == tag_id][0]
|
||||
task_id, task_title = tag.title.split(" - ")
|
||||
result.append(
|
||||
Activity(
|
||||
id=activity["id"],
|
||||
tag_id=tag_id,
|
||||
task_id=task_id,
|
||||
task_title=task_title,
|
||||
project_id=activity["projectId"],
|
||||
description=activity["description"],
|
||||
time_spent=time_to_hour(end - start),
|
||||
time_spent=time_to_hour((end if end else datetime.now()) - start),
|
||||
date_start=start,
|
||||
date_end=end
|
||||
date_end=end,
|
||||
is_tracked=activity["billable"]
|
||||
)
|
||||
)
|
||||
logging.info(f"Сохранено задач: {len(result)}")
|
||||
@@ -70,16 +74,27 @@ class ClockifyAPI(ClockifyConfig):
|
||||
def mark_tracked(self, activities: list[Activity]):
|
||||
for activity in activities:
|
||||
try:
|
||||
resp = requests.put(self.base_url + self.update_time_entry.format(
|
||||
workspace_id=WORKSPACE_ID,
|
||||
id=activity.id
|
||||
), data={
|
||||
"description": "*" + activity.description
|
||||
})
|
||||
resp = requests.put(
|
||||
self.base_url + self.update_time_entry.format(
|
||||
workspace_id=WORKSPACE_ID,
|
||||
id=activity.id
|
||||
), json={
|
||||
"billable": True,
|
||||
"start": activity.date_start.strftime(config.DATETIME_FORMAT),
|
||||
"end": activity.date_end.strftime(config.DATETIME_FORMAT) if activity.date_end else None,
|
||||
"description": activity.description,
|
||||
"tagIds": [activity.tag_id],
|
||||
"projectId": activity.project_id
|
||||
}, headers={
|
||||
"X-API-KEY": self.token
|
||||
}
|
||||
)
|
||||
if resp.ok:
|
||||
logging.debug(activity, "помечена как затреканная")
|
||||
logging.debug(resp.json())
|
||||
logging.info(f"{activity} {Fore.GREEN} помечена как затреканная{Fore.RESET}")
|
||||
else:
|
||||
logging.error(activity, "не помечена как затреканная")
|
||||
logging.debug(resp.json())
|
||||
logging.error(f"{activity} {Fore.RED} не помечена как затреканная{Fore.RESET}")
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
return
|
||||
@@ -111,13 +126,13 @@ class RedmineAPI(RedmineConfig):
|
||||
resp = requests.post(
|
||||
self.base_url + self.time_entries_url,
|
||||
headers={"X-Redmine-Api-Key": self.token},
|
||||
data={
|
||||
json={
|
||||
"time_entry": {
|
||||
"issue_id": activity.task_id,
|
||||
"hours": activity.time_spent,
|
||||
"activity_id": ActivityType.DEVELOPMENT,
|
||||
"comments": activity.description,
|
||||
"spent_on": activity.date.strftime(self.datetime_format)
|
||||
"spent_on": activity.date_start.strftime(self.datetime_format)
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -126,8 +141,8 @@ class RedmineAPI(RedmineConfig):
|
||||
tracked.append(activity)
|
||||
logging.debug(activity)
|
||||
else:
|
||||
logging.error(activity, "Не была затрекана")
|
||||
logging.error(resp.json(), resp)
|
||||
logging.error(f"{activity} Не была затрекана")
|
||||
logging.error(f"{resp.json()['errors']} {resp.status_code}")
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user