This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebuild_video_list.cs
77 lines (72 loc) · 2.44 KB
/
rebuild_video_list.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
using System;
using System.Collections.Generic;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using System.Linq;
using Microsoft.Extensions.Logging;
using Google.Apis.Services;
using Google.Apis.YouTube.v3;
using System.Text;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Documents;
using System.Web.Http;
namespace SilvaGunnerPokemon
{
public static class rebuild_video_list
{
private static string YouTubeToken = Environment.GetEnvironmentVariable("YouTubeToken");
public static List<string> GetVideos()
{
List<string> result = new List<string>();
try
{
var yt = new YouTubeService(new BaseClientService.Initializer() { ApiKey = YouTubeToken });
var channelsListRequest = yt.Channels.List("contentDetails");
channelsListRequest.Id = "UC9ecwl3FTG66jIKA9JRDtmg";
var channelsListResponse = channelsListRequest.Execute();
foreach (var channel in channelsListResponse.Items)
{
// of videos uploaded to the authenticated user's channel.
var uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads;
var nextPageToken = "";
while (nextPageToken != null)
{
var playlistItemsListRequest = yt.PlaylistItems.List("snippet");
playlistItemsListRequest.PlaylistId = uploadsListId;
playlistItemsListRequest.MaxResults = 50;
playlistItemsListRequest.PageToken = nextPageToken;
// Retrieve the list of videos uploaded to the authenticated user's channel.
var playlistItemsListResponse = playlistItemsListRequest.Execute();
var matching = (from i in playlistItemsListResponse.Items where i.Snippet.Title.ToLower().Contains("pokemon") || i.Snippet.Title.ToLower().Contains("pokémon") select i.Snippet.ResourceId.VideoId).ToList();
if (matching.Count != 0)
result.AddRange(matching);
nextPageToken = playlistItemsListResponse.NextPageToken;
}
}
}
catch (Exception e)
{
throw e;
}
return result;
}
[FunctionName("rebuild_video_list")]
public static void Run(
[TimerTrigger("0 0 * * * *")]TimerInfo myTimer,
[Blob("silvagunnerlist/items", FileAccess.Write)] Stream items,
ILogger log
)
{
try
{
List<string> items_list = GetVideos();
items.Write(Encoding.ASCII.GetBytes(String.Join('\n', items_list)));
log.LogInformation($"Updated the video list at {DateTime.Now}");
} catch (Exception e)
{
log.LogError($"Error updating the database:\n{e}");
}
}
}
}