Skip to content

Commit 0835c80

Browse files
[youtube-data-api] feat: add support for sorts
1 parent 4a21589 commit 0835c80

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

libs/community/google/youtube/youtube-data-api/garf_youtube_data_api/report_fetcher.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from collections.abc import Iterable, MutableSequence
2121
from typing import Any, Final
2222

23+
import pandas as pd
2324
from garf_core import parsers, report, report_fetcher
2425
from typing_extensions import override
2526

@@ -96,4 +97,16 @@ def fetch(
9697
query_specification, args, **{name: element}, **kwargs
9798
)
9899
results.append(res)
99-
return functools.reduce(operator.add, results)
100+
res = functools.reduce(operator.add, results)
101+
if sorts := (
102+
query_editor.YouTubeDataApiQuery(text=query_specification)
103+
.generate()
104+
.sorts
105+
):
106+
sorts = sorts[0]
107+
key, *desc = sorts.split(' ')
108+
asc_order = not desc or 'ASC' in desc[0]
109+
return report.GarfReport.from_pandas(
110+
res.to_pandas().sort_values(by=key, ascending=asc_order)
111+
)
112+
return res

libs/community/google/youtube/youtube-data-api/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ build-backend = "setuptools.build_meta"
55
[project]
66
name = "garf-youtube-data-api"
77
dependencies = [
8-
"garf-core",
8+
"garf-core[pandas]",
99
"garf-io",
1010
"google-api-python-client",
1111
]
1212
authors = [
13+
{name = "Andrei Markin", email = "amarkin@google.com"},
1314
{name = "Google Inc. (gTech gPS CSE team)", email = "no-reply@google.com"},
1415
]
1516
license = {text = "Apache 2.0"}

libs/community/google/youtube/youtube-data-api/tests/unit/test_report_fetcher.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class TestYouTubeDataApiReportFetcher:
2222
def fetcher(self):
2323
return YouTubeDataApiReportFetcher()
2424

25-
def test_fetch(self, mocker, fetcher):
25+
def test_fetch_returns_filtered_data(self, mocker, fetcher):
2626
query = """
2727
SELECT
2828
id,
@@ -61,3 +61,43 @@ def test_fetch(self, mocker, fetcher):
6161
)
6262

6363
assert result == expected_report
64+
65+
def test_fetch_returns_sorted_data(self, mocker, fetcher):
66+
query = """
67+
SELECT
68+
id,
69+
statistics.viewCount AS views,
70+
statistics.likeCount AS likes,
71+
snippet.publishedAt AS published_at,
72+
FROM videos
73+
ORDER BY likes DESC
74+
"""
75+
76+
mocker.patch(
77+
'garf_youtube_data_api.api_clients.YouTubeDataApiClient._list',
78+
return_value={
79+
'items': [
80+
{
81+
'id': 1,
82+
'statistics': {'viewCount': 10, 'likeCount': 1},
83+
'snippet': {'publishedAt': '2024-07-10T22:15:44Z'},
84+
},
85+
{
86+
'id': 2,
87+
'statistics': {'viewCount': 11, 'likeCount': 2},
88+
'snippet': {'publishedAt': '2025-07-10T22:15:44Z'},
89+
},
90+
],
91+
},
92+
)
93+
94+
result = fetcher.fetch(query, id=['1', '2'])
95+
expected_report = garf_core.GarfReport(
96+
results=[
97+
[2, 11, 2, '2025-07-10T22:15:44Z'],
98+
[1, 10, 1, '2024-07-10T22:15:44Z'],
99+
],
100+
column_names=['id', 'views', 'likes', 'published_at'],
101+
)
102+
103+
assert result == expected_report

0 commit comments

Comments
 (0)