Replace WebClient usage with HttpClient

This commit is contained in:
Laura Hausmann 2022-02-09 18:04:49 +01:00
parent b79bb982f6
commit 686cab15bf
Signed by: zotan
GPG key ID: D044E84C5BE01605
2 changed files with 113 additions and 116 deletions

View file

@ -4,113 +4,112 @@
@using global::c3stream.DataModels @using global::c3stream.DataModels
@using static ConferenceModel @using static ConferenceModel
@{ @{
if (c3stream.Conferences.All(c => c.Acronym != Request.Query["c"])) { if (c3stream.Conferences.All(c => c.Acronym != Request.Query["c"])) {
Response.Redirect("/"); Response.Redirect("/");
return; return;
} }
var cookie = c3stream.UpdateCookie(Request, Response, $"/Conference?c={Request.Query["c"]}"); var cookie = c3stream.UpdateCookie(Request, Response, $"/Conference?c={Request.Query["c"]}");
ViewData["Title"] = Request.Query["c"]; ViewData["Title"] = Request.Query["c"];
var wc = new WebClient(); var conference = c3stream.Conferences.First(c => c.Acronym == Request.Query["c"]);
var conference = c3stream.Conferences.First(c => c.Acronym == Request.Query["c"]); if (conference.Ongoing) {
if (conference.Ongoing) { c3stream.UpdateConference(conference);
c3stream.UpdateConference(conference); }
} await using var db = new Database.DbConn();
wc.Dispose(); var states = db.States.ToList();
await using var db = new Database.DbConn();
var states = db.States.ToList();
} }
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
<th scope="col">Event</th> <th scope="col">Event</th>
<th scope="col"> <th scope="col">
@Html.Raw(Request.Query["orderby"] == "published" ? $"<a href=\"/Conference?c={Request.Query["c"]}\">Published" : $"<a href=\"/Conference?c={Request.Query["c"]}&orderby=published\">Date")</th> @Html.Raw(Request.Query["orderby"] == "published" ? $"<a href=\"/Conference?c={Request.Query["c"]}\">Published" : $"<a href=\"/Conference?c={Request.Query["c"]}&orderby=published\">Date")
<th scope="col">Category</th> </th>
<th scope="col">Title</th> <th scope="col">Category</th>
<th scope="col">Speaker(s)</th> <th scope="col">Title</th>
<th scope="col">Lang</th> <th scope="col">Speaker(s)</th>
<th scope="col">Actions</th> <th scope="col">Lang</th>
</tr> <th scope="col">Actions</th>
</thead> </tr>
<tbody> </thead>
@foreach (var talk in Request.Query["orderby"] == "published" ? conference.Talks.OrderByDescending(p => p.ReleaseDate) : conference.Talks.OrderBy(p => p.Date)) { <tbody>
var state = states.FirstOrDefault(p => p.TalkId == talk.Guid && p.UserId == cookie)?.State; @foreach (var talk in Request.Query["orderby"] == "published" ? conference.Talks.OrderByDescending(p => p.ReleaseDate) : conference.Talks.OrderBy(p => p.Date)) {
var isWatched = state == "watched"; var state = states.FirstOrDefault(p => p.TalkId == talk.Guid && p.UserId == cookie)?.State;
var isMarked = state == "marked"; var isWatched = state == "watched";
var file = $"{talk.Slug}.mp4"; var isMarked = state == "marked";
var eventName = talk.Tags.Count <= 1 ? conference.Acronym : talk.Tags[0].Replace("-", "-<br/>"); var file = $"{talk.Slug}.mp4";
var category = talk.Tags.Count switch { var eventName = talk.Tags.Count <= 1 ? conference.Acronym : talk.Tags[0].Replace("-", "-<br/>");
0 => "<no category>", var category = talk.Tags.Count switch {
1 => talk.Tags[0], 0 => "<no category>",
2 => "<no category>", 1 => talk.Tags[0],
3 => talk.Tags[2], 2 => "<no category>",
4 => talk.Tags[3], 3 => talk.Tags[2],
5 => talk.Tags[3], 4 => talk.Tags[3],
6 => talk.Tags[3], // rc3: is this correct? 5 => talk.Tags[3],
_ => "<unknown tag format>" 6 => talk.Tags[3], // rc3: is this correct?
}; _ => "<unknown tag format>"
<tr> };
<td>@Html.Raw(eventName)</td> <tr>
<td>@(Request.Query["orderby"] == "published" ? talk.ReleaseDate?.Date.ToShortDateString() : talk.Date?.Date.ToShortDateString())</td> <td>@Html.Raw(eventName)</td>
<td>@category</td> <td>@(Request.Query["orderby"] == "published" ? talk.ReleaseDate?.Date.ToShortDateString() : talk.Date?.Date.ToShortDateString())</td>
@if (isWatched) { <td>@category</td>
<td style="color: #95cb7a">@talk.Title</td> @if (isWatched) {
} <td style="color: #95cb7a">@talk.Title</td>
else if (isMarked) { }
<td style="color: #da7d4f">@talk.Title</td> else if (isMarked) {
} <td style="color: #da7d4f">@talk.Title</td>
else { }
<td>@talk.Title</td> else {
} <td>@talk.Title</td>
<td>@(talk.Persons.Any() ? talk.Persons.Aggregate((s, s1) => $"{s}, {s1}") : "<no speakers>")</td> }
<td>@talk.OriginalLanguage</td> <td>@(talk.Persons.Any() ? talk.Persons.Aggregate((s, s1) => $"{s}, {s1}") : "<no speakers>")</td>
<td> <td>@talk.OriginalLanguage</td>
<div class="btn-group" role="group"> <td>
<a href="@talk.FrontendLink.AbsoluteUri" role="button" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Play"> <div class="btn-group" role="group">
<i class="fas fa-play-circle"></i> <a href="@talk.FrontendLink.AbsoluteUri" role="button" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Play">
</a> <i class="fas fa-play-circle"></i>
@if (System.IO.File.Exists(System.IO.Path.Combine(c3stream.CachePath, conference.Acronym, file))) { </a>
<a href="@(c3stream.CacheUrl + $"{conference.Acronym}/{file}")" role="button" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Mirror"> @if (System.IO.File.Exists(System.IO.Path.Combine(c3stream.CachePath, conference.Acronym, file))) {
<i class="fas fa-cloud-download"></i> <a href="@(c3stream.CacheUrl + $"{conference.Acronym}/{file}")" role="button" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Mirror">
</a> <i class="fas fa-cloud-download"></i>
} </a>
else { }
<a href="/" role="button" class="btn btn-primary btn-c3saction disabled"> else {
<i class="fas fa-cloud-download"></i> <a href="/" role="button" class="btn btn-primary btn-c3saction disabled">
</a> <i class="fas fa-cloud-download"></i>
} </a>
<a href="/Info?guid=@talk.Guid" role="button" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Info"> }
<i class="fas fa-info-circle"></i> <a href="/Info?guid=@talk.Guid" role="button" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Info">
</a> <i class="fas fa-info-circle"></i>
@if (isWatched) { </a>
<button onclick="SetState('@talk.Guid', 'unwatched')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Mark unwatched"> @if (isWatched) {
<i class="fas fa-times"></i> <button onclick="SetState('@talk.Guid', 'unwatched')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Mark unwatched">
</button> <i class="fas fa-times"></i>
<button class="btn btn-primary btn-c3saction disabled"> </button>
<i class="fas fa-clock"></i> <button class="btn btn-primary btn-c3saction disabled">
</button> <i class="fas fa-clock"></i>
} </button>
else if (isMarked) { }
<button onclick="SetState('@talk.Guid', 'watched')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Mark watched"> else if (isMarked) {
<i class="fas fa-check"></i> <button onclick="SetState('@talk.Guid', 'watched')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Mark watched">
</button> <i class="fas fa-check"></i>
<button onclick="SetState('@talk.Guid', 'unwatched')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Remove from watch later"> </button>
<i class="fas fa-undo-alt"></i> <button onclick="SetState('@talk.Guid', 'unwatched')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Remove from watch later">
</button> <i class="fas fa-undo-alt"></i>
} </button>
else { }
<button onclick="SetState('@talk.Guid', 'watched')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Mark watched"> else {
<i class="fas fa-check"></i> <button onclick="SetState('@talk.Guid', 'watched')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Mark watched">
</button> <i class="fas fa-check"></i>
<button onclick="SetState('@talk.Guid', 'marked')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Add to watch later"> </button>
<i class="fas fa-clock"></i> <button onclick="SetState('@talk.Guid', 'marked')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Add to watch later">
</button> <i class="fas fa-clock"></i>
} </button>
</div> }
</td> </div>
</tr> </td>
} </tr>
</tbody> }
</tbody>
</table> </table>

