use super::helpers::{json_str, json_str_opt};
use super::types::{IndustryCategory, IndustryChange, IndustryClfHistSw, IndustryPeRatio};
use crate::client::AkShareClient;
use crate::error::{Error, Result};
impl AkShareClient {
pub async fn stock_industry_category_cninfo(&self) -> Result<Vec<IndustryCategory>> {
let url = "http://webapi.cninfo.com.cn/api/sysapi/p_sysapi1125";
let resp = self
.post(url)
.header("User-Agent", "Mozilla/5.0")
.send()
.await
.map_err(Error::from)?
.error_for_status()
.map_err(Error::from)?;
let json: serde_json::Value = resp.json().await.map_err(Error::from)?;
let records = json
.get("records")
.and_then(|r| r.as_array())
.cloned()
.unwrap_or_default();
Ok(records
.iter()
.map(|v| IndustryCategory {
code: json_str(v, "INDUSTRYCODE"),
name: json_str(v, "INDUSTRYNAME"),
industry: json_str_opt(v, "CATALOGNAME"),
extra: None,
})
.collect())
}
pub async fn stock_industry_change_cninfo(&self, symbol: &str) -> Result<Vec<IndustryChange>> {
let url = "http://webapi.cninfo.com.cn/api/sysapi/p_sysapi1126";
let resp = self
.post(url)
.form(&[("indcode", symbol)])
.header("User-Agent", "Mozilla/5.0")
.send()
.await
.map_err(Error::from)?
.error_for_status()
.map_err(Error::from)?;
let json: serde_json::Value = resp.json().await.map_err(Error::from)?;
let records = json
.get("records")
.and_then(|r| r.as_array())
.cloned()
.unwrap_or_default();
Ok(records
.iter()
.map(|v| IndustryChange { data: v.clone() })
.collect())
}
pub async fn stock_industry_clf_hist_sw(&self, symbol: &str) -> Result<Vec<IndustryClfHistSw>> {
let url = "https://www.swsindex.com/swindex.aspx";
let resp = self
.get(url)
.query(&[("swindexcode", symbol)])
.header("User-Agent", "Mozilla/5.0")
.send()
.await
.map_err(Error::from)?;
let _text = resp.text().await.map_err(Error::from)?;
Ok(vec![IndustryClfHistSw {
data: serde_json::json!({"symbol": symbol, "note": "SW index HTML parsing not implemented"}),
}])
}
pub async fn stock_industry_pe_ratio_cninfo(
&self,
symbol: &str,
) -> Result<Vec<IndustryPeRatio>> {
let url = "http://webapi.cninfo.com.cn/api/sysapi/p_sysapi1131";
let resp = self
.post(url)
.form(&[("indcode", symbol)])
.header("User-Agent", "Mozilla/5.0")
.send()
.await
.map_err(Error::from)?
.error_for_status()
.map_err(Error::from)?;
let json: serde_json::Value = resp.json().await.map_err(Error::from)?;
let records = json
.get("records")
.and_then(|r| r.as_array())
.cloned()
.unwrap_or_default();
Ok(records
.iter()
.map(|v| IndustryPeRatio { data: v.clone() })
.collect())
}
}