Side Project Document
  • 如果沒能一次成功,那就叫它 1.0 版吧。
  • Stock Price for LINE Notify V1
    • 前言
    • 一、申請玉山富果 API
    • 二、申請 LINE Notify Token
    • 三、環境架設與資料表設定
    • 四、SSIS 設計
      • SSIS 流程圖
      • Move to History Table & Truncate Table
      • Foreach Loop to Get API
      • Lookup File - Chart
      • Lookup File - Quote
      • Join Table to Result Set
      • Post Result Set to LINE Notify
      • Delete Over 3 days Files
      • 事件處理常式
    • 五、安裝教學
  • Stock Price for LINE Notify V2
    • 前言
    • 一、軟體架構與流程圖
    • 二、環境架設與資料表設定
    • 三、Python 程式說明
      • 主程式:Local_Stock_Main.py
      • 取得個股基本資訊:Local_Stock_Meta.py
      • 取得個股交易資訊:Local_Stock_Quote.py
      • 將折線圖發送至 LINE Notify:Local_Stock_Line_Notify.py
      • 資料解密:Local_Encryption.py
      • 排程設定
  • Stock Price for LINE Notify V3
    • 前言
    • 一、軟體架構與流程圖
    • 二、環境架設與資料表設定
    • 三、Python 程式說明
      • Local_Stock_Main.py
      • Local_Stock_Meta.py
      • Local_Stock_Quote.py
      • Local_Stock_Line_Notify.py
      • Create_RSA_Key.py
      • Encrypt_Data.py
      • Decrypt_Data.py
      • 排程設定
      • 加碼:.py 打包成 .exe 執行檔
Powered by GitBook
On this page
  • Foreach 迴圈容器
  • 資料流程工作
  1. Stock Price for LINE Notify V1
  2. 四、SSIS 設計

Foreach Loop to Get API

Foreach 迴圈 &資料流程

Foreach 迴圈容器

  • 目的:設定股票代號來源檔案路徑

  • Foreach 迴圈容器列舉值:Foreach 檔案列舉值

  • 資料夾:C:\Stock LINE Notify

  • 檔案:Stock_Code.txt(包含完整路徑與檔案名稱)

  • 變數對應:User::SourceFileName_StockCode

資料流程工作

  • 元件:指令碼元件

  • 目的:打玉山富果的 API,把結果存成 JSON 檔案。

  • Script:main.cs

#region Ref Start
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
#endregion Ref End

// ......


#region API Setting Start
private const string _URL_Quote = @"https://api.fugle.tw/realtime/v0.3/intraday/quote?apiToken=[TokenID]&oddLot=true&symbolId=";
private const string _URL_Chart = @"https://api.fugle.tw/realtime/v0.3/intraday/chart?apiToken=[TokenID]&oddLot=true&symbolId=";
#endregion API Setting End

// ......

public override void CreateNewOutputRows()
{
    /*
      Add rows by calling the AddRow method on the member variable named "<Output Name>Buffer".
      For example, call MyOutputBuffer.AddRow() if your output was named "MyOutput".
    */

    GetStockChart();
    GetStockQuote();
}

public void GetStockChart()
{
    foreach (string line in File.ReadLines(Variables.SourceFileNameStockCode))
    {
        HttpClient httpClient = new HttpClient();
        httpClient.BaseAddress = new Uri(_URL_Chart + line);

        // As of .NET 4.0, the minimum of TLS is 1.2
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        HttpResponseMessage response = httpClient.GetAsync(_URL_Chart + line).Result;

        if (response.IsSuccessStatusCode)
        {
            var dataObjects = response.Content.ReadAsStringAsync().Result;

            var dataObjects1 = "";

            HttpResponseMessage response1 = httpClient.GetAsync(_URL_Chart + line).Result;
            dataObjects1 = response1.Content.ReadAsStringAsync().Result;

            // Write to JSON file.
            string docPath = @"C:\Stock LINE Notify\Chart";
            string fileName = "Stock_Chart_";
            string date = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff");
            using (StreamWriter streamWriter = new StreamWriter(Path.Combine(docPath, fileName + date + ".json")))
            {
                streamWriter.WriteLine(dataObjects1);
                streamWriter.Close();
            }
        }
        else
        {
            Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
         }

         httpClient.Dispose();
    }
}

public void GetStockQuote()
{
    foreach (string line in File.ReadLines(Variables.SourceFileNameStockCode))
    {
        HttpClient httpClient = new HttpClient();
        httpClient.BaseAddress = new Uri(_URL_Quote + line);

        // As of .NET 4.0, the minimum of TLS is 1.2
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        HttpResponseMessage response = httpClient.GetAsync(_URL_Quote + line).Result;

        if (response.IsSuccessStatusCode)
        {
            var dataObjects = response.Content.ReadAsStringAsync().Result;

            var dataObjects1 = "";

            HttpResponseMessage response1 = httpClient.GetAsync(_URL_Quote + line).Result;
            dataObjects1 = response1.Content.ReadAsStringAsync().Result;

            // Write to JSON file.
            string docPath = @"C:\Stock LINE Notify\Quote";
            string fileName = "Stock_Quote_";
            string date = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff");
            using (StreamWriter streamWriter = new StreamWriter(Path.Combine(docPath, fileName + date + ".json")))
            {
                streamWriter.WriteLine(dataObjects1);
                streamWriter.Close();
            }
        }
        else
        {
            Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
        }

        httpClient.Dispose();
    }
}

Last updated 3 years ago