PseFramesClient.java

  1. /**
  2.  * Copyright 2012 the original author or authors
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *         http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package com.googlecode.phisix.api.client;

  17. import java.io.IOException;
  18. import java.io.Reader;
  19. import java.io.StringReader;

  20. import org.jsoup.Jsoup;
  21. import org.jsoup.nodes.Document;
  22. import org.jsoup.select.Elements;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;

  25. import com.googlecode.phisix.api.model.Stocks;
  26. import com.googlecode.phisix.api.parser.GsonAwareParser;
  27. import com.googlecode.phisix.api.parser.Parser;

  28. /**
  29.  * @author Edge Dalmacio
  30.  */
  31. public class PseFramesClient implements PseClient {

  32.     private final static Logger LOGGER = LoggerFactory.getLogger(PseFramesClient.class);
  33.     private final Parser<Reader,Stocks> parser;
  34.    
  35.     public PseFramesClient() {
  36.         this.parser = new GsonAwareParser();
  37.     }

  38.     @Override
  39.     public Stocks getSecuritiesAndIndicesForPublic(String referer, String method, boolean ajax) {
  40.         return getSecuritiesAndIndicesForPublic(referer, null, method, ajax);
  41.     }

  42.     @Override
  43.     public Stocks getSecuritiesAndIndicesForPublic(String referer, String userAgent, String method, boolean ajax) {
  44.         try {
  45.             Document doc = Jsoup.connect("https://frames.pse.com.ph").get();
  46.             Elements newsHeadlines = doc.select("#JsonId");
  47.             if (!newsHeadlines.isEmpty()) {
  48.                 String value = newsHeadlines.get(0).attr("value");
  49.                 return parser.parse(new StringReader(value));
  50.             }
  51.         } catch (IOException e) {
  52.             if (LOGGER.isErrorEnabled()) {
  53.                 LOGGER.error(e.getMessage(), e);
  54.             }
  55.         }
  56.         return null;
  57.     }

  58.     @Override
  59.     public String findSecurityOrCompany(String method, boolean ajax, String body) {
  60.         // TODO Auto-generated method stub
  61.         return null;
  62.     }

  63.     @Override
  64.     public String companyInfo(String method, boolean ajax, String body) {
  65.         // TODO Auto-generated method stub
  66.         return null;
  67.     }

  68.     @Override
  69.     public String companyInfoHistoricalData(String method, boolean ajax, String body) {
  70.         // TODO Auto-generated method stub
  71.         return null;
  72.     }

  73. }