]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/function/gpsies/GetGpsiesFunction.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / function / gpsies / GetGpsiesFunction.java
index 804c4ce12e8d897369c47037b5b85c0951fff186..e60ed7429c4578e2bbfb44f8e3902dd69e102d63 100644 (file)
@@ -3,12 +3,14 @@ package tim.prune.function.gpsies;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.net.URLConnection;
 import java.util.ArrayList;
 
 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
 
 import tim.prune.App;
+import tim.prune.GpsPrune;
 import tim.prune.I18nManager;
 import tim.prune.load.xml.XmlFileLoader;
 import tim.prune.load.xml.ZipFileLoader;
@@ -23,6 +25,8 @@ public class GetGpsiesFunction extends GenericDownloaderFunction
        private static final int RESULTS_PER_PAGE = 20;
        /** Maximum number of results to get */
        private static final int MAX_RESULTS = 60;
+       /** New API key (specific to this program) */
+       private static final String GPSIES_API_KEY = "oumgvvbckiwpvsnb";
 
 
        /**
@@ -59,7 +63,6 @@ public class GetGpsiesFunction extends GenericDownloaderFunction
                _statusLabel.setText(I18nManager.getText("confirm.running"));
                // Act on callback to update list and send another request if necessary
                double[] coords = _app.getViewport().getBounds();
-               // Example http://www.gpsies.com/api.do?BBOX=10,51,12,53&limit=20&trackTypes=jogging&filetype=kml&device=Run.GPS
                int currPage = 1;
 
                ArrayList<GpsiesTrack> trackList = null;
@@ -69,16 +72,20 @@ public class GetGpsiesFunction extends GenericDownloaderFunction
                // Loop for each page of the results
                do
                {
-                       String urlString = "http://www.gpsies.com/api.do?BBOX=" +
+                       // Example http://ws.gpsies.com/api.do?BBOX=10,51,12,53&limit=20&resultPage=1&key=oumgvvbckiwpvsnb
+                       String urlString = "http://ws.gpsies.com/api.do?BBOX=" +
                                coords[1] + "," + coords[0] + "," + coords[3] + "," + coords[2] +
-                               "&limit=" + RESULTS_PER_PAGE + "&resultPage=" + currPage;
+                               "&limit=" + RESULTS_PER_PAGE + "&resultPage=" + currPage +
+                               "&key=" + GPSIES_API_KEY;
                        // Parse the returned XML with a special handler
                        GpsiesXmlHandler xmlHandler = new GpsiesXmlHandler();
                        try
                        {
                                url = new URL(urlString);
                                SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
-                               inStream = url.openStream();
+                               URLConnection conn = url.openConnection();
+                               conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
+                               inStream = conn.getInputStream();
                                saxParser.parse(inStream, xmlHandler);
                        }
                        catch (Exception e) {
@@ -105,23 +112,30 @@ public class GetGpsiesFunction extends GenericDownloaderFunction
        }
 
        /**
-        * Load the selected track or point
+        * Load the selected track(s)
         */
        protected void loadSelected()
        {
-               // Find the row selected in the table and get the corresponding track
-               int rowNum = _trackTable.getSelectedRow();
-               if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
+               // Find the row(s) selected in the table and get the corresponding track
+               int numSelected = _trackTable.getSelectedRowCount();
+               if (numSelected < 1) return;
+               int[] rowNums = _trackTable.getSelectedRows();
+               for (int i=0; i<numSelected; i++)
                {
-                       String url = _trackListModel.getTrack(rowNum).getDownloadLink();
-                       XmlFileLoader xmlLoader = new XmlFileLoader(_app);
-                       ZipFileLoader loader = new ZipFileLoader(_app, xmlLoader);
-                       try
+                       int rowNum = rowNums[i];
+                       if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
                        {
-                               loader.openStream(new URL(url).openStream());
-                       }
-                       catch (IOException ioe) {
-                               System.err.println("IO Exception : " + ioe.getMessage());
+                               String url = _trackListModel.getTrack(rowNum).getDownloadLink();
+                               XmlFileLoader xmlLoader = new XmlFileLoader(_app);
+                               ZipFileLoader loader = new ZipFileLoader(_app, xmlLoader);
+                               if (i>0) _app.autoAppendNextFile();
+                               try
+                               {
+                                       loader.openStream(new URL(url).openStream());
+                               }
+                               catch (IOException ioe) {
+                                       System.err.println("IO Exception : " + ioe.getMessage());
+                               }
                        }
                }
                // Close the dialog