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