2014年1月11日

匯率從 2014 開始台灣銀行抓不到的問題

台灣銀行自從 2014/1/1 開始,網頁內容就有做一些變更,
造成 TIPTOP 判斷網頁內容錯誤,無法抓到指定的 HTML 內容來抓取匯率的下載網址。

分析一下 4GL 的程式,是副程式的 s_exrate.4gl 抓取,
透過 exrate.jar 的 JAVA 程式把網頁下載到 /reout 的目錄下。
因此就要從 exrate.jar 去下手,修改正確的網址連結。

首先要先反組譯,將藍色的部份做修改,然後再重新組譯再製成 JAR 檔就大功告成了,
以下是修改的 JAVA 程式碼:

package com.dsc.tiptop.netutil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExRateGetter
{
  public static int DEFAULT_TIMEOUT = 30000;
  public static final String STR_DEFAULT_TIMEOUT = new Integer(DEFAULT_TIMEOUT).toString();

  public static void main(String[] args)
  {
    String sUrl = "http://rate.bot.com.tw/Pages/Static/UIP003.zh-TW.htm";
    String sUrl2 = null;
    String sFilePath = null;
    Pattern pattern = null;
    Matcher matcher = null;
    String sContent = null;
    URL url = null;
    String sHost = null;
    //String sHref = "self.location.href='";
    String sHref = "href=\"";

    int pos1 = 0;
    int posStart = 0;
    int posEnd = 0;

    if (args.length >= 1) sUrl = args[0];

    try
    {
      url = new URL(sUrl);
      sHost = url.getHost();
      sContent = getContent(url);

      //pattern = Pattern.compile("(?i)<input name=\"DownloadTxt\".*/>");
      pattern = Pattern.compile("(?i)<td><a id=\"DownloadTxt\" class=\"buttonLink\".*");
      matcher = pattern.matcher(sContent);
      if (matcher.find()) sContent = matcher.group(0); else {
        throw new RuntimeException("Can't get the suitable URL.");
      }

      pos1 = sContent.indexOf(sHref);
      if (pos1 > 0) {
        posStart = pos1 + sHref.length();
        for (int i = posStart; i < sContent.length(); i++) {
          //if (sContent.charAt(i) == '\'') {
            if (sContent.charAt(i) == '\"') {
            posEnd = i;
            break;
          }
        }
      }

      sUrl2 = sContent.substring(posStart, posEnd);
      sUrl2 = sUrl2.replaceAll("&amp;", "&");

      sUrl2 = "http://" + sHost + sUrl2;

      sContent = getContent(sUrl2);

      sFilePath = getFilePath();
      write(sFilePath, sContent);

      System.out.print("0 \"");
      System.out.print(sFilePath);
      System.out.println("\"");
    } catch (Exception e) {
      System.out.print("-1 \"");
      System.out.print(e);
      System.out.println("\"");
    }
  }

1 則留言: