Skip to content

Commit

Permalink
Add tag fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
bswck committed Jan 9, 2024
1 parent 42e6736 commit 623848f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
34 changes: 20 additions & 14 deletions redesc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def get_playlist_items(
) -> list[dict[str, Any]]:
current_page = None
items = []
item_map = {}
pages_to_fetch, last_page_limit = divmod(limit, 50)
for page_idx in range(pages_to_fetch + 1):
request = self.client.playlistItems().list(
Expand All @@ -56,23 +57,28 @@ def get_playlist_items(
raise LookupError(msg) from exc
raise
page_items = resp.get("items", [])
for item in page_items:
data = (
self.client.videos()
.list(
part="snippet",
id=item["snippet"]["resourceId"]["videoId"],
)
.execute()
)
try:
data_item = data["items"][0]["snippet"]
except IndexError:
data_item = {"tags": []}
item["snippet"].update({"tags": data_item.setdefault("tags", [])})
if page_idx == pages_to_fetch:
page_items = page_items[:last_page_limit]
items.extend(page_items)
for item in items:
item_map[item["id"]] = item
items_with_tags = (
self.client.videos()
.list(
part="snippet",
id=",".join(item["id"] for item in page_items),
)
.execute()
)
for item_with_tags in items_with_tags["items"]:
try:
item_snippet = item_with_tags["snippet"]
except IndexError:
item_snippet = {"tags": []}
item_map[item_snippet["id"]]["snippet"].update(
{"tags": item_snippet.setdefault("tags", [])},
)

if len(items) >= limit:
break
current_page = resp.get("nextPageToken")
Expand Down
2 changes: 1 addition & 1 deletion redesc/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ async def callback( # noqa: C901
new_title=new_title,
old_description=old_description,
new_description=new_description,
tags=snippet["tags"],
tags=snippet.get("tags") or [],
),
)

Expand Down

0 comments on commit 623848f

Please sign in to comment.