Skip to content

Commit

Permalink
added cooldown on refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
MilchRatchet committed Nov 14, 2019
1 parent 335889f commit eddc08c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 0 additions & 2 deletions SubBox/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ public string[] GetSettings()
[HttpPost("refresh")]
public void Refresh()
{
Console.WriteLine("Refreshing...");

DataRetriever fetcher = new DataRetriever();

fetcher.UpdateVideoList();
Expand Down
12 changes: 8 additions & 4 deletions SubBox/Models/AppSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;

namespace SubBox.Models
{
Expand All @@ -10,12 +11,15 @@ public class AppSettings
public static int PlaylistPlaybackSize { get; set; }
public static string Color { get; set; }
public static bool NightMode { get; set; }
public static DateTime LastRefresh { get; set; }

//Runtime vars
public static bool GCMode { get; set; }

public static void Save()
public static async void Save()
{
string[] options = new string[] {RetrievalTimeFrame.ToString(), NewChannelTimeFrame.ToString(), DeletionTimeFrame.ToString(), PlaylistPlaybackSize.ToString(), Color, NightMode.ToString() };
File.WriteAllLines("settings.txt", options);
string[] options = new string[] {RetrievalTimeFrame.ToString(), NewChannelTimeFrame.ToString(), DeletionTimeFrame.ToString(), PlaylistPlaybackSize.ToString(), Color, NightMode.ToString(), LastRefresh.ToString("O") };
await File.WriteAllLinesAsync("settings.txt", options);
}
}
}
17 changes: 17 additions & 0 deletions SubBox/Models/DataRetriever.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class DataRetriever

private static int LifeTime = 7;

private readonly int RefreshLimit = 120;

public DataRetriever()
{
service = new YouTubeService(new BaseClientService.Initializer()
Expand Down Expand Up @@ -200,8 +202,23 @@ private List<string> CreateRequestList(List<string> videoIds)

public void UpdateVideoList()
{
int diffSec = (int) (DateTime.Now - AppSettings.LastRefresh).TotalSeconds;

if (diffSec < RefreshLimit)
{
Console.WriteLine("Can only refresh every " + RefreshLimit + " seconds! " + (RefreshLimit - diffSec) + " seconds remaining");

return;
}

AppSettings.LastRefresh = DateTime.Now;

AppSettings.Save();

GarbageCollector();

Console.WriteLine("Refreshing...");

LifeTime = AppSettings.RetrievalTimeFrame;

List<Channel> Channels;
Expand Down
2 changes: 2 additions & 0 deletions SubBox/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
AppSettings.NightMode = false;
}
AppSettings.LastRefresh = DateTime.ParseExact(options[6], "O", CultureInfo.InvariantCulture);
}
catch (Exception)
{
Expand All @@ -93,6 +94,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
AppSettings.PlaylistPlaybackSize = 50;
AppSettings.Color = "DB4437";
AppSettings.NightMode = false;
AppSettings.LastRefresh = BuildTime;
AppSettings.Save();
}

Expand Down

0 comments on commit eddc08c

Please sign in to comment.