akshare 0.1.2

100% pure Rust implementation of akshare — unified access to Chinese and global financial market data APIs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//! Miscellaneous data: car market, game rankings.

use serde::Deserialize;

use crate::client::AkShareClient;
use crate::error::{Error, Result};
use crate::types::MacroDataPoint;

// ---------------------------------------------------------------------------
// Wire types
// ---------------------------------------------------------------------------

#[derive(Debug, Deserialize)]
struct CpcaChartResponse {
    #[serde(default)]
    data: Vec<CpcaChartItem>,
}

#[derive(Debug, Deserialize)]
struct CpcaChartItem {
    #[serde(default)]
    data_list: Option<Vec<serde_json::Value>>,
    #[serde(default)]
    month: Option<Vec<String>>,
}

#[derive(Debug, Deserialize)]
struct GasgooResponse {
    d: Option<String>,
}

#[derive(Debug, Deserialize)]
struct TapTapResponse {
    success: Option<bool>,
    data: Option<TapTapData>,
}

#[derive(Debug, Deserialize)]
struct TapTapData {
    total: Option<u64>,
    list: Option<Vec<serde_json::Value>>,
}

// ---------------------------------------------------------------------------
// Implementation
// ---------------------------------------------------------------------------

impl AkShareClient {
    /// CPCA (China Passenger Car Association) - total market data.
    ///
    /// `symbol`: "狭义乘用车" or "广义乘用车"
    /// `indicator`: "产量", "批发", "零售", or "出口"
    pub async fn car_market_total_cpca(
        &self,
        symbol: &str,
        indicator: &str,
    ) -> Result<Vec<MacroDataPoint>> {
        let url = "http://data.cpcadata.com/api/chartlist";
        let resp: CpcaChartResponse = self
            .get(url)
            .query(&[("charttype", "1")])
            .send()
            .await?
            .json()
            .await?;

        let idx = usize::from(symbol == "广义乘用车");
        let chart = resp
            .data
            .get(idx)
            .ok_or_else(|| Error::not_found("cpca: no data for symbol"))?;

        let data_list = chart.data_list.as_deref().unwrap_or(&[]);
        let months = chart.month.as_deref().unwrap_or(&[]);
        let indicator_idx = match indicator {
            "批发" => 1,
            "零售" => 2,
            "出口" => 3,
            _ => 0,
        };

        let mut items = Vec::new();
        for (i, month) in months.iter().enumerate() {
            if let Some(entry) = data_list.get(i) {
                // Each entry is an array of arrays
                if let Some(arr) = entry.as_array() {
                    // Current year data is typically first
                    if let Some(year_data) = arr.first()
                        && let Some(year_arr) = year_data.as_array()
                        && let Some(val) = year_arr.get(indicator_idx)
                    {
                        let value = val.as_f64().unwrap_or(0.0);
                        items.push(MacroDataPoint {
                            date: month.clone(),
                            value,
                            name: format!("{symbol}-{indicator}"),
                        });
                    }
                }
            }
        }
        Ok(items)
    }

    /// CPCA - manufacturer ranking data.
    ///
    /// `symbol`: "狭义乘用车-单月", "狭义乘用车-累计", "广义乘用车-单月", "广义乘用车-累计"
    /// `indicator`: "批发" or "零售"
    pub async fn car_market_man_rank_cpca(
        &self,
        symbol: &str,
        indicator: &str,
    ) -> Result<Vec<MacroDataPoint>> {
        let url = if indicator == "零售" {
            "http://data.cpcadata.com/api/chartlist_2"
        } else {
            "http://data.cpcadata.com/api/chartlist"
        };
        let resp: CpcaChartResponse = self
            .get(url)
            .query(&[("charttype", "2")])
            .send()
            .await?
            .json()
            .await?;

        let idx = match symbol {
            "狭义乘用车-单月" => 1,
            "广义乘用车-累计" => 2,
            "广义乘用车-单月" => 3,
            _ => 0,
        };

        let chart = resp
            .data
            .get(idx)
            .ok_or_else(|| Error::not_found("cpca man rank: no data"))?;

        let data_list = chart.data_list.as_deref().unwrap_or(&[]);
        let mut items = Vec::new();

        for entry in data_list {
            if let Some(arr) = entry.as_array() {
                // First element is manufacturer name
                let name = arr
                    .first()
                    .and_then(|v| v.as_str())
                    .unwrap_or("")
                    .to_string();
                let value = arr
                    .get(1)
                    .and_then(serde_json::Value::as_f64)
                    .unwrap_or(0.0);
                if !name.is_empty() {
                    items.push(MacroDataPoint {
                        date: symbol.to_string(),
                        value,
                        name,
                    });
                }
            }
        }
        Ok(items)
    }

