add ds100 ics export

This commit is contained in:
Laura Hausmann 2020-10-19 01:36:44 +02:00
parent c1fe9d327f
commit 3caf1113fa
Signed by: zotan
GPG key ID: 5EC1D38FFC321311
8 changed files with 5507 additions and 6 deletions

View file

@ -0,0 +1,8 @@
@page
@model bahnplan.web.Pages.GenIcs
@{
Layout = null;
Response.ContentType = "text/calendar";
Response.Headers.Add("content-disposition", $@"attachment;filename=""trip-{Model.Legs.First().DepTime.Split("T")[0]}.ics""");
}
@Html.Raw(Model.IcsOutput)

View file

@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using bahnplan.web.database;
using bahnplan.web.database.Tables;
using CsvHelper;
using CsvHelper.Configuration.Attributes;
using Ical.Net.CalendarComponents;
using Ical.Net.DataTypes;
using Ical.Net.Serialization;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Calendar = Ical.Net.Calendar;
namespace bahnplan.web.Pages {
public class GenIcs : PageModel {
public string IcsOutput;
public List<Leg> Legs;
public new string User;
public void OnGet() {
using var db = new Database.DbConn();
if (!db.Legs.Any(p => p.TripId == int.Parse(Request.Query["id"]))) {
IcsOutput = "";
return;
}
Legs = db.Legs.Where(p => p.TripId == int.Parse(Request.Query["id"])).OrderBy(p => p.DepTime).ToList();
User = db.Users.First(p => p.UserId == Legs.First().UserId).Username;
if (Request.Query["user"] != User) {
IcsOutput = "";
return;
}
List<Ds100object> ds100Mapping;
using (var reader = new StreamReader("ds100.csv"))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) {
csv.Configuration.Delimiter = ";";
csv.Configuration.Encoding = Encoding.UTF8;
csv.Configuration.PrepareHeaderForMatch = (header, index) => header.ToLower();
ds100Mapping = csv.GetRecords<Ds100object>().ToList();
}
var calendar = new Calendar();
foreach (var leg in Legs.Where(p => p.TrainType != "placeholder")) {
var depst = ds100Mapping.Where(p => p.Station == leg.DepStation)
.DefaultIfEmpty(new Ds100object {Station = leg.DepStation, Ds100 = leg.DepStation})
.First()
.Ds100;
var arrst = ds100Mapping.Where(p => p.Station == leg.ArrStation)
.DefaultIfEmpty(new Ds100object {Station = leg.ArrStation, Ds100 = leg.ArrStation})
.First()
.Ds100;
var title = $"{depst} -> {arrst} ({leg.TrainType} {leg.TrainNr})";
var e = new CalendarEvent {
Summary = title, Start = new CalDateTime(DateTime.Parse(leg.DepTime)), End = new CalDateTime(DateTime.Parse(leg.ArrTime))
};
if (!string.IsNullOrWhiteSpace(leg.Comment))
e.Description = leg.Comment;
calendar.Events.Add(e);
}
var serializer = new CalendarSerializer();
IcsOutput = serializer.SerializeToString(calendar);
}
private class Ds100object {
[Name("Bundesland")] public string Bundesland { get; set; }
[Name("RB")] public string Regionalbereich { get; set; }
[Name("BM")] public string Bahnhofsmanagement { get; set; }
[Name("Bf. Nr.")] public string BfNr { get; set; }
[Name("Station")] public string Station { get; set; }
[Name("Bf DS 100Abk.")] public string Ds100 { get; set; }
[Name("Kat. Vst")] public string Kategorie { get; set; }
[Name("Straße")] public string Strasse { get; set; }
[Name("PLZ")] public string Plz { get; set; }
[Name("Ort")] public string Ort { get; set; }
[Name("Aufgabenträger")] public string Verkehrsverbund { get; set; }
}
}
}

View file

@ -1,10 +1,8 @@
@page
@using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Http.Extensions
@model SharedTripModel
@{
ViewData["Title"] = "Shared Trip";
if (Model.RedirToIndex) {
Response.Redirect("/");
return;
@ -72,7 +70,7 @@
<a class="btn btn-sm btn-primary" href="https://marudor.de/details/@leg.TrainType @leg.TrainNr/@(deptime.ToUniversalTime().Subtract(new DateTime(1970, 1, 1)).TotalSeconds)000/?station=@leg.DepStationId" target="_blank">Marudor</a>
<a class="btn btn-sm btn-warning" href="https://travelynx.de/s/@leg.DepStationId?train=@leg.TrainType @leg.TrainNr" target="_blank">Travelynx</a>
</td>
</tr>
</tr>
}
</tbody>
</table>

View file

@ -6,9 +6,9 @@ using Microsoft.AspNetCore.Mvc.RazorPages;
namespace bahnplan.web.Pages {
public class SharedTripModel : PageModel {
public new string User;
public List<Leg> Legs;
public bool RedirToIndex;
public new string User;
public void OnGet() {
using var db = new Database.DbConn();

View file

@ -144,6 +144,7 @@
@if (!Request.Query.ContainsKey("edit")) {
<a class="btn btn-sm btn-success" href="/SharedTrip?id=@Request.Query["id"]&user=@Model.User">Share Trip</a>
<a class="btn btn-sm btn-primary" href="/GenIcs?id=@Request.Query["id"]&user=@Model.User">Download ICS</a>
<a class="btn btn-sm btn-secondary" href="/Trip?id=@Request.Query["id"]&edit=true">Edit Trip</a>
}
else {

View file

@ -8,9 +8,9 @@ using Microsoft.AspNetCore.Mvc.RazorPages;
namespace bahnplan.web.Pages {
public class TripModel : PageModel {
public new string User;
public List<Leg> Legs;
public bool RedirToIndex;
public new string User;
public void OnGet() {
if (HttpContext.Session.GetString("authorized") != "true")

View file

@ -68,10 +68,13 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="CsvHelper" Version="15.0.8" />
<PackageReference Include="Ical.Net" Version="4.1.11" />
<PackageReference Include="linq2db" Version="2.9.8" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.4" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="3.1.4" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.7.0" />
</ItemGroup>
</Project>

5394
bahnplan.web/ds100.csv Normal file

File diff suppressed because it is too large Load diff