Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT channel_id, video_id FROM builtin.channelVideos
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from garf_youtube_data_api.builtins import channel_videos

BUILTIN_QUERIES = {
'channelVideos': channel_videos.get_youtube_channel_videos,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Fetches mapping between channel_id and its videos."""


def get_youtube_channel_videos(
report_fetcher: 'garf_youtube_data_api.YouTubeDataApiReportFetcher',
**kwargs: str,
):
channel_uploads_playlist_query = """
SELECT
contentDetails.relatedPlaylists.uploads AS uploads_playlist
FROM channels
"""
videos_playlist = report_fetcher.fetch(
channel_uploads_playlist_query, **kwargs
)

channel_videos_query = """
SELECT
contentDetails.videoId AS video_id
FROM playlistItems
"""
videos = report_fetcher.fetch(
channel_videos_query,
playlistId=videos_playlist.to_list(flatten=True, distinct=True),
maxResults=50,
).to_list(flatten=True, distinct=True)

video_performance_query = """
SELECT
snippet.channelId AS channel_id,
id AS video_id,
FROM videos
"""
return report_fetcher.fetch(video_performance_query, id=videos)
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from typing_extensions import override

from garf_core import parsers, report, report_fetcher
from garf_youtube_data_api import query_editor
from garf_youtube_data_api import builtins, query_editor
from garf_youtube_data_api.api_clients import YouTubeDataApiClient

ALLOWED_FILTERS: Final[set[str]] = (
Expand All @@ -32,7 +32,7 @@
'videoCategoryId',
'chart',
'forHandle',
'forUserName',
'forUsername',
'onBehalfOfContentOwner',
'playlistId',
'videoId',
Expand All @@ -58,12 +58,13 @@ def __init__(
query_spec: query_editor.YouTubeDataApiQuery = (
query_editor.YouTubeDataApiQuery
),
builtin_queries=builtins.BUILTIN_QUERIES,
**kwargs: str,
) -> None:
"""Initializes YouTubeDataApiReportFetcher."""
if not api_client:
api_client = YouTubeDataApiClient()
super().__init__(api_client, parser, query_spec, **kwargs)
super().__init__(api_client, parser, query_spec, builtin_queries, **kwargs)

@override
def fetch(
Expand All @@ -85,7 +86,6 @@ def fetch(
return super().fetch(query_specification, args, **kwargs)
for batch in _batched(ids, MAX_BATCH_SIZE):
batch_ids = {name: batch[0]} if name != 'id' else {name: batch}
results.append(
super().fetch(query_specification, args, **batch_ids, **kwargs)
)
res = super().fetch(query_specification, args, **batch_ids, **kwargs)
results.append(res)
return functools.reduce(operator.add, results)
Loading