Skip to content

Commit

Permalink
Merge pull request #17 from MilchRatchet/dev-1.9
Browse files Browse the repository at this point in the history
Version 1.9
  • Loading branch information
MilchRatchet authored Aug 5, 2022
2 parents feed1d3 + 5380285 commit 6d9f862
Show file tree
Hide file tree
Showing 16 changed files with 498 additions and 283 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,4 @@ healthchecksdb
/SubBox/settings.json.backup
/SubBox/youtube-dl_OLD.exe
/SubBox/saves_intro181.db
/SubBox/wwwroot/channelPictures/*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SubBox [![Github Release](https://img.shields.io/badge/Release-1.8.1-red)](https://github.com/MilchRatchet/SubBox/releases)
# SubBox [![Github Release](https://img.shields.io/badge/Release-1.9-red)](https://github.com/MilchRatchet/SubBox/releases)
`SubBox` is a tool to manage the uploaded videos of your favourite Youtube channels. It resembles the `Subscriptions` tab on Youtube.

![DemoImage1](SubBox/wwwroot/media/intro1.png)
Expand Down
12 changes: 0 additions & 12 deletions SubBox/.config/dotnet-tools.json

This file was deleted.

27 changes: 16 additions & 11 deletions SubBox/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ValuesController : ControllerBase

public ValuesController(AppDbContext context)
{
this.context = context;
this.context = context;
}

// GET: api/values/videos
Expand All @@ -34,7 +34,7 @@ public async Task<ActionResult<IEnumerable<Video>>> GetVideos()

// GET: api/values/localvideos
[HttpGet("localvideos")]
public IEnumerable<KeyValuePair<string,LocalVideo>> GetLocalVideos()
public IEnumerable<KeyValuePair<string, LocalVideo>> GetLocalVideos()
{
return LocalCollection.DownloadedVideos.ToList();
}
Expand Down Expand Up @@ -161,21 +161,26 @@ public void Refresh()
// POST: api/values/channel/{name}
[HttpPost("channel/{name}")]
public void PostChannel(string name)
{
{
Logger.Info("Adding " + name + " to Database...");

DataRetriever fetcher = new DataRetriever();

Channel channel = fetcher.AddChannel(name);
List<Channel> channels = fetcher.AddChannels(name);

if (channel == null)
_ = StatusBoard.PutStatus("channelResult", name, (channels.Count != 0) ? "true" : "false");

if (channels.Count == 0)
{
Logger.Warn("channel:" + name + " was requested but is not present in db");
Logger.Warn("channel:" + name + " was requested but no matching channels were found! Report this on Github!");

return;
}

Downloader.AddChannelPicture(channel);
foreach (Channel channel in channels)
{
Downloader.AddChannelPicture(channel);
}
}

// POST: api/values/list/add/{id}/{listNumber}
Expand Down Expand Up @@ -288,7 +293,7 @@ public void ReactivateVideo(string id)

context.SaveChanges();
}
catch(Exception e)
catch (Exception e)
{
Logger.Warn("video: " + id + "could not be reactivated");

Expand All @@ -306,7 +311,7 @@ public void DownloadVideo(string id)
{
Downloader.DownloadVideo(id);
}
catch(Exception e)
catch (Exception e)
{
Logger.Warn("video: " + id + "could not be downloaded");

Expand All @@ -333,7 +338,7 @@ public async void SaveSettings()
{
string raw = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();

JsonConvert.DeserializeObject(raw, typeof(AppSettings), new JsonSerializerSettings() { ObjectCreationHandling = ObjectCreationHandling.Replace});
JsonConvert.DeserializeObject(raw, typeof(AppSettings), new JsonSerializerSettings() { ObjectCreationHandling = ObjectCreationHandling.Replace });

AppSettings.Save();
}
Expand All @@ -344,7 +349,7 @@ public void AddTag(string name)
{
try
{
context.Tags.Add(new Tag{ Name = name, Filter = string.Empty});
context.Tags.Add(new Tag { Name = name, Filter = string.Empty });

context.SaveChanges();
}
Expand Down
4 changes: 4 additions & 0 deletions SubBox/Models/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public enum DownloadQuality { H2160F60, H2160F30, H1440F60, H1440F30, H1080F60,
public static DownloadQuality PreferredQuality { get; set; }
[JsonProperty]
public static List<bool> DisplayPlaylists { get; set; }
[JsonProperty]
public static bool AutoDeleteShorts { get; set; } = false;
[JsonProperty]
public static bool UseEmbedLinks { get; set; } = false;

//Runtime vars
public static bool GCMode { get; set; }
Expand Down
91 changes: 63 additions & 28 deletions SubBox/Models/DataRetriever.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,13 @@ public DataRetriever()
});
}

public Channel AddChannel(string name)
private Channel RetrieveChannelData(string ID)
{
LifeTime = AppSettings.NewChannelTimeFrame;

var Request = service.Channels.List("snippet");

if (name.Length == 24)
{
Request.Id = name;
}
else
{
Request.ForUsername = name;
}
Request.Id = ID;

Request.MaxResults = 1;

var Response = Request.Execute();

Expand All @@ -52,7 +45,7 @@ public Channel AddChannel(string name)
{
Id = Response.Items.First().Id,

Username = name,
Username = Response.Items.First().Snippet.CustomUrl,

Displayname = Response.Items.First().Snippet.Title,

Expand All @@ -61,12 +54,17 @@ public Channel AddChannel(string name)

using (AppDbContext context = new AppDbContext())
{
if (context.Channels.Any(c => c.Id == NewChannel.Id))
{
Logger.Debug("Channel " + NewChannel.Id + " was already present!");

return null;
}

context.Channels.Add(NewChannel);

context.SaveChanges();

_ = StatusBoard.PutStatus("channelResult", name, "true");

List<string> list = RequestVideoIdsFromChannel(NewChannel.Id).GetAwaiter().GetResult();

if (list.Count == 0) return NewChannel;
Expand All @@ -89,16 +87,45 @@ public Channel AddChannel(string name)

return NewChannel;
}
catch (Exception)
catch (Exception e)
{
Logger.Info(name + " couldn't be added");
Logger.Warn("Channel with ID " + ID + " couldn't be added! This shouldn't happen! Reason: " + e.Message);

_ = StatusBoard.PutStatus("channelResult", name, "false");
Logger.Error(e.InnerException.Message);

return null;
}
}

public List<Channel> AddChannels(string name)
{
LifeTime = AppSettings.NewChannelTimeFrame;

List<Channel> result = new List<Channel>();

var ChannelsRequest = service.Search.List("snippet");

ChannelsRequest.Q = name;
ChannelsRequest.Type = "channel";
ChannelsRequest.MaxResults = 50;

var ChannelsResponse = ChannelsRequest.Execute();

foreach (var response in ChannelsResponse.Items)
{
Logger.Debug("Channel found with title: " + response.Snippet.ChannelTitle);
if (response.Snippet.ChannelTitle == name)
{
Channel addedChannel = RetrieveChannelData(response.Id.ChannelId);

if (addedChannel != null)
result.Add(addedChannel);
}
}

return result;
}

private async Task RequestVideosFromIds(string id)
{
var VideoRequest = service.Videos.List("snippet,contentDetails");
Expand All @@ -111,12 +138,19 @@ private async Task RequestVideosFromIds(string id)
{
Video[] videoList = context.Videos.ToArray();

foreach(var item in VideoResponse.Items)
foreach (var item in VideoResponse.Items)
{
try
{
Video v = ParseVideo(item, 0, 0);

if (AppSettings.AutoDeleteShorts && v.Title.Contains("#shorts"))
{
Logger.Debug("Video \"" + v.Title + "\" was deleted because it is a short.");

continue;
}

context.Videos.Add(v);
}
catch (Exception e)
Expand Down Expand Up @@ -208,7 +242,7 @@ private List<string> CreateRequestList(List<string> videoIds)
if (requestId != "")
{
requests.Add(requestId);
}
}
}
}

Expand Down Expand Up @@ -263,7 +297,7 @@ public void UpdateVideoList()
}

Task.WaitAll(tasks);
}
}
catch (Exception e)
{
Logger.Error("at DataRetriever.UpdateVideoList()");
Expand All @@ -275,12 +309,13 @@ public void UpdateVideoList()
return;
}

foreach(Task<List<string>> list in tasks)
foreach (Task<List<string>> list in tasks)
{
videoIds.AddRange(list.Result);
}

if (videoIds.Count == 0) {
if (videoIds.Count == 0)
{
Logger.Info("Finished loading 0 new Videos");

return;
Expand All @@ -304,7 +339,7 @@ public void UpdateVideoList()
using (AppDbContext context = new AppDbContext())
{
Logger.Info("Finished loading " + (context.Videos.LongCount() - count) + " new Videos");
}
}
}

public void AddPlaylist(int number, string listId)
Expand All @@ -326,7 +361,7 @@ public void AddPlaylist(int number, string listId)

foreach (Video v in listOfVideos)
{
if ((v.List == number)&&(v.Index>=count))
if ((v.List == number) && (v.Index >= count))
{
count = v.Index + 1;
}
Expand Down Expand Up @@ -422,7 +457,7 @@ public void AddPlaylist(int number, string listId)
break;
}
else
{
{
request.PageToken = response.NextPageToken;
}
}
Expand All @@ -439,7 +474,7 @@ public void AddPlaylist(int number, string listId)

private Video ParseVideo(Google.Apis.YouTube.v3.Data.Video item, int number, int count)
{
DateTime time = DateTime.Parse(item.Snippet.PublishedAt,null,System.Globalization.DateTimeStyles.RoundtripKind);
DateTime time = (DateTime) item.Snippet.PublishedAt;//DateTime.Parse(item.Snippet.PublishedAt,null,System.Globalization.DateTimeStyles.RoundtripKind);

Video newVideo = new Video()
{
Expand Down Expand Up @@ -562,9 +597,9 @@ public void GarbageCollector()
{
var list = context.Videos;

foreach(Video v in list)
foreach (Video v in list)
{
if ((!v.New)&&(v.List==0))
if ((!v.New) && (v.List == 0))
{
if (v.PublishedAt.AddDays(LifeTime) < DateTime.Now)
{
Expand Down
2 changes: 0 additions & 2 deletions SubBox/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

services.AddDbContext<AppDbContext>();

services.AddControllers();
Expand Down
32 changes: 20 additions & 12 deletions SubBox/SubBox.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<ApplicationIcon>Logo.ico</ApplicationIcon>
</PropertyGroup>

Expand All @@ -10,28 +10,36 @@
</PropertyGroup>

<PropertyGroup>
<Version>1.8.1</Version>
<Version>1.9.0</Version>
</PropertyGroup>

<PropertyGroup>
<RuntimeIdentifier>win7-x64</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
<OutputType>Exe</OutputType>
<PublishSingleFile Condition="'$(Configuration)' == 'Release'">true</PublishSingleFile>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<Optimize>true</Optimize>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Content Remove="settings.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Google.Apis.YouTube.v3" Version="1.49.0.2120" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Google.Apis.YouTube.v3" Version="1.57.0.2756" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.7" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\channelPictures\" />
</ItemGroup>

<ItemGroup>
<None Include="settings.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading

0 comments on commit 6d9f862

Please sign in to comment.