This repository has been archived on 2023-04-02. You can view files and clone it, but cannot push or open issues or pull requests.
trainav/trainav.web/Pages/OEAPI.cshtml.cs

80 lines
2.9 KiB
C#
Raw Permalink Normal View History

2020-06-11 20:29:16 +02:00
using System;
using System.Linq;
using System.Net;
2021-06-27 22:38:20 +02:00
using System.Text.RegularExpressions;
2020-06-11 20:29:16 +02:00
using LinqToDB;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.RazorPages;
2022-04-29 14:59:24 +02:00
using trainav.web.database;
using trainav.web.database.Tables;
using trainav.web.JSON;
using trainav.web.Utils;
2022-04-29 14:59:24 +02:00
using Leg = trainav.web.database.Tables.Leg;
2020-06-11 20:29:16 +02:00
// ReSharper disable PossibleInvalidOperationException
2022-05-03 00:20:02 +02:00
namespace trainav.web.Pages;
2020-06-11 20:29:16 +02:00
2022-05-03 00:20:02 +02:00
public class OEAPIModel : PageModel {
public int TripId;
2022-05-03 00:20:02 +02:00
public void OnGet() {
var link = Request.Query["link"].ToString();
var shortcode = link;
var oepage = "oeffisear.ch";
if (link.Contains("oeffisear.ch")) {
shortcode = link.Split("/#/").Last();
}
else if (link.Contains("transit.ztn.sh")) {
shortcode = link.Split("/#/").Last();
oepage = "transit.ztn.sh";
}
var jid = shortcode.Split("/").Last();
shortcode = shortcode.Split("/").First();
2020-06-11 20:29:16 +02:00
2022-05-03 00:20:02 +02:00
using var db = new Database.DbConn();
var response = new WebClient().DownloadString($"https://{oepage}/journeys?{{\"reqId\":\"{shortcode}\"}}");
2020-06-11 20:29:16 +02:00
2022-05-03 00:20:02 +02:00
var parsed = OeapiResponse.FromJson(response);
2020-06-11 20:29:16 +02:00
var authorizedUser = db.Users.FirstOrDefault(p => p.Username == AuthUtil.GetRemoteUser(HttpContext, db));
2022-05-03 00:20:02 +02:00
if (!string.IsNullOrWhiteSpace(Request.Query["tripid"].ToString()))
if (db.Trips.First(p => p.TripId == int.Parse(Request.Query["tripid"].ToString())).UserId != authorizedUser!.UserId)
2022-05-03 00:20:02 +02:00
return;
2022-05-03 00:20:02 +02:00
var tripId = Request.Query["action"] == "addleg"
? int.Parse(Request.Query["tripid"])
: db.InsertWithInt32Identity(new Trip { UserId = authorizedUser!.UserId });
2020-06-11 20:29:16 +02:00
2022-05-03 00:20:02 +02:00
foreach (var journey in parsed.Data.Journeys[jid].Legs.Where(p => p.IsTransfer != true && p.IsWalking != true)) {
var arrtime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var deptime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
arrtime = arrtime.AddSeconds((long)journey.Arrival.PlannedTime).ToLocalTime();
deptime = deptime.AddSeconds((long)journey.Departure.PlannedTime).ToLocalTime();
var trainNo = int.Parse(journey.Line.TripNum);
if (trainNo == 0) {
var extractedTrainNo = Regex.Match(journey.Line.Name, @"\d+").Value;
if (extractedTrainNo.Length > 0)
trainNo = int.Parse(extractedTrainNo);
2020-06-11 20:29:16 +02:00
}
2022-05-03 00:20:02 +02:00
db.InsertWithInt32Identity(new Leg {
TripId = tripId,
UserId = authorizedUser!.UserId,
2022-05-03 00:20:02 +02:00
TrainType = journey.Line.ProductName,
TrainNr = trainNo,
ArrStation = journey.Arrival.Point.Stop.Name,
ArrStationId = int.Parse(journey.Arrival.Point.Stop.Id),
ArrTime = arrtime.ToString("yyyy-MM-ddTHH:mm:ss"),
DepStation = journey.Departure.Point.Stop.Name,
DepStationId = int.Parse(journey.Departure.Point.Stop.Id),
DepTime = deptime.ToString("yyyy-MM-ddTHH:mm:ss")
});
2020-06-11 20:29:16 +02:00
}
2022-05-03 00:20:02 +02:00
TripId = tripId;
2020-06-11 20:29:16 +02:00
}
}