travisgoodspeed / codeplugtool

KK4VCZ's tool for programming the Kenwood TH-D74 .
8 stars 1 forks source link

RepeaterBook API Queries #11

Closed travisgoodspeed closed 4 years ago

travisgoodspeed commented 4 years ago

Currently we have support for CAT radios and potential support for Image radios, but we'll also need some sort of an abstraction for API queries, so that services like RadioReference and RepeaterBook can be used to quickly generate codeplugs.

The first of these will be RepeaterBook, because it doesn't require an API key or registration. API Documentation is freely available on their wiki.

This is an example of a ranged query, which does not use the API. https://www.repeaterbook.com/repeaters/prox_result.php?city=37902&distance=10&Dunit=m&status_id=1

Downloads are also available in the CHIRP CSV fromat, which we already support.

https://www.repeaterbook.com/repeaters/downloads/CHIRP/app_direct.php?loc=Tacoma,%20WA&dist=25&band=14
https://www.repeaterbook.com/repeaters/downloads/CHIRP/app_direct.php?loc=Tacoma&band=14

Chirp performs similar queries to its own mirror:

http://chirp.danplanet.com/query/rb/1.0/app_direct?loc=Tacoma&band=14
travisgoodspeed commented 4 years ago

This is enough to do a minimal query.

package com.kk4vcz.codeplug.api;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import com.kk4vcz.codeplug.Radio;
import com.kk4vcz.codeplug.RadioAPI;

public class RepeaterBook implements RadioAPI {
    //Test method, to fetch a given query.
    public static void main(String[] args) {
        System.out.println("Testing a simple RepeaterBook query:");
        RepeaterBook rb=new RepeaterBook();

        try {
            rb.queryProximity("Knoxville, TN", 10);
            //rb.queryProximity("Tacoma", 10);
        }catch(IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Radio queryProximity(String loc, float distance) throws IOException {
        try {

            Map<String, String> parameters = new HashMap<>();
            parameters.put("loc", loc);
            parameters.put("dist", "100");
            parameters.put("band", "14"); //2 meters

            //Two servers support this protocol.
            URL url = new URL("https://www.repeaterbook.com/repeaters/downloads/CHIRP/app_direct.php?"+ParameterStringBuilder.getParamsString(parameters));
            //URL url = new URL("http://chirp.danplanet.com/query/rb/1.0/app_direct?");

            //Begin with the simple connection.
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("User-Agent", "curl/7.68.0");
            con.setDoOutput(true);

            int status = con.getResponseCode();
            if(status!=200) {
                System.out.println("Request status="+status);
                return null;
            }

            BufferedReader in = new BufferedReader(
                      new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer content = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine+"\n");
            }
            in.close();

            System.out.println(content);

        }catch(MalformedURLException e) {
            e.printStackTrace();
        }catch(ProtocolException e) {
            e.printStackTrace();            
        }

        return null;
    }

    public static class ParameterStringBuilder {
        public static String getParamsString(Map<String, String> params) 
          throws UnsupportedEncodingException{
            StringBuilder result = new StringBuilder();

            for (Map.Entry<String, String> entry : params.entrySet()) {
              result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
              result.append("=");
              result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
              result.append("&");
            }

            String resultString = result.toString();
            return resultString.length() > 0
              ? resultString.substring(0, resultString.length() - 1)
              : resultString;
        }
    }
}