diff options
| author | Fuwn <[email protected]> | 2021-04-26 15:42:39 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-04-26 15:42:39 -0700 |
| commit | f4c92fbf8362f789ce3c00de6cf6a64ebc2502d8 (patch) | |
| tree | 78545fde636ec248aa039aa2ec1581b9b9055338 /src/utils.rs | |
| parent | fmt: Change case (diff) | |
| download | api-worker-f4c92fbf8362f789ce3c00de6cf6a64ebc2502d8.tar.xz api-worker-f4c92fbf8362f789ce3c00de6cf6a64ebc2502d8.zip | |
major: :star:
Diffstat (limited to 'src/utils.rs')
| -rw-r--r-- | src/utils.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..d51a813 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,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 +} |