aboutsummaryrefslogtreecommitdiff
path: root/src/utils.rs
blob: d51a813d0b3017ae272956d0b454f4589d967ee4 (plain) (blame)
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
// Copyleft 2021-2021 The Senpy Club
// SPDX-License-Identifier: GPL-3.0-only

use crate::{
  constants::{GITHUB_API_ENDPOINT, GITHUB_USER_CONTENT, USER_AGENT},
  structures::GitHubAPIResponse,
};

pub async fn github_api() -> Result<GitHubAPIResponse, reqwest::Error> {
  Ok(
    reqwest::Client::new()
      .get(GITHUB_API_ENDPOINT)
      .header("User-Agent", USER_AGENT)
      .header(
        "Authorization",
        format!("token {}", std::env::var("GITHUB_TOKEN").unwrap()),
      )
      .send()
      .await?
      .json::<GitHubAPIResponse>()
      .await?,
  )
}

pub async fn filter_languages() -> Vec<String> {
  let mut languages = vec![];

  for i in github_api().await.unwrap().tree {
    if i._type == "tree" {
      languages.push(i.path);
    }
  }

  languages
}

pub async fn filter_images_by_language(language: String) -> Vec<String> {
  let mut images = vec![];

  for i in github_api().await.unwrap().tree {
    // Example:
    //  "Language/Image.png" would become ["Language", "Image.png"]

    // TODO: Fix this with type_ascription
    let x: Vec<&str> = i.path.split("/").collect();
    if x[0] == language && i.path.contains('/') {
      images.push(format!("{}{}", GITHUB_USER_CONTENT, i.path))
    }
  }

  images
}