|
1 | 1 | # garf for YouTube Data API |
2 | 2 |
|
| 3 | +Interacts with [YouTube Data API](https://developers.google.com/youtube/v3/docs). |
| 4 | + |
3 | 5 | ## Install |
4 | 6 |
|
5 | 7 | Install `garf-youtube-data-api` library |
6 | 8 |
|
| 9 | +/// tab | pip |
7 | 10 | ``` |
8 | 11 | pip install garf-executors garf-youtube-data-api |
9 | 12 | ``` |
| 13 | +/// |
| 14 | + |
| 15 | +/// tab | uv |
| 16 | +``` |
| 17 | +uv add garf-executors garf-youtube-data-api |
| 18 | +``` |
| 19 | +/// |
10 | 20 |
|
11 | 21 | ## Usage |
12 | 22 |
|
| 23 | +### Prerequisites |
| 24 | + |
| 25 | +* [YouTube Data API](https://console.cloud.google.com/apis/library/youtube.googleapis.com) enabled. |
| 26 | +* [API key](https://support.google.com/googleapi/answer/6158862?hl=en) to access to access YouTube Data API exposed as `export GARF_YOUTUBE_DATA_API_KEY=<YOUR_API_KEY>` |
| 27 | + |
| 28 | + |
| 29 | +/// tab | cli |
| 30 | +```bash |
| 31 | +echo "SELECT id, snippet.title AS channel_name FROM channels" > query.sql |
| 32 | +garf query.sql --source youtube-data-api \ |
| 33 | + --output csv \ |
| 34 | + --source.id=YOUTUBE_CHANNEL_ID |
13 | 35 | ``` |
14 | | -garf <PATH_TO_QUERIES> --source youtube-data-api \ |
15 | | - --output <OUTPUT_TYPE> \ |
16 | | - --source.<SOURCE_PARAMETER=VALUE> |
17 | | -``` |
| 36 | +/// |
| 37 | + |
| 38 | +/// tab | python |
18 | 39 |
|
19 | | -where: |
| 40 | +```python |
| 41 | +import os |
20 | 42 |
|
21 | | -* `<PATH_TO_QUERIES>` - local or remove files containing queries |
22 | | -* `<OUTPUT_TYPE>` - output supported by [`garf-io` library](../garf_io/README.md). |
23 | | -* `<SOURCE_PARAMETER=VALUE` - key-value pairs to refine fetching, check [available source parameters](#available-source-parameters). |
| 43 | +from garf_io import writer |
| 44 | +from garf_youtube_data_api import YouTubeDataApiReportFetcher |
| 45 | + |
| 46 | +query = 'SELECT id, snippet.title AS channel_name FROM channels' |
| 47 | + |
| 48 | +fetched_report = ( |
| 49 | + YouTubeDataApiReportFetcher(api_key=os.getenv('GARF_YOUTUBE_DATA_API_KEY')) |
| 50 | + .fetch(query, id=[YOUTUBE_CHANNEL_ID]) |
| 51 | +) |
| 52 | + |
| 53 | +csv_writer = writer.create_writer('csv') |
| 54 | +csv_writer.write(fetched_report, 'query') |
| 55 | +``` |
| 56 | +/// |
24 | 57 |
|
25 | 58 | ### Available source parameters |
26 | 59 |
|
27 | 60 | | name | values| comments | |
28 | 61 | |----- | ----- | -------- | |
29 | | -| `id` | id of YouTube channel or videos| Multiple ids are supported, should be comma-separated| |
| 62 | +| `id` | id(s) of YouTube channels or videos | Multiple ids are supported, should be comma-separated| |
30 | 63 | | `forHandle` | YouTube channel handle | i.e. @myChannel | |
31 | 64 | | `forUsername` | YouTube channel name | i.e. myChannel | |
32 | 65 | | `regionCode` | ISO 3166-1 alpha-2 country code | i.e. US | |
33 | 66 | | `chart` | `mostPopular` | Gets most popular in `regionCode`, can be narrowed down with `videoCategoriId` | |
| 67 | +| `videoId` | id(s) of YouTube Video to get comments from | Multiple ids are supported, should be comma-separated | |
| 68 | + |
| 69 | +## Examples |
| 70 | + |
| 71 | +### Videos |
| 72 | + |
| 73 | +**Gets meta information and statistics for YouTube videos.** |
| 74 | + |
| 75 | +/// tab | cli |
| 76 | +```bash |
| 77 | +echo " |
| 78 | +SELECT |
| 79 | + id, |
| 80 | + snippet.publishedAt AS published_at, |
| 81 | + snippet.title AS title, |
| 82 | + snippet.description AS description, |
| 83 | + snippet.channelTitle AS channel, |
| 84 | + snippet.tags AS tags, |
| 85 | + snippet.defaultLanguage AS language, |
| 86 | + snippet.defaultAudioLanguage AS audio_language, |
| 87 | + status.madeForKids AS made_for_kids, |
| 88 | + topicDetails.topicCategories AS topics, |
| 89 | + contentDetails.duration AS duration, |
| 90 | + contentDetails.caption AS has_caption, |
| 91 | + statistics.viewCount AS views, |
| 92 | + statistics.likeCount AS likes, |
| 93 | + statistics.commentCount AS comments, |
| 94 | + statistics.favoriteCount AS favourites |
| 95 | +FROM videos |
| 96 | +" > video_info.sql |
| 97 | + |
| 98 | +garf video_info.sql --source youtube-data-api \ |
| 99 | + --output csv \ |
| 100 | + --source.id=YOUTUBE_VIDEO_ID_1,YOUTUBE_VIDEO_ID_2 |
| 101 | +``` |
| 102 | +/// |
| 103 | + |
| 104 | +/// tab | python |
| 105 | + |
| 106 | +```python |
| 107 | +import os |
| 108 | + |
| 109 | +from garf_io import writer |
| 110 | +from garf_youtube_data_api import YouTubeDataApiReportFetcher |
| 111 | + |
| 112 | +query = """ |
| 113 | +SELECT |
| 114 | + id, |
| 115 | + snippet.publishedAt AS published_at, |
| 116 | + snippet.title AS title, |
| 117 | + snippet.description AS description, |
| 118 | + snippet.channelTitle AS channel, |
| 119 | + snippet.tags AS tags, |
| 120 | + snippet.defaultLanguage AS language, |
| 121 | + snippet.defaultAudioLanguage AS audio_language, |
| 122 | + status.madeForKids AS made_for_kids, |
| 123 | + topicDetails.topicCategories AS topics, |
| 124 | + contentDetails.duration AS duration, |
| 125 | + contentDetails.caption AS has_caption, |
| 126 | + statistics.viewCount AS views, |
| 127 | + statistics.likeCount AS likes, |
| 128 | + statistics.commentCount AS comments, |
| 129 | + statistics.favoriteCount AS favourites |
| 130 | +FROM videos |
| 131 | +""" |
| 132 | + |
| 133 | +fetched_report = ( |
| 134 | + YouTubeDataApiReportFetcher(api_key=os.getenv('GARF_YOUTUBE_DATA_API_KEY')) |
| 135 | + .fetch(query, id=[YOUTUBE_VIDEO_ID_1, YOUTUBE_VIDEO_ID_2]) |
| 136 | +) |
| 137 | + |
| 138 | +csv_writer = writer.create_writer('csv') |
| 139 | +csv_writer.write(fetched_report, 'video_info') |
| 140 | +``` |
| 141 | +/// |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +### Channels |
| 146 | + |
| 147 | +**Gets meta information and statistics for YouTube channel(s).** |
| 148 | + |
| 149 | +/// tab | cli |
| 150 | +```bash |
| 151 | +echo " |
| 152 | +SELECT |
| 153 | + id, |
| 154 | + snippet.title AS title, |
| 155 | + snippet.description AS description, |
| 156 | + snippet.publishedAt AS published_at, |
| 157 | + snippet.country AS country, |
| 158 | + snippet.defaultLanguage AS language, |
| 159 | + status.madeForKids AS made_for_kids, |
| 160 | + topicDetails.topicCategories AS topics, |
| 161 | + statistics.videoCount AS videos, |
| 162 | + statistics.viewCount AS views, |
| 163 | + statistics.subscriberCount AS subscribers |
| 164 | +FROM channels |
| 165 | +" > channel_info.sql |
| 166 | + |
| 167 | +garf channel_info.sql --source youtube-data-api \ |
| 168 | + --output csv \ |
| 169 | + --source.id=YOUTUBE_CHANNEL_ID |
| 170 | +``` |
| 171 | +/// |
| 172 | + |
| 173 | +/// tab | python |
| 174 | + |
| 175 | +```python |
| 176 | +import os |
| 177 | + |
| 178 | +from garf_io import writer |
| 179 | +from garf_youtube_data_api import YouTubeDataApiReportFetcher |
| 180 | + |
| 181 | +query = """ |
| 182 | +SELECT |
| 183 | + id, |
| 184 | + snippet.title AS title, |
| 185 | + snippet.description AS description, |
| 186 | + snippet.publishedAt AS published_at, |
| 187 | + snippet.country AS country, |
| 188 | + snippet.defaultLanguage AS language, |
| 189 | + status.madeForKids AS made_for_kids, |
| 190 | + topicDetails.topicCategories AS topics, |
| 191 | + statistics.videoCount AS videos, |
| 192 | + statistics.viewCount AS views, |
| 193 | + statistics.subscriberCount AS subscribers |
| 194 | +FROM channels |
| 195 | +""" |
| 196 | + |
| 197 | +fetched_report = ( |
| 198 | + YouTubeDataApiReportFetcher(api_key=os.getenv('GARF_YOUTUBE_DATA_API_KEY')) |
| 199 | + .fetch(query, id=[YOUTUBE_CHANNEL_ID]) |
| 200 | +) |
| 201 | + |
| 202 | +csv_writer = writer.create_writer('csv') |
| 203 | +csv_writer.write(fetched_report, 'channel_info') |
| 204 | +``` |
| 205 | +/// |
| 206 | + |
| 207 | + |
| 208 | +**Gets all public videos from YouTube channel(s)** |
| 209 | + |
| 210 | +/// tab | cli |
| 211 | +```bash |
| 212 | +echo " |
| 213 | +SELECT |
| 214 | + channel_id, |
| 215 | + video_id |
| 216 | +FROM builtin.channelVideos |
| 217 | +" > channel_videos.sql |
| 218 | + |
| 219 | +garf channel_videos.sql --source youtube-data-api \ |
| 220 | + --output csv \ |
| 221 | + --source.id=YOUTUBE_CHANNEL_ID |
| 222 | +``` |
| 223 | +/// |
| 224 | + |
| 225 | +/// tab | python |
| 226 | + |
| 227 | +```python |
| 228 | +import os |
| 229 | + |
| 230 | +from garf_io import writer |
| 231 | +from garf_youtube_data_api import YouTubeDataApiReportFetcher |
| 232 | + |
| 233 | +query = """ |
| 234 | +SELECT |
| 235 | + channel_id, |
| 236 | + video_id |
| 237 | +FROM builtin.channelVideos |
| 238 | +""" |
| 239 | + |
| 240 | +fetched_report = ( |
| 241 | + YouTubeDataApiReportFetcher(api_key=os.getenv('GARF_YOUTUBE_DATA_API_KEY')) |
| 242 | + .fetch(query, id=[YOUTUBE_CHANNEL_ID]) |
| 243 | +) |
| 244 | + |
| 245 | +csv_writer = writer.create_writer('csv') |
| 246 | +csv_writer.write(fetched_report, 'channel_videos') |
| 247 | +``` |
| 248 | +/// |
| 249 | + |
| 250 | +### Commentaries |
| 251 | + |
| 252 | +**Gets tops level commentaries for YouTube video(s).** |
| 253 | + |
| 254 | +/// tab | cli |
| 255 | +```bash |
| 256 | +echo " |
| 257 | +SELECT |
| 258 | + id AS commentary_id, |
| 259 | + snippet.videoId AS video_id, |
| 260 | + snippet.topLevelComment.snippet.textDisplay AS comment |
| 261 | +FROM commentThreads |
| 262 | +" > video_commentaries.sql |
| 263 | + |
| 264 | +garf video_commentaries.sql --source youtube-data-api \ |
| 265 | + --output csv \ |
| 266 | + --source.id=YOUTUBE_VIDEO_ID_1,YOUTUBE_VIDEO_ID_2 |
| 267 | +``` |
| 268 | +/// |
| 269 | + |
| 270 | +/// tab | python |
| 271 | + |
| 272 | +```python |
| 273 | +import os |
| 274 | + |
| 275 | +from garf_io import writer |
| 276 | +from garf_youtube_data_api import YouTubeDataApiReportFetcher |
| 277 | + |
| 278 | + |
| 279 | +query = """ |
| 280 | +SELECT |
| 281 | + id AS commentary_id, |
| 282 | + snippet.videoId AS video_id, |
| 283 | + snippet.topLevelComment.snippet.textDisplay AS comment |
| 284 | +FROM commentThreads |
| 285 | +""" |
| 286 | + |
| 287 | +fetched_report = ( |
| 288 | + YouTubeDataApiReportFetcher(api_key=os.getenv('GARF_YOUTUBE_DATA_API_KEY')) |
| 289 | + .fetch(query, id=[YOUTUBE_VIDEO_ID_1, YOUTUBE_VIDEO_ID_2]) |
| 290 | +) |
| 291 | + |
| 292 | +csv_writer = writer.create_writer('csv') |
| 293 | +csv_writer.write(fetched_report, 'video_commentaries') |
| 294 | +``` |
| 295 | +/// |
0 commit comments