diff --git a/Cargo.toml b/Cargo.toml
index 0ccfec4..55f708e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,7 +1,7 @@
[package]
name = "taikoothong"
version = "0.0.0"
-workspace = "../"
+#workspace = "."
edition = "2021"
publish = false
@@ -16,6 +16,7 @@ chrono = "0.4.11"
rocket = "0.5.0-rc.2"
round = "0.1.0"
num-format = "0.4.4"
+rss = "1.9.0"
[dependencies.rocket_dyn_templates]
rocket_dyn_templates = "0.5.0-rc.3-2023-06-09"
diff --git a/d3js/example.html b/d3js/example.html
new file mode 100644
index 0000000..51e6e85
--- /dev/null
+++ b/d3js/example.html
@@ -0,0 +1,168 @@
+
+
+
+
+
+
+ 5日
+ 30日
+ 今年迄今
+
+
+
+
+
+
diff --git a/src/main.rs b/src/main.rs
index 970821b..cfa7708 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -11,6 +11,7 @@ use rocket_dyn_templates::Template;
use rocket::{Rocket, Build};
use round::round;
use num_format::{Locale, WriteFormatted};
+use rss::Channel;
#[macro_use] extern crate rocket;
@@ -35,14 +36,23 @@ impl Date {
}
-
-#[get("/")]
-fn get_tw_stock(stock_id: String) -> Template {
- // let a = "👋 Hello, stock no: a22";
-
- let response_body = get_stock_data(stock_id.as_str(), Date::Day(1), Date::Day(5));
+#[get("//json")]
+fn get_tw_stock_json(stock_id: String) -> String {
+ let response_body = get_stock_data(stock_id.as_str(), Date::Day(1), Date::YearToDate);
let response_json: Value = serde_json::from_str(response_body.as_str()).unwrap();
+
+ let mut stock_total_data = tw_stock_process_json(&response_json);
+
+ let stock_total_data_json = serde_json::json!(stock_total_data);
+
+ return stock_total_data_json.to_string();
+}
+
+
+fn tw_stock_process_json(response_json : &Value) -> HashMap<&str, Vec>{
+
+
let days_in_unix_time = &response_json["chart"]["result"][0]["timestamp"];
let mut stock_total_data = HashMap::new();
@@ -55,8 +65,6 @@ fn get_tw_stock(stock_id: String) -> Template {
_ => vec![format!("Not a series of date")],
};
- println!("{:?}", &days_in_unix_time);
-
stock_total_data.insert("date", days_in_custom_format);
let mut open_prices : Vec = vec![];
@@ -99,25 +107,33 @@ fn get_tw_stock(stock_id: String) -> Template {
stock_total_data.insert("low", low_prices);
stock_total_data.insert("volume", volumes);
+ return stock_total_data;
+}
+
+#[get("/")]
+fn get_tw_stock(stock_id: String) -> Template {
+
+ let response_body = get_stock_data(stock_id.as_str(), Date::Day(1), Date::YearToDate);
+ let response_json: Value = serde_json::from_str(response_body.as_str()).unwrap();
+
+
+ let mut stock_total_data = tw_stock_process_json(&response_json);
+ stock_total_data.insert("stock_id", vec![stock_id]);
let mut stock_total_data_by_date = transverse_stock_data_by_date(stock_total_data.clone());
//let mut stock_total_data_by_date_wrapper = HashMap::new();
//stock_total_data_by_date_wrapper.insert("data", stock_total_data_by_date);
- //println!("{:?}", stock_total_data_by_date_wrapper);
- println!("{:?}", stock_total_data);
return Template::render("tw_stock", stock_total_data);
}
fn json_unix_time_to_date(json_value: &Value) -> String {
let unix_time = json_value.as_i64().unwrap();
- println!("{:?}", unix_time);
let naive_time = Utc.timestamp_opt(unix_time, 0).unwrap();
let date = format!("{}", naive_time.format("%Y-%m-%d"));
- println!("{:?}", date);
return date;
}
@@ -156,27 +172,35 @@ fn get_stock_data(stock_id: &str, interval: Date, range: Date) -> String {
stock_id, intrval_str, range_str
);
- let mut curl_easy = Easy::new(); // fetch the data with the curl binding
- let mut response = String::new();
- {
- curl_easy.url(url.as_str()).unwrap();
- let mut curl_transfer = curl_easy.transfer();
+ return get_url_data(&url);
+}
- curl_transfer
- .write_function(|data| {
- response.push_str(std::str::from_utf8(data).unwrap());
- Ok(data.len())
- })
- .unwrap();
+fn get_url_data(url : &String) -> String{
+let mut curl_easy = Easy::new(); // fetch the data with the curl binding
+let mut response = String::new();
- curl_transfer.perform().unwrap();
- }
+{
+ curl_easy.url(url.as_str()).unwrap();
- let response_returned = response.clone();
+ let mut curl_transfer = curl_easy.transfer();
- return response_returned;
+ curl_transfer
+ .write_function(|data| {
+ let s = match std::str::from_utf8(data){
+ Err(_) => {println!("解碼錯誤"); ""}
+ Ok(cont) => { println!("解碼成功"); cont}
+ };
+ response.push_str(s);
+ Ok(data.len())
+ })
+ .unwrap();
+
+ curl_transfer.perform().unwrap();
+}
+
+return response.clone();
}
@@ -184,7 +208,7 @@ fn get_stock_data(stock_id: &str, interval: Date, range: Date) -> String {
fn rocket() -> Rocket {
// rocket::ignite().mount("/", routes![index]).launch();
rocket::build().attach(Template::fairing())
- .mount("/tw", routes![get_tw_stock])
+ .mount("/tw", routes![get_tw_stock, get_tw_stock_json])
}