c3stream/c3stream.cs
2020-01-04 14:23:58 +01:00

95 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using c3stream.Pages;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
namespace c3stream {
public static class c3stream {
public const string DataPath = "data";
public const string DbFile = "c3stream.user.json";
public const string CachePath = "/mnt/storage/archive/Video/congress/";
public const string CacheUrl = "https://mirror.c3stream.de/";
public static object Lock = new object();
public static string DbPath = Path.Combine(DataPath, DbFile);
public static List<ConferenceObject> Conferences = new List<ConferenceObject> {
new ConferenceObject("36c3", 2, true),
new ConferenceObject("35c3", 1),
new ConferenceObject("34c3", 1),
new ConferenceObject("33c3"),
new ConferenceObject("32c3"),
};
public static void Main(string[] args) {
if (!Directory.Exists(DataPath))
Directory.CreateDirectory(DataPath);
if (!File.Exists(DbPath))
ConferenceModel.WriteUserData();
using var wc = new WebClient();
foreach (var conference in Conferences) {
var jsonpath = Path.Combine(DataPath, conference.Acronym + "_index.json");
var json = "";
if (conference.Ongoing || !File.Exists(jsonpath)) {
json = wc.DownloadString($"https://api.media.ccc.de/public/conferences/{conference.Acronym}");
File.WriteAllText(jsonpath, json);
}
else {
json = File.ReadAllText(jsonpath);
}
var parsed = Conference.FromJson(json);
conference.Talks.AddRange(parsed.Events);
}
CreateHostBuilder(args).Build().Run();
}
public static void UpdateCookie(HttpRequest resquest, HttpResponse response, string redirectUri) {
//if new bookmark is in uri
if (resquest.Query.ContainsKey("bookmark") && resquest.Cookies["bookmark"] != resquest.Query["bookmark"]) {
response.Cookies.Append("bookmark", resquest.Query["bookmark"], new CookieOptions {Expires = DateTimeOffset.MaxValue});
response.Redirect(redirectUri + "bookmark=" + resquest.Query["bookmark"]);
}
//if no cookie exists or cookie is invalid
else if (!resquest.Cookies.ContainsKey("bookmark") || !Guid.TryParseExact(resquest.Cookies["bookmark"], "D", out _)) {
var guid = Guid.NewGuid().ToString();
response.Cookies.Append("bookmark", guid, new CookieOptions {Expires = DateTimeOffset.MaxValue});
response.Redirect(redirectUri + "bookmark=" + guid);
}
//redir to cookie
else if (!resquest.Query.ContainsKey("bookmark")) {
response.Redirect(redirectUri + "bookmark=" + resquest.Cookies["bookmark"]);
}
}
public static Event GetEventByGuid(string guid) {
return Conferences.SelectMany(c => c.Talks.Where(talk => talk.Guid.ToString() == guid)).FirstOrDefault();
}
public static ConferenceObject GetConferenceByEventGuid(string guid) {
return Conferences.FirstOrDefault(c => c.Talks.Any(t => t.Guid.ToString() == guid));
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
public class ConferenceObject {
public string Acronym;
public bool Ongoing;
public int TagVersion;
public List<Event> Talks = new List<Event>();
public ConferenceObject(string acronym, int tagVersion = 0, bool ongoing = false) {
Acronym = acronym;
TagVersion = tagVersion;
Ongoing = ongoing;
}
}
}
}