Skip to content
This repository has been archived by the owner on Jan 15, 2023. It is now read-only.

Commit

Permalink
fix null alerts, mock alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
Katsute committed Dec 1, 2022
1 parent a9e1dac commit 5fb5ec0
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.katsute</groupId>
<artifactId>mta-information-site</artifactId>
<version>3.30.0</version>
<version>3.32.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
4 changes: 2 additions & 2 deletions site/live.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<b>Developer Tools:</b>

[<a href="/">Home</a>]
[<a href="?type=bus&route=M1&direction=1">M1 Bus</a> | <a href="?type=bus&route=M1&direction=1&lang=ja">translated</a>]
[<a href="?type=subway&route=6&direction=1">6 Subway</a> | <a href="?type=subway&route=6&direction=1&lang=ja">translated</a>]
[<a href="?type=bus&route=M1&direction=1">M1 Bus</a> | <a href="?type=bus&route=M1&direction=1&lang=ja">translated</a> | <a href="?type=bus&route=M1&direction=1&lang=ja&mock">mock alerts</a>]
[<a href="?type=subway&route=6&direction=1">6 Subway</a> | <a href="?type=subway&route=6&direction=1&lang=ja">translated</a> | <a href="?type=subway&route=6&direction=1&lang=ja&mock">mock alerts</a>]

<i>Open developer tools for more information</i>

