algorithm_problem_client/atcoder/
client.rs

1use crate::util;
2use crate::Result;
3
4use super::*;
5
6const ATCODER_PREFIX: &str = "https://atcoder.jp";
7
8pub struct AtCoderClient;
9
10impl Default for AtCoderClient {
11    fn default() -> Self {
12        Self
13    }
14}
15
16impl AtCoderClient {
17    pub async fn fetch_atcoder_contests(&self, page: u32) -> Result<Vec<AtCoderContest>> {
18        let url = format!("{}/contests/archive?lang=ja&page={}", ATCODER_PREFIX, page);
19        let html = util::get_html(&url).await?;
20        contest::scrape(&html)
21    }
22
23    /// Fetch a list of submissions.
24    pub async fn fetch_atcoder_submission_list(
25        &self,
26        contest_id: &str,
27        page: Option<u32>,
28    ) -> Result<AtCoderSubmissionListResponse> {
29        let page = page.unwrap_or(1);
30        let url = format!(
31            "{}/contests/{}/submissions?page={}",
32            ATCODER_PREFIX, contest_id, page
33        );
34        let html = util::get_html(&url).await?;
35        let submissions = submission::scrape(&html, contest_id)?;
36        let max_page = submission::scrape_submission_page_count(&html)?;
37        Ok(AtCoderSubmissionListResponse {
38            max_page,
39            submissions,
40        })
41    }
42
43    pub async fn fetch_problem_list(&self, contest_id: &str) -> Result<Vec<AtCoderProblem>> {
44        let url = format!("{}/contests/{}/tasks", ATCODER_PREFIX, contest_id);
45        let html = util::get_html(&url).await?;
46        problem::scrape(&html, contest_id)
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53    use futures::executor::block_on;
54
55    #[test]
56    fn test_fetch_contest_list() {
57        let client = AtCoderClient::default();
58        let contests = block_on(client.fetch_atcoder_contests(1)).unwrap();
59        assert_eq!(contests.len(), 50);
60    }
61
62    #[test]
63    fn test_fetch_problem_list() {
64        let client = AtCoderClient::default();
65        let problems = block_on(client.fetch_problem_list("abc107")).unwrap();
66        assert_eq!(problems.len(), 4);
67    }
68
69    #[test]
70    fn test_fetch_submission_list() {
71        let client = AtCoderClient::default();
72        let response = block_on(client.fetch_atcoder_submission_list("xmascon17", None)).unwrap();
73        assert_eq!(response.submissions.len(), 20);
74
75        let response =
76            block_on(client.fetch_atcoder_submission_list("xmascon17", Some(response.max_page)))
77                .unwrap();
78        assert!(!response.submissions.is_empty());
79
80        let response = block_on(
81            client.fetch_atcoder_submission_list("xmascon17", Some(response.max_page + 1)),
82        );
83        assert!(response.is_err());
84    }
85}