2017-09-01 08:55:02 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Google.Apis.YouTube.v3;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
2018-02-20 04:57:14 +00:00
|
|
|
|
namespace YTManager.Tasks {
|
|
|
|
|
public class FetchVideos {
|
|
|
|
|
// Get a bunch of videos from youtube that the channel generated.
|
|
|
|
|
private static async Task<List<Models.Video>> Get_YTVideos(string channelID) {
|
2017-09-01 08:55:02 +00:00
|
|
|
|
// YT API access key
|
|
|
|
|
var youtubeService = new YouTubeService(new Google.Apis.Services.BaseClientService.Initializer()
|
|
|
|
|
{
|
|
|
|
|
ApiKey = "AIzaSyCuIYkMc5SktlnXRXNaDf2ObX-fQvtWCnQ",
|
|
|
|
|
ApplicationName = "testingapppp"
|
|
|
|
|
});
|
|
|
|
|
|
2018-02-20 04:57:14 +00:00
|
|
|
|
// Search youtube for all the relevant data of the channel.
|
|
|
|
|
var query = youtubeService.Search.List("snippet");
|
|
|
|
|
query.ChannelId = channelID;
|
|
|
|
|
query.Order = SearchResource.ListRequest.OrderEnum.Date;
|
|
|
|
|
query.MaxResults = 50;
|
|
|
|
|
var response = await query.ExecuteAsync();
|
|
|
|
|
|
|
|
|
|
// Convert the response into models.
|
|
|
|
|
return response.Items?
|
|
|
|
|
.Where(i => i.Id.Kind == "youtube#video")
|
|
|
|
|
.Select(newvid => new Models.Video
|
|
|
|
|
{
|
|
|
|
|
Title = newvid.Snippet.Title,
|
|
|
|
|
Description = newvid.Snippet.Description,
|
|
|
|
|
YoutubeID = newvid.Id.VideoId,
|
|
|
|
|
AddedToYT = newvid.Snippet.PublishedAt.GetValueOrDefault(),
|
|
|
|
|
AddedtoDB = DateTime.Now,
|
|
|
|
|
ThumbnailURL = newvid.Snippet.Thumbnails.Medium.Url
|
|
|
|
|
}).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-20 05:13:06 +00:00
|
|
|
|
// Gets some info about a youtube channel.
|
2018-02-20 04:57:14 +00:00
|
|
|
|
public static async Task<Models.Channel> Get_YTChannel(string channelID) {
|
|
|
|
|
// YT API access key
|
|
|
|
|
var youtubeService = new YouTubeService(new Google.Apis.Services.BaseClientService.Initializer()
|
|
|
|
|
{
|
|
|
|
|
ApiKey = "AIzaSyCuIYkMc5SktlnXRXNaDf2ObX-fQvtWCnQ",
|
|
|
|
|
ApplicationName = "testingapppp"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Search youtube for all the relevant data of the channel.
|
|
|
|
|
var query = youtubeService.Channels.List("snippet");
|
|
|
|
|
query.Id = channelID;
|
|
|
|
|
query.MaxResults = 1;
|
|
|
|
|
var response = await query.ExecuteAsync();
|
|
|
|
|
|
|
|
|
|
// Parse the response into a channel.
|
|
|
|
|
return new Models.Channel {
|
|
|
|
|
Description = response.Items.First().Snippet.Description,
|
|
|
|
|
Title = response.Items.First().Snippet.Title,
|
|
|
|
|
ThumbnailURL = response.Items.First().Snippet.Thumbnails.Medium.Url,
|
|
|
|
|
YoutubeID = channelID,
|
|
|
|
|
AddedtoDB = DateTime.Now
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update videos for all our channels.
|
|
|
|
|
public static async Task MassUpdate(string dbstr) {
|
2018-02-18 19:06:16 +00:00
|
|
|
|
// Get the interface to the database.
|
2017-09-01 08:55:02 +00:00
|
|
|
|
var ops = new DbContextOptionsBuilder<MediaDB>();
|
2018-02-20 04:57:14 +00:00
|
|
|
|
ops.UseNpgsql(dbstr);
|
|
|
|
|
|
|
|
|
|
// Get all the channels from the db.
|
2018-02-20 05:13:06 +00:00
|
|
|
|
var channel_ids = await
|
|
|
|
|
(new MediaDB(ops.Options))
|
|
|
|
|
.Channels.Select(ch => ch.YoutubeID)
|
|
|
|
|
.ToListAsync();
|
2018-02-20 04:57:14 +00:00
|
|
|
|
|
|
|
|
|
// For each channel, do an update.
|
2018-02-20 05:13:06 +00:00
|
|
|
|
channel_ids.ForEach(async id => await ChannelUpdate(dbstr, id));
|
2018-02-20 04:57:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update videos for just one channel.
|
|
|
|
|
public static async Task ChannelUpdate(string dbstr, string youtubechannelIDstr) {
|
|
|
|
|
// Get the interface to the database.
|
|
|
|
|
var ops = new DbContextOptionsBuilder<MediaDB>();
|
|
|
|
|
ops.UseNpgsql(dbstr);
|
|
|
|
|
var db = new MediaDB(ops.Options);
|
|
|
|
|
|
|
|
|
|
// Get the channel from the db when including it's videos.
|
|
|
|
|
var channel = await db.Channels
|
|
|
|
|
.Include(c => c.Videos)
|
|
|
|
|
.SingleOrDefaultAsync(ch => ch.YoutubeID == youtubechannelIDstr);
|
|
|
|
|
|
|
|
|
|
// Update the channel if it was found.
|
|
|
|
|
if (channel != null) {
|
|
|
|
|
// Get all the new videos for the channel.
|
|
|
|
|
var Videos = await Get_YTVideos(channel.YoutubeID);
|
|
|
|
|
|
|
|
|
|
// Get all the videos which haven't been put into this channels videos.
|
|
|
|
|
var newvids = Videos.Where(nv => !channel.Videos.Any(cv => cv.YoutubeID == nv.YoutubeID));
|
|
|
|
|
|
|
|
|
|
// Add all the videos to the databse.
|
|
|
|
|
await db.Videos.AddRangeAsync(newvids);
|
|
|
|
|
|
|
|
|
|
// Update the videos this channel refers to.
|
|
|
|
|
channel.Videos.AddRange(newvids);
|
|
|
|
|
|
|
|
|
|
// And say the database should be changed.
|
|
|
|
|
await db.SaveChangesAsync();
|
2017-09-01 08:55:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|