From f4c92fbf8362f789ce3c00de6cf6a64ebc2502d8 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Mon, 26 Apr 2021 15:42:39 -0700 Subject: major: :star: --- src/utils.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/utils.rs (limited to 'src/utils.rs') 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 { + 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::() + .await?, + ) +} + +pub async fn filter_languages() -> Vec { + 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 { + 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 +} -- cgit v1.2.3