From 8f4d9181169529c5a8d4459c1870c21430f4d881 Mon Sep 17 00:00:00 2001 From: Andrew Tang Date: Wed, 20 Dec 2017 15:09:01 +1000 Subject: [PATCH] Bug fixes - Fix encoding issue in test script - Check downloaded file is a zip file before proceeding --- scripts/test.py | 7 ++++--- src/main/java/Loader.java | 2 ++ src/main/java/Main.java | 18 ++++++++++++++---- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/scripts/test.py b/scripts/test.py index 04905d5..72c75d1 100644 --- a/scripts/test.py +++ b/scripts/test.py @@ -23,12 +23,13 @@ def main(): jobs = [] for feed in feeds: if 't' in feed and 'u' in feed and 'd' in feed['u']: - job = {'title': feed['t'],'url': feed['u']['d']} + job = {'title': feed['t'].encode('utf8'),'url': feed['u']['d']} jobs.append(job) for job in jobs: - title = "'" + job['title'] + "'".encode('utf8') - url = job['url'].encode('utf8') + title = "'" + job['title'].decode('utf-8') + "'" + title = title.encode('utf-8') + url = job['url'].encode('utf-8') print(title) print(url) os.system('mkdir ' + title) diff --git a/src/main/java/Loader.java b/src/main/java/Loader.java index 2dfe15b..c8e29a0 100644 --- a/src/main/java/Loader.java +++ b/src/main/java/Loader.java @@ -1,3 +1,4 @@ +import javafx.application.Application; import org.json.JSONException; import org.json.JSONObject; @@ -75,6 +76,7 @@ private List getFiles(File gtfsPath) { try { ZipFile zipFile = new ZipFile(gtfsPath); + File gtfsFolder = new File(gtfsPath.getParent() + System.getProperty("file.separator") + "GTFS"); gtfsFolder.mkdir(); diff --git a/src/main/java/Main.java b/src/main/java/Main.java index af1e8c2..abe9ae7 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,7 @@ import org.apache.commons.cli.*; import java.io.File; +import java.io.RandomAccessFile; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -17,8 +18,7 @@ public class Main { public static final String URL_OPTION = "u"; public static final String DATABASE_OPTION = "d"; public static final String CMD_NAME = "gtsql"; - public static final String FOOTER = "\nThis tool is used to generate an SQLite database from a GTFS feed. " + - "\nPlease report issues at https://github.com/aytee17/gtfs-to-sqlite"; + public static final String FOOTER = "\nPlease report issues at https://github.com/aytee17/gtfs-to-sqlite"; public static void main(String[] args) { try { @@ -84,10 +84,20 @@ public static void main(String[] args) { if (line.hasOption(URL_OPTION)) { String gtfsURL = line.getOptionValue(URL_OPTION); print("Downloading GTFS feed from: " + gtfsURL); + String zipPath = gtfsPath + System.getProperty("file.separator") + "GTFS.zip"; gtfsFile = IO.getFileFromURL( - gtfsPath + System.getProperty("file.separator") + "GTFS.zip", + zipPath, gtfsURL, true); + + // Check if the download is a zip file + RandomAccessFile raf = new RandomAccessFile(zipPath, "r"); + long magicNumber = raf.readInt(); + raf.close(); + if (magicNumber != 0x504B0304) { + throw new Exception("\nDownload is not a zip file."); + } + print("\nFeed downloaded."); } else { gtfsFile = new File(gtfsPath); @@ -101,7 +111,7 @@ public static void main(String[] args) { print(exception.getMessage() + "\n"); new HelpFormatter().printHelp(CMD_NAME, "", options, FOOTER, true); } - } catch (Exception e) { + } catch (Exception e) { e.printStackTrace(); print(e.getMessage()); print(FOOTER);