Android Open Source - bike-friend You Bike Station Html Parser V2






From Project

Back to project page bike-friend.

License

The source code is released under:

GNU General Public License

If you think the Android project bike-friend listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.lemoulinstudio.bikefriend.ubike;
/* w  w  w  .  ja  va 2  s .  c om*/
import com.google.android.gms.maps.model.LatLng;
import com.lemoulinstudio.bikefriend.StationParser;
import com.lemoulinstudio.bikefriend.ParsingException;
import com.lemoulinstudio.bikefriend.Utils;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.Reader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.net.URLDecoder;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;

/**
 * This parser extracts information from the website of YouBike.
 * This is a temporary solution until I find a data source which is easier to read (JSON or XML).
 * 
 * If you work for YouBike, please let me know if you know which data source I should use, thank you.
 * I can be contacted by email: "vincent.cantin at le-moulin-studio.com" (please replace "at" by "@").
 *
 * @author Vincent Cantin
 */
public class YouBikeStationHtmlParserV2 implements StationParser<YouBikeStation> {

  private final SimpleDateFormat dateFormat;

  public YouBikeStationHtmlParserV2() {
    TimeZone taiwanTimeZone = TimeZone.getTimeZone("Asia/Taipei");
    this.dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    this.dateFormat.setTimeZone(taiwanTimeZone);
  }
  
  @Override
  public List<YouBikeStation> parse(InputStream in) throws IOException, ParsingException {
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    
    try {
      for (String line = br.readLine(); line != null; line = br.readLine()) {
        if (line.contains("JSON.parse(")) {
          Pattern p = Pattern.compile("'([^']*)'");
          Matcher m = p.matcher(line);

          if (m.find()) {
            String encodedData = m.group(1);
            String jsonText = URLDecoder.decode(encodedData, "UTF-8");
            return parseStations(jsonText);
          }
        }
      }
    }
    catch (Exception e) {
      throw new ParsingException(e);
    }
    finally {
      br.close();
    }
    
    // If we cannot find the list, we throw an exception.
    throw new ParsingException("Data not found");
  }
  
  private List<YouBikeStation> parseStations(String jsonText) throws JSONException {
    List<YouBikeStation> result = new ArrayList<YouBikeStation>();
    
    JSONObject jsonRoot = new JSONObject(jsonText);
    Iterator iter = jsonRoot.keys();
    while (iter.hasNext()) {
      String key = (String) iter.next();
      try {
        JSONObject jsonStation = jsonRoot.getJSONObject(key);
        //Log.i(jsonStation.toString(2));

        YouBikeStation station = new YouBikeStation();
        station.date           = dateFormat.parse(jsonStation.getString("mday"));
        station.chineseName    = jsonStation.optString("sna", "n/a");
        station.chineseAddress = jsonStation.optString("ar", "n/a");
        station.englishName    = jsonStation.optString("sna", "n/a");
        station.englishAddress = jsonStation.optString("ar", "n/a");
        station.location = new LatLng(
                Double.parseDouble(jsonStation.getString("lat")),
                Double.parseDouble(jsonStation.getString("lng")));
        station.nbBikes = jsonStation.getInt("sbi");
        station.nbEmptySlots = jsonStation.getInt("bemp");
        station.nbTotalPlaces = jsonStation.getInt("tot");

        result.add(station);
      }
      catch (Exception e) {
        // If we cannot read this station, we just skip it.
      }
    }
    
    return result;
  }
  
}




Java Source Code List

com.lemoulinstudio.bikefriend.InternetStationProvider.java
com.lemoulinstudio.bikefriend.ParsingException.java
com.lemoulinstudio.bikefriend.StationInfoWindowAdapter.java
com.lemoulinstudio.bikefriend.StationMapActivity.java
com.lemoulinstudio.bikefriend.StationParser.java
com.lemoulinstudio.bikefriend.StationProvider.java
com.lemoulinstudio.bikefriend.Station.java
com.lemoulinstudio.bikefriend.Utils.java
com.lemoulinstudio.bikefriend.cbike.CBikeStationXmlParserV1.java
com.lemoulinstudio.bikefriend.cbike.CBikeStation.java
com.lemoulinstudio.bikefriend.cbike.KaohsiungStationProvider.java
com.lemoulinstudio.bikefriend.ubike.ChanghuaStationProvider.java
com.lemoulinstudio.bikefriend.ubike.TaichungStationProvider.java
com.lemoulinstudio.bikefriend.ubike.TaipeiStationProvider.java
com.lemoulinstudio.bikefriend.ubike.YouBikeStationCSVParserV1.java
com.lemoulinstudio.bikefriend.ubike.YouBikeStationHtmlParserV2.java
com.lemoulinstudio.bikefriend.ubike.YouBikeStationJsonParserV1.java
com.lemoulinstudio.bikefriend.ubike.YouBikeStation.java
com.lemoulinstudio.bikefriend.webapp.StationParser.java
com.lemoulinstudio.bikefriend.webapp.cbike.BIKEStationData.java
com.lemoulinstudio.bikefriend.webapp.cbike.BIKEStation.java
com.lemoulinstudio.bikefriend.webapp.cbike.CBikeStationParser.java
com.lemoulinstudio.bikefriend.webapp.cbike.XmlStation.java
com.lemoulinstudio.bikefriend.webapp.conf.ShutdownHook.java
com.lemoulinstudio.bikefriend.webapp.conf.StartupHook.java
com.lemoulinstudio.bikefriend.webapp.entity.StationLog.java
com.lemoulinstudio.bikefriend.webapp.entity.Station.java
com.lemoulinstudio.bikefriend.webapp.io.InputStreamSequence.java
com.lemoulinstudio.bikefriend.webapp.io.IntArrayInputStream.java
com.lemoulinstudio.bikefriend.webapp.quartz.LoadStationDataJob.java
com.lemoulinstudio.bikefriend.webapp.rest.StationResource.java
com.lemoulinstudio.bikefriend.webapp.ubike.MarkerList.java
com.lemoulinstudio.bikefriend.webapp.ubike.Marker.java
com.lemoulinstudio.bikefriend.webapp.ubike.YouBikeStationParser.java
com.lemoulinstudio.bikefriend.webapp.vo.StationLogVo.java
com.lemoulinstudio.bikefriend.webapp.vo.StationVo.java