You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.0 KiB

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);
}
}