add ds100 ics export
This commit is contained in:
parent
c1fe9d327f
commit
3caf1113fa
8
bahnplan.web/Pages/GenIcs.cshtml
Normal file
8
bahnplan.web/Pages/GenIcs.cshtml
Normal 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)
|
97
bahnplan.web/Pages/GenIcs.cshtml.cs
Normal file
97
bahnplan.web/Pages/GenIcs.cshtml.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,10 +1,8 @@
|
||||||
@page
|
@page
|
||||||
@using Microsoft.AspNetCore.Http
|
|
||||||
@using Microsoft.AspNetCore.Http.Extensions
|
|
||||||
@model SharedTripModel
|
@model SharedTripModel
|
||||||
@{
|
@{
|
||||||
ViewData["Title"] = "Shared Trip";
|
ViewData["Title"] = "Shared Trip";
|
||||||
|
|
||||||
if (Model.RedirToIndex) {
|
if (Model.RedirToIndex) {
|
||||||
Response.Redirect("/");
|
Response.Redirect("/");
|
||||||
return;
|
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-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>
|
<a class="btn btn-sm btn-warning" href="https://travelynx.de/s/@leg.DepStationId?train=@leg.TrainType @leg.TrainNr" target="_blank">Travelynx</a>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
}
|
}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
|
@ -6,9 +6,9 @@ using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
|
|
||||||
namespace bahnplan.web.Pages {
|
namespace bahnplan.web.Pages {
|
||||||
public class SharedTripModel : PageModel {
|
public class SharedTripModel : PageModel {
|
||||||
public new string User;
|
|
||||||
public List<Leg> Legs;
|
public List<Leg> Legs;
|
||||||
public bool RedirToIndex;
|
public bool RedirToIndex;
|
||||||
|
public new string User;
|
||||||
|
|
||||||
public void OnGet() {
|
public void OnGet() {
|
||||||
using var db = new Database.DbConn();
|
using var db = new Database.DbConn();
|
||||||
|
|
|
@ -144,6 +144,7 @@
|
||||||
|
|
||||||
@if (!Request.Query.ContainsKey("edit")) {
|
@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-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>
|
<a class="btn btn-sm btn-secondary" href="/Trip?id=@Request.Query["id"]&edit=true">Edit Trip</a>
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -8,9 +8,9 @@ using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
|
|
||||||
namespace bahnplan.web.Pages {
|
namespace bahnplan.web.Pages {
|
||||||
public class TripModel : PageModel {
|
public class TripModel : PageModel {
|
||||||
public new string User;
|
|
||||||
public List<Leg> Legs;
|
public List<Leg> Legs;
|
||||||
public bool RedirToIndex;
|
public bool RedirToIndex;
|
||||||
|
public new string User;
|
||||||
|
|
||||||
public void OnGet() {
|
public void OnGet() {
|
||||||
if (HttpContext.Session.GetString("authorized") != "true")
|
if (HttpContext.Session.GetString("authorized") != "true")
|
||||||
|
|
|
@ -68,10 +68,13 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<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="linq2db" Version="2.9.8" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.4" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.4" />
|
||||||
<PackageReference Include="Microsoft.Data.Sqlite" Version="3.1.4" />
|
<PackageReference Include="Microsoft.Data.Sqlite" Version="3.1.4" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||||
|
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.7.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
5394
bahnplan.web/ds100.csv
Normal file
5394
bahnplan.web/ds100.csv
Normal file
File diff suppressed because it is too large
Load diff
Reference in a new issue