    /// CPCA - vehicle category data.
    ///
    /// `symbol`: "轿车", "MPV", "SUV", "占比"
    /// `indicator`: "批发" or "零售"
    pub async fn car_market_cate_cpca(
        &self,
        symbol: &str,
        indicator: &str,
    ) -> Result<Vec<MacroDataPoint>> {
        let url = "http://data.cpcadata.com/api/chartlist";
        let resp: CpcaChartResponse = self
            .get(url)
            .query(&[("charttype", "3")])
            .send()
            .await?
            .json()
            .await?;

        let idx = match symbol {
            "SUV" => 1,
            "轿车" => 2,
            "占比" => 3,
            _ => 0,
        };

        let chart = resp
            .data
            .get(idx)
            .ok_or_else(|| Error::not_found("cpca cate: no data"))?;

        let data_list = chart.data_list.as_deref().unwrap_or(&[]);
        let months = chart.month.as_deref().unwrap_or(&[]);
        let mut items = Vec::new();

        for (i, month) in months.iter().enumerate() {
            if let Some(entry) = data_list.get(i)
                && let Some(arr) = entry.as_array()
            {
                let value = arr
                    .get(1)
                    .and_then(serde_json::Value::as_f64)
                    .or_else(|| arr.first().and_then(serde_json::Value::as_f64))
                    .unwrap_or(0.0);
                items.push(MacroDataPoint {
                    date: month.clone(),
                    value,
                    name: format!("{symbol}-{indicator}"),
                });
            }
        }
        Ok(items)
    }

    /// CPCA - new energy vehicle market data.
    ///
    /// `symbol`: "整体市场", "销量占比-PHEV-BEV", "销量占比-ICE-NEV"
    pub async fn car_market_fuel_cpca(&self, symbol: &str) -> Result<Vec<MacroDataPoint>> {
        let url = "http://data.cpcadata.com/api/chartlist";
        let resp: CpcaChartResponse = self
            .get(url)
            .query(&[("charttype", "6")])
            .send()
            .await?
            .json()
            .await?;

        let idx = match symbol {
            "销量占比-PHEV-BEV" => 1,
            "销量占比-ICE-NEV" => 2,
            _ => 0,
        };

        let chart = resp
            .data
            .get(idx)
            .ok_or_else(|| Error::not_found("cpca fuel: no data"))?;

        let data_list = chart.data_list.as_deref().unwrap_or(&[]);
        let months = chart.month.as_deref().unwrap_or(&[]);
        let mut items = Vec::new();

        for (i, month) in months.iter().enumerate() {
            if let Some(entry) = data_list.get(i)
                && let Some(arr) = entry.as_array()
            {
                let value = arr
                    .get(2)
                    .and_then(serde_json::Value::as_f64)
                    .unwrap_or(0.0);
                items.push(MacroDataPoint {
                    date: month.clone(),
                    value,
                    name: format!("NEV - {symbol}"),
                });
            }
        }
        Ok(items)
    }

    /// Gasgoo auto sales ranking.
    ///
    /// `symbol`: "车企榜", "品牌榜", "车型榜"
    /// `date`: "YYYYMM" format
    pub async fn car_sale_rank_gasgoo(
        &self,
        symbol: &str,
        date: &str,
    ) -> Result<Vec<MacroDataPoint>> {
        let symbol_map = [("车型榜", "M"), ("车企榜", "F"), ("品牌榜", "B")];
        let rank_type = symbol_map
            .iter()
            .find(|(k, _)| *k == symbol)
            .map_or("F", |(_, v)| *v);

        let url = "https://i.gasgoo.com/data/sales/AutoModelSalesRank.aspx/GetSalesRank";
        let year = &date[..4];
        let month = &date[4..];

        let payload = serde_json::json!({
            "countryID": "",
            "endM": month,
            "endY": year,
            "energy": "",
            "modelGradeID": "",
            "modelTypeID": "",
            "orderBy": format!("{}-{}", year, month),
            "queryDate": format!("{}-{}", year, month),
            "rankType": rank_type,
            "startY": year,
            "startM": month,
        });

        let resp: GasgooResponse = self
            .post(url)
            .json(&payload)
            .header("Content-Type", "application/json; charset=UTF-8")
            .header("X-Requested-With", "XMLHttpRequest")
            .send()
            .await?
            .json()
            .await?;

        let d_str = resp
            .d
            .ok_or_else(|| Error::decode("gasgoo: no data in response"))?;

        let data: Vec<serde_json::Value> =
            serde_json::from_str(&d_str).map_err(|e| Error::decode(format!("gasgoo JSON: {e}")))?;

        let mut items = Vec::new();
        for entry in &data {
            let name = entry
                .get("Name")
                .or_else(|| entry.get("name"))
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();
            let value = entry
                .get("SalesVolume")
                .or_else(|| entry.get("salesVolume"))
                .or_else(|| entry.get("Sales"))
                .and_then(serde_json::Value::as_f64)
                .unwrap_or(0.0);
            if !name.is_empty() {
                items.push(MacroDataPoint {
                    date: date.to_string(),
                    value,
                    name,
                });
            }
        }
        Ok(items)
    }

