HUGE arch changes to make searching work right

This commit is contained in:
2018-03-05 01:08:55 -05:00
parent 96a26ce30d
commit 9f5fb78835
32 changed files with 800 additions and 447 deletions

View File

@ -53,14 +53,5 @@ namespace YTManager.Controllers {
public IActionResult Get_Update_Status() {
return Ok(get_massupdatedaemon() == null ? "false" : "true");
}
// Testing
[HttpGet("Test")]
public async System.Threading.Tasks.Task<IActionResult> Test() {
// await Tasks.FetchVideos.MassUpdate(Startup.DBStr);
// var vids = Tasks.FetchVideos.Get_YTVideos("UCsXVk37bltHxD1rDPwtNM8Q", 1).Result;
// return Ok(vids);
return Ok();
}
}
}

View File

@ -1,14 +1,13 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
namespace YTManager.Controllers {
[Produces("application/json")]
[Route("api/Channels")]
[EnableCors("AllowAllOrigins")]
public class ChannelsController : Controller {
// Custom return type for API accesses. Done this way to ensure we
// always return the expected data regardless of the underlying model.
@ -16,6 +15,7 @@ namespace YTManager.Controllers {
public string Title;
public string Description;
public string ID;
public List<string> User_Tags;
public List<string> Video_IDs;
public Channel_ForAPI(Models.Channel c) {
@ -23,6 +23,7 @@ namespace YTManager.Controllers {
Description = c.Description;
ID = c.YoutubeID;
Video_IDs = c.Videos.Select(v => v.YoutubeID).ToList();
User_Tags = c.UserTags == null ? new List<string>() : c.UserTags.ToList();
}
}
@ -38,6 +39,9 @@ namespace YTManager.Controllers {
// Returns the most recently added channels.
[HttpGet]
public async Task<IActionResult> Get() {
// Log this to the terminal.
Console.WriteLine($"{DateTime.Now} == Channels GET");
// Get all the relevant channels.
var chanels = await db.Channels
.Include(c => c.Videos)
@ -53,5 +57,29 @@ namespace YTManager.Controllers {
// Convert all the videos to what we will send back.
return Ok(converted);
}
[HttpPost("{channelid}")]
public async Task<IActionResult> PostChannel([FromRoute] string channelid) {
Console.WriteLine($"{DateTime.Now} == Channels POST -> {channelid}");
// Verify the channel looks resonable.
var expected_len = "UCyS4xQE6DK4_p3qXQwJQAyA".Length;
if (channelid.Length != expected_len)
return BadRequest($"Length should be {expected_len} but is {channelid.Length}");
// Only add it to the databse if it's not in there already.
if (db.Channels.Any(c => c.YoutubeID == channelid))
return BadRequest($"Channel {channelid} is already in DB!");
// Get the channel contents from youtube.
var channel = await Tasks.FetchVideos.Get_YTChannel(channelid);
// Add it to the databse.
await db.Channels.AddAsync(channel);
await db.SaveChangesAsync();
// Say all went ok.
return Ok();
}
}
}

View File

@ -82,23 +82,6 @@ namespace YTManager.Controllers.Private
return NoContent();
}
[HttpPost("{channelid}")]
public async Task<IActionResult> PostChannel([FromRoute] string channelid) {
// Only add it to the databse if it's not in there already.
if (db.Channels.Any(c => c.YoutubeID == channelid))
return BadRequest();
// Get the channel contents from youtube.
var channel = await Tasks.FetchVideos.Get_YTChannel(channelid);
// Add it to the databse.
await db.Channels.AddAsync(channel);
await db.SaveChangesAsync();
// Say all went ok.
return Ok(channel);
}
[HttpPost]
public async Task<IActionResult> PostChannel([FromBody] Channel channel)
{

View File

@ -3,14 +3,12 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace YTManager.Controllers {
[Produces("application/json")]
[Route("api/Videos")]
[EnableCors("AllowAllOrigins")]
public class VideosController : Controller {
// Custom return type for API accesses. Done this way to ensure we
// always return the expected data regardless of the underlying model.
@ -44,7 +42,7 @@ namespace YTManager.Controllers {
Thumbnail = video.ThumbnailURL;
Channel = video.Channel.Title;
Seconds = (int)video.Duration.TotalSeconds;
Tags = video.Tags?.Select(t => t.Name).ToList();
Tags = video.Tags == null ? new List<string>() : video.Tags.ToList();
}
}
@ -57,31 +55,7 @@ namespace YTManager.Controllers {
// Constructor to fetch the db context.
public VideosController(MediaDB context) => db = context;
// Returns the most recent videos.
[HttpGet]
public async Task<IActionResult> GetVideos() {
// Get all the relevant videos.
var vids = await db.Videos
.OrderByDescending(i => i.AddedToYT)
.Take(max_per_query)
.Include(v => v.Channel)
.Include(v => v.Tags)
.ToListAsync();
// Convert them to what we will send out.
var converted = vids
.Select(v => new Video_ForAPI(v))
.ToList();
// Convert all the videos to what we will send back.
return Ok(converted);
}
struct Search_Query
{
// Tags.
public List<Models.Tag> Tags;
struct Search_Query {
// What the duration type is.
public enum _Duration_Type { LessThan, GreaterThan, Unset };
public _Duration_Type Duration_Type;
@ -92,11 +66,27 @@ namespace YTManager.Controllers {
// Remaining tokens.
public List<String> Remaining_Tokens;
}
// Searches for videos using the specified tag(s).
[HttpGet("search/{searchstr}")]
[HttpGet]
public async Task<IActionResult> Search() {
var converted = await db.Videos
.OrderByDescending(i => i.AddedToYT)
.Take(max_per_query)
.Include(v => v.Channel)
.Select(v => new Video_ForAPI(v))
.ToListAsync();
// Convert all the videos to what we will send back.
return Ok(converted);
}
// Searches for videos using the specified tag(s).
[HttpGet("{searchstr}")]
public async Task<IActionResult> Search([FromRoute] string searchstr) {
// Log this to the terminal.
Console.WriteLine($"{DateTime.Now} == Search request for -> {searchstr}.");
// What to use for searching videos with.
var parsed_query = new Search_Query();
@ -128,12 +118,6 @@ namespace YTManager.Controllers {
}
}
// Find which tokens are explicit tags
parsed_query.Tags = await db.Tags
.Where(t => parsed_query.Remaining_Tokens.Any(token => token == t.Name.ToLower()))
.ToListAsync();
parsed_query.Tags.ForEach(tag => parsed_query.Remaining_Tokens.Remove(tag.Name.ToLower()));
// Get from the database all videos which satisfy the query via
// AND'ing all the queries.
var dbquery = db.Videos.Select(v => v);
@ -144,16 +128,14 @@ namespace YTManager.Controllers {
else if (parsed_query.Duration_Type == Search_Query._Duration_Type.LessThan)
dbquery = dbquery.Where(v => v.Duration <= parsed_query.Duration);
// Match videos where the tag matches.
parsed_query.Tags.ForEach(tag => dbquery = dbquery.Where(V => V.Tags.Any(vt => vt.Name == tag.Name)));
// Get all videos that match their title, description, or channel name
// with the remaining tokens.
parsed_query.Remaining_Tokens.ForEach(token => {
dbquery = dbquery.Where(v =>
v.Channel.Title.ToLower().Contains(token) ||
v.Title.ToLower().Contains(token) ||
v.Description.ToLower().Contains(token));
v.Description.ToLower().Contains(token) ||
v.YoutubeID.ToLower().Contains(token));
});
// Get all the relevant videos.
@ -161,7 +143,6 @@ namespace YTManager.Controllers {
.OrderByDescending(i => i.AddedToYT)
.Take(max_per_query)
.Include(v => v.Channel)
.Include(v => v.Tags)
.ToListAsync();
// Convert them to what we will send out.
@ -169,26 +150,8 @@ namespace YTManager.Controllers {
.Select(v => new Video_ForAPI(v))
.ToList();
// Convert all the videos to what we will send back.
return Ok(converted);
}
// Returns the most recent videos of a channel.
[HttpGet("fromchannel/{channelName}")]
public async Task<IActionResult> Get_Channel_Videos([FromRoute] string channelName) {
// Get all the relevant videos.
var vids = await db.Videos
.Where(v => v.Channel.Title == channelName)
.OrderByDescending(i => i.AddedToYT)
.Take(max_per_query)
.Include(v => v.Channel)
.Include(v => v.Tags)
.ToListAsync();
// Convert them to what we will send out.
var converted = vids
.Select(v => new Video_ForAPI(v))
.ToList();
// Log this to the terminal.
Console.WriteLine($"{DateTime.Now} == Search request for -> {searchstr} found {converted.Count()} videos.");
// Convert all the videos to what we will send back.
return Ok(converted);