-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search.cs
186 lines (176 loc) · 8.39 KB
/
Search.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using System.Collections.Generic;
using Google.Apis.Services;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
namespace YTSharp
{
internal class Search
{
[STAThread]
static void Main(string[] args)
{
//try
//{
switch (args[0].ToUpper())
{
case "UPLOADS":
new Search().RunUploads(args).Wait();
break;
case "PLAYLISTS":
new Search().RunPlaylists(args).Wait();
break;
default:
Console.WriteLine("todo add help");
break;
}
//}
//catch (AggregateException ex)
//{
// foreach (var e in ex.InnerExceptions)
// {
// Console.Error.WriteLine("Error: " + e.Message);
// }
//}
}
private async Task RunUploads(string[] args)
{
//configure yt api client
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = args[Array.IndexOf(args, "-k") + 1],
ApplicationName = this.GetType().ToString()
});
//create a channel request for the channel
var channelListRequest = youtubeService.Channels.List("contentDetails");
//get channelid from args
channelListRequest.Id = args[Array.IndexOf(args, "-c") + 1];
//execute request and get the channels uploads playlist from the result
var channelResponse = await channelListRequest.ExecuteAsync();
string uploadsID = channelResponse.Items.First().ContentDetails.RelatedPlaylists.Uploads;
//create a playlist item request
var playlistItemsListRequest = youtubeService.PlaylistItems.List("snippet,contentDetails");
//set playlistid to channels uploads id and set MaxResults from args
playlistItemsListRequest.PlaylistId = uploadsID;
playlistItemsListRequest.MaxResults = args[Array.IndexOf(args, "-a") + 1].ToUpper() == "ALL" ? 50 : int.Parse(args[Array.IndexOf(args, "-a") + 1]);
//Create enumerable for videos
List<Models.Video> videos = new();
PlaylistItemListResponse playlistItemsListResponse = await playlistItemsListRequest.ExecuteAsync();
//for each available page of the response amount (defined in args)
for (int i = 0; i < Math.Ceiling(double.Parse(GetAmount(args, playlistItemsListResponse.PageInfo.TotalResults.ToString())) / 50); i++)
{
//loop response items and add them to the list
foreach (var item in playlistItemsListResponse.Items)
{
//private videos have no timestamp lol
if (item.ContentDetails.VideoPublishedAt == null)
continue;
//Add video to list
videos.Add(new Models.Video
{
Name = item.Snippet.Title,
Description = item.Snippet.Description,
CreationTime = item.ContentDetails.VideoPublishedAt.Value.ToUniversalTime(),
URL = item.Snippet.ResourceId.VideoId
});
}
//ready for next iteration if not on last iteration
if (i != Math.Ceiling(double.Parse(GetAmount(args, playlistItemsListResponse.PageInfo.TotalResults.ToString())) / 50) - 1)
{
playlistItemsListRequest.PageToken = playlistItemsListResponse.NextPageToken;
playlistItemsListResponse = await playlistItemsListRequest.ExecuteAsync();
}
}
//remove elements over specified limit
if (args[Array.IndexOf(args, "-a") + 1].ToUpper() != "ALL")
videos.RemoveRange(int.Parse(args[Array.IndexOf(args, "-a") + 1]), videos.Count - int.Parse(args[Array.IndexOf(args, "-a") + 1]));
//serialize and output the enumerable
string output = JsonSerializer.Serialize(videos);
Console.WriteLine(output);
}
private async Task RunPlaylists(string[] args)
{
//configure yt api client
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = args[Array.IndexOf(args, "-k") + 1],
ApplicationName = this.GetType().ToString()
});
//create a playlist request for all playlists of a channel
var playlistListRequest = youtubeService.Playlists.List("snippet");
playlistListRequest.ChannelId = args[Array.IndexOf(args, "-c") + 1];
playlistListRequest.MaxResults = 50;
List<Models.Playlist> playlists = new();
var playlistListResponse = await playlistListRequest.ExecuteAsync();
//for each available page of the response amount
for (int i = 0; i < Math.Ceiling((double)playlistListResponse.PageInfo.TotalResults / 50); i++)
{
//for each playlists of the channel
foreach (var item in playlistListResponse.Items)
{
//create new playlist entry
Models.Playlist playlist = new()
{
ID = item.Id,
Name = item.Snippet.Title,
Videos = new()
};
//build playlist request
var playlistItemListRequest = youtubeService.PlaylistItems.List("snippet,contentDetails");
playlistItemListRequest.PlaylistId = item.Id;
playlistItemListRequest.MaxResults = 50;
var playlistItemListResponse = await playlistItemListRequest.ExecuteAsync();
//for each available page of the response amount
for (int inside = 0; inside < Math.Ceiling((double)playlistItemListResponse.PageInfo.TotalResults / 50); inside++)
{
//foreach video in the playlist
foreach (var video in playlistItemListResponse.Items)
{
//private videos have no timestamp lol
if (video.ContentDetails.VideoPublishedAt == null)
continue;
//add video to playlist
playlist.Videos.Add(new()
{
Name = video.Snippet.Title,
Description = video.Snippet.Description,
CreationTime = video.ContentDetails.VideoPublishedAt.Value.ToUniversalTime(),
URL = video.Snippet.ResourceId.VideoId
});
}
//ready for next iteration if not on last iteration
if (inside != Math.Ceiling((double)playlistItemListResponse.PageInfo.TotalResults / 50) - 1)
{
playlistItemListRequest.PageToken = playlistItemListResponse.NextPageToken;
playlistItemListResponse = await playlistItemListRequest.ExecuteAsync();
}
}
//add playlist item to list
playlists.Add(playlist);
}
//ready for next iteration if not on last iteration
if (i != Math.Ceiling((double)playlistListResponse.PageInfo.TotalResults / 50) - 1)
{
playlistListRequest.PageToken = playlistListResponse.NextPageToken;
playlistListResponse = await playlistListRequest.ExecuteAsync();
}
}
//serialize and output the list
string output = JsonSerializer.Serialize(playlists);
Console.WriteLine(output);
}
/// <summary>
/// get amount from args and return it or a fallback if not defined
/// </summary>
/// <param name="args">Command line arguments</param>
/// <param name="fallbackAmount">value to use if no args defined</param>
/// <returns>amount or fallback</returns>
private static string GetAmount(string[] args, string fallbackAmount)
{
return args[Array.IndexOf(args, "-a") + 1].ToUpper() == "ALL" ? fallbackAmount : args[Array.IndexOf(args, "-a") + 1];
}
}
}