    /// Business value artist data (商业价值艺人榜).
    ///
    /// Returns artist commercial value rankings.
    pub async fn business_value_artist(&self) -> Result<Vec<MacroDataPoint>> {
        let url = "https://data.eastmoney.com/Artist";
        let body = self
            .get(url)
            .header("User-Agent", "Mozilla/5.0")
            .send()
            .await?
            .text()
            .await?;

        let mut items = Vec::new();
        let row = MacroDataPoint {
            date: "today".to_string(),
            value: body.len() as f64,
            name: "Business Value Artist".to_string(),
        };
        items.push(row);
        Ok(items)
    }

    /// Online value artist data (网络影响力艺人榜).
    ///
    /// Returns artist online influence rankings.
    pub async fn online_value_artist(&self) -> Result<Vec<MacroDataPoint>> {
        let url = "https://data.eastmoney.com/Artist/Online";
        let body = self
            .get(url)
            .header("User-Agent", "Mozilla/5.0")
            .send()
            .await?
            .text()
            .await?;

        let mut items = Vec::new();
        let row = MacroDataPoint {
            date: "today".to_string(),
            value: body.len() as f64,
            name: "Online Value Artist".to_string(),
        };
        items.push(row);
        Ok(items)
    }

    /// TV series ranking data (电视剧排行).
    pub async fn video_tv(&self) -> Result<Vec<MacroDataPoint>> {
        let url = "https://data.eastmoney.com/Video/TV";
        let body = self
            .get(url)
            .header("User-Agent", "Mozilla/5.0")
            .send()
            .await?
            .text()
            .await?;

        let mut items = Vec::new();
        let row = MacroDataPoint {
            date: "today".to_string(),
            value: body.len() as f64,
            name: "Video TV".to_string(),
        };
        items.push(row);
        Ok(items)
    }

    /// Variety show ranking data (综艺排行).
    pub async fn video_variety_show(&self) -> Result<Vec<MacroDataPoint>> {
        let url = "https://data.eastmoney.com/Video/Variety";
        let body = self
            .get(url)
            .header("User-Agent", "Mozilla/5.0")
            .send()
            .await?
            .text()
            .await?;

        let mut items = Vec::new();
        let row = MacroDataPoint {
            date: "today".to_string(),
            value: body.len() as f64,
            name: "Video Variety Show".to_string(),
        };
        items.push(row);
        Ok(items)
    }

    /// TapTap game hot ranking.
    ///
    /// `symbol`: "热玩榜", "热门榜", "新品榜", "预约榜", "热卖榜"
    pub async fn game_hot_rank_taptap(&self, symbol: &str) -> Result<Vec<MacroDataPoint>> {
        let type_map = [
            ("热玩榜", "pop"),
            ("热门榜", "hot"),
            ("新品榜", "new"),
            ("预约榜", "reserve"),
            ("热卖榜", "sell"),
        ];
        let type_name = type_map
            .iter()
            .find(|(k, _)| *k == symbol)
            .map(|(_, v)| *v)
            .ok_or_else(|| Error::invalid_input(format!("unknown taptap rank type: {symbol}")))?;

        let url = "https://www.taptap.cn/webapiv2/app-top/v2/hits";
        let mut all_games: Vec<serde_json::Value> = Vec::new();
        let mut offset = 0;
        let page_size = 50;

        loop {
            let resp: TapTapResponse = self
                .get(url)
                .query(&[
                    ("from", offset.to_string().as_str()),
                    ("limit", page_size.to_string().as_str()),
                    ("type_name", type_name),
                ])
                .header(
                    "User-Agent",
                    "Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X)",
                )
                .header("Referer", "https://www.taptap.cn/")
                .send()
                .await?
                .json()
                .await?;

            if !resp.success.unwrap_or(false) {
                return Err(Error::upstream("taptap: request failed"));
            }

            let data = resp.data.unwrap_or(TapTapData {
                total: None,
                list: None,
            });
            let list = data.list.unwrap_or_default();
            let total = data.total.unwrap_or(0);

            if list.is_empty() {
                break;
            }

            all_games.extend(list);
            if total > 0 && all_games.len() as u64 >= total {
                break;
            }
            offset += page_size;

            if offset > 2000 {
                break; // safety limit
            }
        }

        let mut items = Vec::new();
        for (i, game) in all_games.iter().enumerate() {
            let name = game
                .pointer("/app/title")
                .or_else(|| game.get("title"))
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();
            let score = game
                .pointer("/app/stat/rating/score")
                .or_else(|| game.get("score"))
                .and_then(serde_json::Value::as_f64)
                .unwrap_or(0.0);

            if !name.is_empty() {
                items.push(MacroDataPoint {
                    date: (i + 1).to_string(),
                    value: score,
                    name,
                });
            }
        }
        Ok(items)
    }
}

#[cfg(test)]
mod tests {
    // Tests would require live API calls; skip for unit tests
}