Skip to content

Instantly share code, notes, and snippets.

@drasticactions
Created May 17, 2018 00:18
Show Gist options
  • Save drasticactions/4d9c260e49cce455942825a28f7c78cd to your computer and use it in GitHub Desktop.
Save drasticactions/4d9c260e49cce455942825a28f7c78cd to your computer and use it in GitHub Desktop.
MetalJesusBot
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MetalJesusThumbnails
{
class Program
{
static void Main(string[] args)
{
if (!File.Exists("client_secret.json"))
{
Console.WriteLine("You must add your Google OAUTH Client JSON! Download it, name it `client_secret.json` and put it in the base directory!");
return;
}
var path = "videos";
if (args.Length >= 1)
path = args[0];
UserCredential credential;
using (FileStream stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { YouTubeService.Scope.Youtube, YouTubeService.Scope.YoutubeForceSsl },
"user",
CancellationToken.None,
new FileDataStore("YouTube.Auth.Store")).Result;
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "MetalJesusBot"
});
var database = new Database();
UpdateVideoDatabase(youtubeService, database);
DownloadThumbnails(path, database);
}
static string CleanFileName(string fileName)
{
return Path.GetInvalidFileNameChars().Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty));
}
static void DownloadThumbnails(string path, Database database)
{
Directory.CreateDirectory(path);
using (var client = new WebClient())
{
foreach(var vid in database.GetVideos())
{
var vidId = string.IsNullOrEmpty(vid.VideoId) ? CleanFileName(vid.Title) : vid.VideoId;
var filename = Path.Combine(path, $"{vidId}{Path.GetExtension(vid.ThumbnailUrl)}");
if (!File.Exists(filename))
{
Console.WriteLine($"Downloading: {filename}");
client.DownloadFile(vid.ThumbnailUrl, filename);
}
}
}
}
static void UpdateVideoDatabase(YouTubeService youtubeService, Database database)
{
Console.WriteLine("Checking for new videos...");
var searchListRequest = youtubeService.Search.List("snippet");
searchListRequest.MaxResults = 50;
searchListRequest.ChannelId = "UCEFymXY4eFCo_AchSpxwyrg";
var nextPageToken = "";
var count = 0;
while (true)
{
var oldVids = database.GetVideos().Select(n => n.VideoId);
if (!string.IsNullOrEmpty(nextPageToken))
searchListRequest.PageToken = nextPageToken;
var searchListResult = searchListRequest.Execute();
var youtubeVids = searchListResult.Items.Select(n => new YoutubeVideo()
{
ChannelId = n.Snippet.ChannelId,
ChannelTitle = n.Snippet.ChannelTitle,
Description = n.Snippet.Description,
ThumbnailUrl = GetThumbnailUrl(n.Snippet.Thumbnails),
Title = n.Snippet.Title,
VideoId = n.Id.VideoId,
PublishedDate = n.Snippet.PublishedAt.Value
}).ToList();
// Should be fresh list of videos.
var list = youtubeVids.Where(n => !oldVids.Contains(n.VideoId)).ToList();
var uploaded = database.UpsertVideos(list);
count += uploaded;
if (uploaded > 0)
Console.WriteLine($"Got {count} videos...");
if (string.IsNullOrEmpty(searchListResult.NextPageToken))
break;
nextPageToken = searchListResult.NextPageToken;
}
}
static string GetThumbnailUrl(ThumbnailDetails deets)
{
if (deets.Maxres != null)
return deets.Maxres.Url;
if (deets.High != null)
return deets.High.Url;
if (deets.Medium != null)
return deets.Medium.Url;
if (deets.Standard != null)
return deets.Standard.Url;
return "";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment