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
use regex::Regex;

use crate::{
    api_traits::{ApiOperation, TrendingProjectURL},
    cmds::trending::TrendingProject,
    http::{Headers, Method},
    io::{HttpRunner, Response},
    remote::query::github_trending_language_projects,
    Result,
};

use super::Github;

impl<R: HttpRunner<Response = Response>> TrendingProjectURL for Github<R> {
    fn list(&self, language: String) -> Result<Vec<TrendingProject>> {
        let url = format!("https://{}/trending/{}", self.domain, language);
        let mut headers = Headers::new();
        headers.set("Accept".to_string(), "text/html".to_string());
        let response = github_trending_language_projects::<_, String>(
            &self.runner,
            &url,
            None,
            headers,
            Method::GET,
            ApiOperation::SinglePage,
        )?;
        parse_response(response)
    }
}

fn parse_response(response: Response) -> Result<Vec<TrendingProject>> {
    let body = response.body;
    let proj_re = Regex::new(r#"href="/[a-zA-Z0-9_-]*/[a-zA-Z0-9_-]*/stargazers""#).unwrap();
    let description_re = Regex::new(r#"<p class="col-9 color-fg-muted my-1 pr-4">"#).unwrap();
    let mut descr_header_matched = false;
    let mut trending = Vec::new();
    let mut description = String::new();
    for line in body.lines() {
        if descr_header_matched {
            description = line.trim().to_string();
            descr_header_matched = false;
            continue;
        }
        if description_re.find(line).is_some() {
            descr_header_matched = true;
            continue;
        }
        if let Some(proj) = proj_re.find(line) {
            let proj = proj.as_str().split('"').collect::<Vec<&str>>();
            let proj_paths = proj[1].split('/').collect::<Vec<&str>>();
            if proj_paths[1] == "features" || proj_paths[1] == "about" || proj_paths[1] == "site" {
                continue;
            }
            let url = format!("https://github.com/{}/{}", proj_paths[1], proj_paths[2]);
            trending.push(TrendingProject::new(url, description.to_string()));
        }
    }
    Ok(trending)
}

#[cfg(test)]
mod test {

    use super::*;

    use crate::{
        setup_client,
        test::utils::{default_github, ContractType, ResponseContracts},
    };

    #[test]
    fn test_list_trending_projects() {
        let contracts =
            ResponseContracts::new(ContractType::Github).add_contract(200, "trending.html", None);
        let (client, github) = setup_client!(contracts, default_github(), dyn TrendingProjectURL);

        let trending = github.list("rust".to_string()).unwrap();
        assert_eq!(2, trending.len());
        assert_eq!("https://github.com/trending/rust", *client.url(),);
        assert_eq!(
            Some(ApiOperation::SinglePage),
            *client.api_operation.borrow()
        );
        let proj = &trending[0];
        assert_eq!("https://github.com/lencx/ChatGPT", proj.url);
        assert_eq!(
            "🔮 ChatGPT Desktop Application (Mac, Windows and Linux)",
            proj.description
        );
        let proj = &trending[1];
        assert_eq!("https://github.com/sxyazi/yazi", proj.url);
        assert_eq!(
            "💥 Blazing fast terminal file manager written in Rust, based on async I/O.",
            proj.description
        );
    }
}