Skip to content

Commit

Permalink
add support for realtime feeds that require an API key for access
Browse files Browse the repository at this point in the history
  • Loading branch information
unreasonableman committed Aug 17, 2023
1 parent a910ca8 commit b90f381
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions gtfu/src/main/java/gtfu/MonitorPositionUpdates.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.ArrayList;
Expand All @@ -22,6 +24,8 @@
import com.google.transit.realtime.GtfsRealtime.Position;

public class MonitorPositionUpdates {
private static final String API_KEY_ARG = "-api-key";
private static final String API_KEY_NAME_ARG = "-api-key-name";
private static final String RAW_ARG = "-raw";
private static final String CSV_ARG = "-csvoutput";
private static final String ID_ARG = "-id";
Expand Down Expand Up @@ -196,6 +200,8 @@ private static String getIP(URL url) throws Exception {
public static void main(String[] arg) throws Exception {
String id = null;
String lookupFile = null;
String apiKeyName = "x-api-key";
String apiKey = null;
boolean raw = false;
boolean csvoutput = false;
boolean help = false;
Expand All @@ -208,6 +214,14 @@ public void run() {
});

for (int i=0; i<arg.length; i++) {
if (arg[i].equals(API_KEY_ARG) && i + 1 < arg.length) {
apiKey = arg[++i];
}

if (arg[i].equals(API_KEY_NAME_ARG) && i + 1 < arg.length) {
apiKeyName = arg[++i];
}

if (arg[i].equals(RAW_ARG)) {
raw = true;
}
Expand Down Expand Up @@ -268,19 +282,35 @@ public void run() {
Util.disableSSLChecking();
}

try (InputStream is = url.openStream()) {
FeedMessage msg = FeedMessage.parseFrom(is);
URLConnection conn = url.openConnection();

if (!csvoutput) {
dumpRawFeed(msg, id != null ? id : url.toString());
} else {
dumpCSV(msg);
try (AutoCloseable ac = () -> ((HttpURLConnection)conn).disconnect()) {
if (apiKey != null ) {
conn.setRequestProperty(apiKeyName, apiKey);
}

try (InputStream is = conn.getInputStream()) {
FeedMessage msg = FeedMessage.parseFrom(is);

if (!csvoutput) {
dumpRawFeed(msg, id != null ? id : url.toString());
} else {
dumpCSV(msg);
}
}
}
} else {
for (;;) {
try (InputStream is = url.openStream()) {
dumpFeed(FeedMessage.parseFrom(is), id);
URLConnection conn = url.openConnection();

try (AutoCloseable ac = () -> ((HttpURLConnection)conn).disconnect()) {
if (apiKey != null ) {
conn.setRequestProperty(apiKeyName, apiKey);
}

try (InputStream is = conn.getInputStream()) {
dumpFeed(FeedMessage.parseFrom(is), id);
}
}

Util.sleep(sleepMillis);
Expand Down

0 comments on commit b90f381

Please sign in to comment.