diff --git a/api/clockify.py b/api/clockify.py index 44e1e9e..3709456 100644 --- a/api/clockify.py +++ b/api/clockify.py @@ -58,9 +58,10 @@ class ClockifyAPI(ClockifyConfig): tag = [tag for tag in tags if tag.id == tag_id] if not tag: - tag = self.get_tags(True, tag_id=tag_id) - if tag: - db.insert_tags([tag] if isinstance(tag, Tag) else tag) + tag = self.get_tags(True, tag_id=tag_id, **{"page-size": self.tags_count}) + if isinstance(tag, Tag): + db.insert_tags([tag]) + tags.append(tag) from config import red try: task_id = parse_task_id(tag.title) @@ -72,6 +73,7 @@ class ClockifyAPI(ClockifyConfig): db.insert_tasks([task]) else: logger.error(f"{Fore.RED} Тег не найден: {tag_id}{Fore.RESET}") + continue else: tag = tag[0] task_id = parse_task_id(tag.title) @@ -98,27 +100,50 @@ class ClockifyAPI(ClockifyConfig): return result - def get_tags(self, is_updating=False, tag_id="", **kwargs) -> Union[list[Tag], Tag]: + def get_tags(self, is_updating=False, tag_id="", **kwargs) -> Union[list[Tag], Tag, None]: """ Получает с сервера Clockify теги name: str - optional archived: bool - optional page-size: int - optional + page: int - optional """ if is_updating: - tags = requests.get(self.base_url + self.tags_url, params={"sort-column": "name", "sort-order": "descending", **kwargs}, headers={"X-API-KEY": self.token}).json() - if tag_id: + page = 1 + page_size = int(kwargs.get("page-size", self.tags_count)) + result = [] + while True: + params = { + "sort-column": "name", + "sort-order": "descending", + **kwargs, + "page": page + } + tags = requests.get( + self.base_url + self.tags_url, + params=params, + headers={"X-API-KEY": self.token} + ).json() + for tag in tags: - if tag_id == tag["id"]: - return Tag( - id=tag["id"], - title=tag["name"] - ) - logger.debug(f"{Fore.YELLOW}Получено тегов: {len(tags)}.{Fore.RESET}") - return [Tag( - id=tag["id"], - title=tag["name"] - ) for tag in tags] + clockify_tag = Tag( + id=tag["id"], + title=tag["name"] + ) + if tag_id and tag_id == clockify_tag.id: + return clockify_tag + if not tag_id: + result.append(clockify_tag) + + if len(tags) < page_size: + break + page += 1 + + if tag_id: + return None + + logger.debug(f"{Fore.YELLOW}Получено тегов: {len(result)}.{Fore.RESET}") + return result return db.get_tags() def get_vetro_projects(self) -> list[VetroProject]: diff --git a/config/clockify.py b/config/clockify.py index 663f20d..d0a8880 100644 --- a/config/clockify.py +++ b/config/clockify.py @@ -10,7 +10,7 @@ class ClockifyConfig: self.user_url = "/user" self.projects_url = f"{self.workspace_url}/projects" - self.tags_count = 100 + self.tags_count = 20 def update_time_entry(self, activity_id): return f"{self.workspace_url}/time-entries/{activity_id}"