media.ccc.de proxy https://c3stream.de
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.

136 lines
4.6 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
3 months ago
4 months ago
3 months ago
4 months ago
3 months ago
4 months ago
3 months ago
4 months ago
4 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using c3stream.DataModels;
  7. using c3stream.Pages;
  8. using LinqToDB.Common;
  9. using LinqToDB.Data;
  10. using Microsoft.AspNetCore.Hosting;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.Extensions.Hosting;
  13. namespace c3stream {
  14. public static class c3stream {
  15. public const string DataPath = "data";
  16. public const string DbFile = "c3stream.sqlite";
  17. public const string CachePath = "/mnt/storage/archive/Video/congress/";
  18. public const string CacheUrl = "https://c3stream-mirror.zotan.services/";
  19. public static object Lock = new();
  20. public static string DbPath = Path.Combine(DataPath, DbFile);
  21. public static readonly List<ConferenceObject> Conferences = new() {
  22. new ConferenceObject("rc3", true),
  23. new ConferenceObject("36c3"),
  24. new ConferenceObject("camp2019"),
  25. new ConferenceObject("gpn19"),
  26. new ConferenceObject("35c3"),
  27. new ConferenceObject("34c3"),
  28. new ConferenceObject("33c3"),
  29. new ConferenceObject("32c3"),
  30. new ConferenceObject("31c3"),
  31. new ConferenceObject("30c3")
  32. };
  33. public static void Main(string[] args) {
  34. if (!Directory.Exists(DataPath))
  35. Directory.CreateDirectory(DataPath);
  36. if (!File.Exists(DbPath))
  37. File.Copy(Path.Combine(DataPath, "database.init.sqlite"), DbPath);
  38. DataConnection.DefaultSettings = new Database.Settings();
  39. foreach (var conference in Conferences)
  40. UpdateConference(conference);
  41. if (args.Length != 0) {
  42. if (args[0] == "logo")
  43. foreach (var conference in Conferences) {
  44. Console.WriteLine($"wget {conference.LogoUri} -O {Path.Combine(CachePath, conference.Acronym, "logo.png")}");
  45. }
  46. else if (Conferences.All(p => p.Acronym != args[0]))
  47. Console.WriteLine("No matching conference found.");
  48. else
  49. foreach (var talk in Conferences.First(p => p.Acronym == args[0]).Talks)
  50. Console.WriteLine($"youtube-dl -f \"best[ext = mp4]\" {talk.FrontendLink} -o \"{Path.Combine(CachePath, args[0], talk.Slug)}.mp4\"");
  51. }
  52. else {
  53. CreateHostBuilder(args).Build().Run();
  54. }
  55. }
  56. //TODO: move this to the database as well
  57. public static void UpdateConference(ConferenceObject conference) {
  58. using var wc = new WebClient();
  59. var jsonpath = Path.Combine(DataPath, conference.Acronym + "_index.json");
  60. var json = "";
  61. if (!File.Exists(jsonpath)) {
  62. json = wc.DownloadString($"https://api.media.ccc.de/public/conferences/{conference.Acronym}");
  63. File.WriteAllText(jsonpath, json);
  64. }
  65. else if (conference.Ongoing) {
  66. json = wc.DownloadString($"https://api.media.ccc.de/public/conferences/{conference.Acronym}");
  67. }
  68. else {
  69. json = File.ReadAllText(jsonpath);
  70. }
  71. var parsed = Conference.FromJson(json);
  72. lock (Lock) {
  73. conference.Talks.Clear();
  74. conference.LogoUri = parsed.LogoUrl.AbsoluteUri;
  75. conference.Talks.AddRange(parsed.Events);
  76. conference.Talks.ForEach(p => p.Guid = p.Guid.Trim());
  77. }
  78. }
  79. public static string UpdateCookie(HttpRequest request, HttpResponse response, string redirectUri) {
  80. var cookie = "";
  81. //if new bookmark is in uri
  82. if (request.Query.ContainsKey("bookmark") && Guid.TryParseExact(request.Query["bookmark"], "D", out _)) {
  83. response.Cookies.Append("bookmark", request.Query["bookmark"], new CookieOptions {Expires = DateTimeOffset.MaxValue});
  84. cookie = request.Query["bookmark"];
  85. }
  86. //if no cookie exists or cookie is invalid
  87. else if (!request.Cookies.ContainsKey("bookmark") || !Guid.TryParseExact(request.Cookies["bookmark"], "D", out _)) {
  88. var guid = Guid.NewGuid().ToString();
  89. response.Cookies.Append("bookmark", guid, new CookieOptions {Expires = DateTimeOffset.MaxValue});
  90. cookie = guid;
  91. }
  92. else {
  93. cookie = request.Cookies["bookmark"];
  94. }
  95. if (request.Query.ContainsKey("bookmark")) {
  96. response.Redirect(redirectUri);
  97. }
  98. return cookie;
  99. }
  100. public static Event GetEventByGuid(string guid) {
  101. return Conferences.SelectMany(c => c.Talks.Where(talk => talk.Guid.ToString() == guid)).FirstOrDefault();
  102. }
  103. public static ConferenceObject GetConferenceByEventGuid(string guid) {
  104. return Conferences.FirstOrDefault(c => c.Talks.Any(t => t.Guid.ToString() == guid));
  105. }
  106. public static IHostBuilder CreateHostBuilder(string[] args) =>
  107. Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
  108. public class ConferenceObject {
  109. public string Acronym;
  110. public bool Ongoing;
  111. public string LogoUri;
  112. public List<Event> Talks = new();
  113. public ConferenceObject(string acronym, bool ongoing = false) {
  114. Acronym = acronym;
  115. Ongoing = ongoing;
  116. }
  117. }
  118. }
  119. }