View file

@ -2,10 +2,8 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net.Http;
using c3stream.DataModels; using c3stream.DataModels;
using c3stream.Pages;
using LinqToDB.Common;
using LinqToDB.Data; using LinqToDB.Data;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
@ -39,8 +37,8 @@ namespace c3stream {
Directory.CreateDirectory(DataPath); Directory.CreateDirectory(DataPath);
if (!File.Exists(DbPath)) if (!File.Exists(DbPath))
File.Copy(Path.Combine(DataPath, "database.init.sqlite"), DbPath); File.Copy(Path.Combine(DataPath, "database.init.sqlite"), DbPath);
DataConnection.DefaultSettings = new Database.Settings(); DataConnection.DefaultSettings = new Database.Settings();
foreach (var conference in Conferences) foreach (var conference in Conferences)
UpdateConference(conference); UpdateConference(conference);
@ -63,16 +61,16 @@ namespace c3stream {
//TODO: move this to the database as well //TODO: move this to the database as well
public static void UpdateConference(ConferenceObject conference) { public static void UpdateConference(ConferenceObject conference) {
using var wc = new WebClient(); using var httpc = new HttpClient();
var jsonpath = Path.Combine(DataPath, conference.Acronym + "_index.json"); var jsonpath = Path.Combine(DataPath, conference.Acronym + "_index.json");
var json = ""; var json = "";
if (!File.Exists(jsonpath)) { if (!File.Exists(jsonpath)) {
json = wc.DownloadString($"https://api.media.ccc.de/public/conferences/{conference.Acronym}"); json = httpc.GetStringAsync($"https://api.media.ccc.de/public/conferences/{conference.Acronym}").Result;
File.WriteAllText(jsonpath, json); File.WriteAllText(jsonpath, json);
} }
else if (conference.Ongoing) { else if (conference.Ongoing) {
json = wc.DownloadString($"https://api.media.ccc.de/public/conferences/{conference.Acronym}"); json = httpc.GetStringAsync($"https://api.media.ccc.de/public/conferences/{conference.Acronym}").Result;
} }
else { else {
json = File.ReadAllText(jsonpath); json = File.ReadAllText(jsonpath);
@ -91,13 +89,13 @@ namespace c3stream {
var cookie = ""; var cookie = "";
//if new bookmark is in uri //if new bookmark is in uri
if (request.Query.ContainsKey("bookmark") && Guid.TryParseExact(request.Query["bookmark"], "D", out _)) { if (request.Query.ContainsKey("bookmark") && Guid.TryParseExact(request.Query["bookmark"], "D", out _)) {
response.Cookies.Append("bookmark", request.Query["bookmark"], new CookieOptions {Expires = DateTimeOffset.MaxValue}); response.Cookies.Append("bookmark", request.Query["bookmark"], new CookieOptions { Expires = DateTimeOffset.MaxValue });
cookie = request.Query["bookmark"]; cookie = request.Query["bookmark"];
} }
//if no cookie exists or cookie is invalid //if no cookie exists or cookie is invalid
else if (!request.Cookies.ContainsKey("bookmark") || !Guid.TryParseExact(request.Cookies["bookmark"], "D", out _)) { else if (!request.Cookies.ContainsKey("bookmark") || !Guid.TryParseExact(request.Cookies["bookmark"], "D", out _)) {
var guid = Guid.NewGuid().ToString(); var guid = Guid.NewGuid().ToString();
response.Cookies.Append("bookmark", guid, new CookieOptions {Expires = DateTimeOffset.MaxValue}); response.Cookies.Append("bookmark", guid, new CookieOptions { Expires = DateTimeOffset.MaxValue });
cookie = guid; cookie = guid;
} }
else { else {
@ -134,4 +132,4 @@ namespace c3stream {
} }
} }
} }
} }