Expand Down
4 changes: 4 additions & 0 deletions site/mta.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ const getSubwayByID = async (id, lang) => {
* @returns object response
*/
const get = async (path, params) => {
const mock = Object.fromEntries(new URLSearchParams(window.location.search).entries()).mock;

if(mock !== undefined) params.mock = "true";

return new Promise((res, rej) => {
const xhr = new XMLHttpRequest();
const query = !params ? "" : '?' + Object.keys(params).map(key => `${key}=${encodeURIComponent(params[key])}`).join('&');
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/katsute/mis/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class Main {

private static final Map<String,String> resources = new HashMap<String,String>(){{
put("google_transit_subway" , "http://web.mta.info/developers/data/nyct/subway/google_transit.zip");
put("google_transit_bronx" , "http://web.mta.info/developers/data/nyct/bus/google_transit_bronx.zip");
put("google_transit_bronx" , "http://web.mta.info/developers/data/nyct/bus/google_transit_bronx.zip");
put("google_transit_brooklyn" , "http://web.mta.info/developers/data/nyct/bus/google_transit_brooklyn.zip");
put("google_transit_manhattan" , "http://web.mta.info/developers/data/nyct/bus/google_transit_manhattan.zip");
put("google_transit_queens" , "http://web.mta.info/developers/data/nyct/bus/google_transit_queens.zip");
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/dev/katsute/mis/Mock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (C) 2022 Katsute <https://github.com/Katsute>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

package dev.katsute.mis;

import java.util.Random;

abstract class Mock {

private static Random rd = new Random();

public static boolean mock(double chance){
return rd.nextDouble() <= chance;
}

public static JsonBuilder mockSubwayDelay(final String line, final String station, final String lang){
final String d = rd.nextBoolean() ? "NYPD" : "FDNY";
return mockAlert(
line + " trains are running with delays in both directions while the " + d + " finishes an investigation at " + station,
lang
);
}

public static JsonBuilder mockSubwayWeather(final String line, final String lang){
return mockAlert(
line + " trains are running with extensive delays in both directions due to inclement weather",
lang
);
}

public static JsonBuilder mockBusDelay(final String line, final String stop, final String lang){
return mockAlert(
line + " buses are running with delays in both directions due to construction at " + stop,
lang
);
}

public static JsonBuilder mockBusWeather(final String line, final String lang){
return mockAlert(
line + " buses are running with delays in both directions due to inclement weather",
lang
);
}

private static JsonBuilder mockAlert(final String desc, final String lang){
return new JsonBuilder()
.set("header", desc)
.set("header_translated", RequestHandler.translate(desc.trim(), "en", lang))
.set("description", desc.trim())
.set("description_translated", RequestHandler.translate(desc.trim(), "en", lang))
.set("type", "Delays")
.set("effect", "UNKNOWN_EFFECT")
.set("slow", desc.contains("slow") || desc.contains("delay"))
.set("skip", desc.startsWith("no") || desc.contains("skips") || desc.contains("skipped"))
.set("construction", desc.contains("construction"))
.set("weather", desc.contains("weather"))
.set("police", desc.contains("police") || desc.contains("nypd"))
.set("fire", desc.contains("fire") || desc.contains("fdny"))
.set("ems", desc.contains("ems"));
}

}
40 changes: 27 additions & 13 deletions src/main/java/dev/katsute/mis/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public final void handle(final SimpleHttpExchange exchange){
}
}

final boolean mock = query.containsKey("mock");

final String lang = query.getOrDefault("lang", "en");

final String id = query.get("id");
Expand Down Expand Up @@ -166,12 +168,12 @@ public final void handle(final SimpleHttpExchange exchange){
for(final Bus.Alert alert : alerts){
for(final TransitAlertPeriod per : alert.getActivePeriods()){
if(per.getStartEpochMillis() != null && per.getStartEpochMillis() <= NOW && per.getEndEpochMillis() != null && per.getEndEpochMillis() >= NOW){
final String desc = alert.getDescription();
final String desc = alert.getDescription() != null ? alert.getDescription().toLowerCase() : "";
a.add(new JsonBuilder()
.set("header", alert.getHeader())
.set("header_translated", translate(alert.getHeader().trim(), "en", lang))
.set("description", alert.getDescription().trim())
.set("description_translated", translate(alert.getDescription().trim(), "en", lang))
.set("description", desc.trim())
.set("description_translated", translate(desc.trim(), "en", lang))
.set("type", alert.getAlertType())
.set("effect", alert.getEffect())
.set("slow", desc.contains("slow") || desc.contains("delay"))
Expand All @@ -187,6 +189,9 @@ public final void handle(final SimpleHttpExchange exchange){
}
}

if(mock && Mock.mock(.2))
a.add(Mock.mockBusDelay(r.getRouteShortName(), s.getStopName(), lang));

stops.add(new JsonBuilder()
.set("id", s.getStopID())
.set("name", s.getStopName())
Expand All @@ -201,12 +206,12 @@ public final void handle(final SimpleHttpExchange exchange){
for(final Bus.Alert alert : r.getAlerts()){
for(final TransitAlertPeriod per : alert.getActivePeriods()){
if(per.getStartEpochMillis() != null && per.getStartEpochMillis() <= NOW && per.getEndEpochMillis() != null && per.getEndEpochMillis() >= NOW){
final String desc = alert.getDescription();
final String desc = alert.getDescription() != null ? alert.getDescription().toLowerCase() : "";
a.add(new JsonBuilder()
.set("header", alert.getHeader())
.set("header_translated", translate(alert.getHeader().trim(), "en", lang))
.set("description", alert.getDescription().trim())
.set("description_translated", translate(alert.getDescription().trim(), "en", lang))
.set("description", desc.trim())
.set("description_translated", translate(desc.trim(), "en", lang))
.set("type", alert.getAlertType())
.set("effect", alert.getEffect())
.set("slow", desc.contains("slow") || desc.contains("delay"))
Expand All @@ -222,6 +227,9 @@ public final void handle(final SimpleHttpExchange exchange){
}
}

if(mock && Mock.mock(.4))
a.add(Mock.mockBusWeather(r.getRouteShortName(), lang));

final JsonBuilder json = new JsonBuilder()
.set("vehicle", new JsonBuilder()
.set("id", bus.getVehicleID())
Expand Down Expand Up @@ -295,12 +303,12 @@ public final void handle(final SimpleHttpExchange exchange){
for(final Subway.Alert alert : alerts){
for(final TransitAlertPeriod per : alert.getActivePeriods()){
if(per.getStartEpochMillis() != null && per.getStartEpochMillis() <= NOW && per.getEndEpochMillis() != null && per.getEndEpochMillis() >= NOW){
final String desc = alert.getDescription().toLowerCase();
final String desc = alert.getDescription() != null ? alert.getDescription().toLowerCase() : "";
a.add(new JsonBuilder()
.set("header", alert.getHeader())
.set("header_translated", translate(alert.getHeader().trim(), "en", lang))
.set("description", alert.getDescription().trim())
.set("description_translated", translate(alert.getDescription().trim(), "en", lang))
.set("description", desc.trim())
.set("description_translated", translate(desc.trim(), "en", lang))
.set("type", alert.getAlertType())
.set("effect", alert.getEffect())
.set("slow", desc.contains("slow") || desc.contains("delay"))
Expand All @@ -319,6 +327,9 @@ public final void handle(final SimpleHttpExchange exchange){
}
}

if(mock && Mock.mock(.2))
a.add(Mock.mockSubwayDelay(r.getRouteShortName(), s.getStopName(), lang));

stops.add(new JsonBuilder()
.set("id", s.getStopID())
.set("name", s.getStopName())
Expand All @@ -333,12 +344,12 @@ public final void handle(final SimpleHttpExchange exchange){
for(final Subway.Alert alert : r.getAlerts()){
for(final TransitAlertPeriod per : alert.getActivePeriods()){
if(per.getStartEpochMillis() != null && per.getStartEpochMillis() <= NOW && per.getEndEpochMillis() != null && per.getEndEpochMillis() >= NOW){
final String desc = alert.getDescription().toLowerCase();
final String desc = alert.getDescription() != null ? alert.getDescription().toLowerCase() : "";
a.add(new JsonBuilder()
.set("header", alert.getHeader())
.set("header_translated", translate(alert.getHeader().trim(), "en", lang))
.set("description", alert.getDescription().trim())
.set("description_translated", translate(alert.getDescription().trim(), "en", lang))
.set("description", desc.trim())
.set("description_translated", translate(desc.trim(), "en", lang))
.set("type", alert.getAlertType())
.set("effect", alert.getEffect())
.set("slow", desc.contains("slow") || desc.contains("delay"))
Expand All @@ -355,6 +366,9 @@ public final void handle(final SimpleHttpExchange exchange){
break OUTER;
}
}

if(mock && Mock.mock(.4))
a.add(Mock.mockSubwayWeather(r.getRouteShortName(), lang));
}

final JsonBuilder json = new JsonBuilder()
Expand Down Expand Up @@ -407,7 +421,7 @@ private static double distance(final double long1, final double lat1, final doub

private static final Pattern trans = Pattern.compile("(?<=\"trans\": ?\").*?(?=\",)");

private static String translate(final String q, final String from, final String to){
static String translate(final String q, final String from, final String to){
if(from.equalsIgnoreCase(to))
return q;

Expand Down

0 comments on commit 5fb5ec0

Please sign in to comment.