Huge cleanup and rework pass, postgres now usable too

This commit is contained in:
2018-02-19 23:57:14 -05:00
parent d996f7a2cb
commit a3a3c3a780
18 changed files with 472 additions and 110 deletions

View File

@ -5,12 +5,10 @@ using System.Threading.Tasks;
using Google.Apis.YouTube.v3;
using Microsoft.EntityFrameworkCore;
namespace YTManager.Tasks
{
public class FetchVideos
{
public static void run(string youtubechannelIDstr = "")
{
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) {
// YT API access key
var youtubeService = new YouTubeService(new Google.Apis.Services.BaseClientService.Initializer()
{
@ -18,49 +16,92 @@ namespace YTManager.Tasks
ApplicationName = "testingapppp"
});
// 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();
}
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) {
// Get the interface to the database.
var ops = new DbContextOptionsBuilder<MediaDB>();
ops.UseInMemoryDatabase(databaseName: "testdb");
ops.UseNpgsql(dbstr);
// Get all the channels to update.
using (var dbcontext = new MediaDB(ops.Options)) {
// Get all the potential relevant channels.
List<Models.Channel> channels;
if (youtubechannelIDstr == "")
channels = dbcontext.Channels.ToList();
else
channels = dbcontext.Channels.Where(c => c.YTChannelID == youtubechannelIDstr).ToList();
// Get all the channels from the db.
var channels = await (new MediaDB(ops.Options)).Channels.ToListAsync();
// Get all the most recent videos for each channel.
channels.ForEach(ch => {
// Get channel videos from youtube.
var query = youtubeService.Search.List("snippet");
query.ChannelId = ch.YTChannelID;
query.Order = SearchResource.ListRequest.OrderEnum.Date;
query.MaxResults = 50;
var response = query.Execute();
// For each channel, do an update.
channels.ForEach(async ch => await ChannelUpdate(dbstr, ch.YoutubeID));
}
// Get all videos which aren't already in the DB based on the ytid.
var notindb = response.Items
.Where(i => i.Id.Kind == "youtube#video")
.Where(i => !dbcontext.Videos.Any(dbvid => dbvid.YTVideoID == i.Id.VideoId))
.Select(newvid =>
new Models.Video {
Title = newvid.Snippet.Title,
Description = newvid.Snippet.Description,
YTVideoID = newvid.Id.VideoId,
AddedToYT = newvid.Snippet.PublishedAt.GetValueOrDefault(),
AddedtoDB = DateTime.Now,
channel = ch,
ThumbnailURL = newvid.Snippet.Thumbnails.Medium.Url
}).ToList();
// 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);
// Add all videos not already in the database over.
notindb.ForEach(newvid => dbcontext.Videos.Add(newvid));
// 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);
// And save since we are done.
dbcontext.SaveChanges();
});
// 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();
}
}
}