parent
e7e71ff820
commit
bf18ded213
@ -0,0 +1,57 @@
|
||||
using Newtonsoft.Json;
|
||||
using System.Net;
|
||||
|
||||
namespace Fahrplan.Network;
|
||||
|
||||
/// <summary>
|
||||
/// Class to help get the plan from the CCC API
|
||||
/// </summary>
|
||||
public class PlanPuller () {
|
||||
public const string PlanJsonUrl = "https://fahrplan.events.ccc.de/congress/2024/fahrplan/schedule/export/schedule.json";
|
||||
public const string LocalPlanFilename = "ansi-fahrplan.json";
|
||||
|
||||
/// <summary>
|
||||
/// Pulls the current fahrplan, stores it in a temp
|
||||
/// directory and returns the parsed JSON object.
|
||||
/// If another plan already exists in the temp directory
|
||||
/// and is not older than three minutes, a new plan
|
||||
/// will not yet be pulled to save on web requests
|
||||
/// </summary>
|
||||
/// <param name="forcePull">If true, skips checking the timestamp on the plan cache</param>
|
||||
/// <returns>Parsed Fahrplan JSON object</returns>
|
||||
public async static Task<Plan.Root> GetFahrplanAsync (bool forcePull = false) {
|
||||
var localPlanFileFullpath =
|
||||
Path.GetTempPath () +
|
||||
LocalPlanFilename;
|
||||
|
||||
if (File.Exists (localPlanFileFullpath)) {
|
||||
var fileInfo = new FileInfo (localPlanFileFullpath);
|
||||
|
||||
// File is not older than three minutes, return cached file
|
||||
if (fileInfo.LastWriteTime + TimeSpan.FromMinutes (3) > DateTime.Now) {
|
||||
var reader = fileInfo.OpenText ();
|
||||
var content = await reader.ReadToEndAsync ();
|
||||
|
||||
return JsonConvert.DeserializeObject<Plan.Root> (content);
|
||||
}
|
||||
}
|
||||
|
||||
var httpClient = new HttpClient ();
|
||||
|
||||
// Be a good netizen, add a proper UA string
|
||||
httpClient.DefaultRequestHeaders.Add ("User-Agent", "AnsiFahrplan/1.0 (by Snep, c3@diskcat.com)");
|
||||
|
||||
var response = await httpClient.GetAsync (new Uri (PlanJsonUrl));
|
||||
|
||||
response.EnsureSuccessStatusCode ();
|
||||
var responseContent = await response.Content.ReadAsStringAsync ();
|
||||
|
||||
try {
|
||||
File.WriteAllText (localPlanFileFullpath, responseContent);
|
||||
} catch (Exception ex) {
|
||||
Console.Error.WriteLine ("Warning: Failed to write Plan cache file: " + ex.Message);
|
||||
}
|
||||
|
||||
return JsonConvert.DeserializeObject<Plan.Root> (responseContent);
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
- [] Ensure minimum width and height of terminal
|
||||
- [] Help text
|
||||
- [] Actually format the different views as text and display them
|
||||
- [] One class per view to store data in?
|
||||
- [] Key events per view, like switching days
|
||||
- [] Footer text
|
||||
- [] Colors
|
||||
- [] Cleanup
|
||||
|
||||
- [] Make a TCP service out of this somehow
|
Loading…
Reference in new issue