Skip to content

Commit

Permalink
cleaned up the downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
Arteneko committed Oct 26, 2024
1 parent 305a9fb commit 129667e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 52 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ edition = "2021"
[dependencies]
maud = "*"
knuffel = "3"
url = "2.3"
miette = { version = "5.7", features = ["fancy"] }
reqwest = { version = "0.11", features = ["blocking"] }
serde = { version = "1.0", features = ["derive"] }
Expand Down
17 changes: 12 additions & 5 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
// we download badges to publish them alongside the website for
// cache / archive / cross-site perfs reasons

use std::{error::Error, fs::File};
use std::{
error::Error,
fs::File,
hash::{DefaultHasher, Hasher},
};

use reqwest::header::CONTENT_TYPE;
use url::Url;

pub fn download(url: Url, output_path: &str) -> Result<String, Box<dyn Error>> {
pub fn download(url: &str, label: &str) -> Result<String, Box<dyn Error>> {
println!(":: grabbing the badge at {}", url);

let mut res = reqwest::blocking::get(url)?;
Expand Down Expand Up @@ -35,9 +38,13 @@ pub fn download(url: Url, output_path: &str) -> Result<String, Box<dyn Error>> {
}
};

let mut file = File::create(format!("{}.{}", output_path, extension))?;
// Generating a proper filename
let mut hasher = DefaultHasher::new();
hasher.write(label.as_bytes());
let output_file = format!("badge.{}.{}", hasher.finish().to_string(), extension);

let mut file = File::create(format!("public/{}", output_file))?;
res.copy_to(&mut file)?;

Ok(String::from(extension))
Ok(output_file)
}
47 changes: 7 additions & 40 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{collections::hash_map::DefaultHasher, hash::Hasher};

use serde::Serialize;
use url::Url;

// Neighbor is both webring neighbor and peer (individual entity) neighbor
#[derive(knuffel::Decode, Serialize, Debug, Clone)]
Expand All @@ -22,7 +19,6 @@ pub struct Bio {

#[derive(knuffel::Decode, Serialize, Debug, Clone)]
pub struct Node {
pub extension: String,
#[knuffel(argument)]
pub url: String,
#[knuffel(property)]
Expand All @@ -34,8 +30,11 @@ pub struct Node {
pub social: Vec<Social>,
#[knuffel(children(name = "bio"))]
pub bio: Vec<Bio>,

pub cached_badge_url: Option<String>,
}

// TODO: want to rework how the nodes we ref are made cuz its kinda messy
#[derive(knuffel::Decode, Serialize, Debug, Clone)]
pub struct Social {
#[knuffel(argument)]
Expand All @@ -45,44 +44,12 @@ pub struct Social {
}

impl<'a> Node {
// This generates a hash based on the label.
// it's notably used for associating the in-cache badge image to the node
pub fn get_id(&'a self) -> String {
let mut hasher = DefaultHasher::new();
hasher.write(self.get_label().as_bytes());
hasher.finish().to_string()
}

pub fn get_url(&'a self) -> Url {
Url::parse(&self.url).expect(format!("invalid url: {}", &self.url).as_str())
}

pub fn get_badge(&'a self) -> Option<Url> {
if let Some(badge) = &self.badge {
Some(Url::parse(badge).expect(format!("invalid url: {}", badge).as_str()))
} else {
None
}
}

pub fn get_cached_badge(&'a self) -> Option<String> {
if let Some(_) = &self.get_badge() {
Some(format!("{}.badge.{}", self.get_id(), self.extension))
} else {
None
}
pub fn get_badge(&'a self) -> Option<&'a String> {
self.cached_badge_url.as_ref().or(self.badge.as_ref())
}

pub fn get_label(&'a self) -> String {
if let Some(label) = &self.label {
String::from(label)
} else {
String::from(
self.get_url()
.host_str()
.expect("node url should have a domain fragment"),
)
}
pub fn get_label(&'a self) -> &'a str {
self.label.as_ref().unwrap_or(&self.url)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/html/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ pub fn make_page(data: &Ring) -> Markup {

@for node in &data.nodes {
article.badged[node.badge.is_some()].node {
@if let Some(badge) = &node.get_cached_badge() {
@if let Some(badge) = &node.get_badge() {
header.badge {
img src=(badge) alt=(node.get_label());
}
}
main {
h3.label { a href=(node.get_url().as_str()) { (node.get_label()) } }
h3.label { a href=(node.url) { (node.get_label()) } }

@if node.bio.len() != 0 {
section.bio {
Expand Down
11 changes: 8 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ fn main() -> miette::Result<()> {
println!(":: grabbing the lil badge thingies");
for node in &mut ring.nodes {
if let Some(badge_url) = node.get_badge() {
let proper_extension = download(badge_url, &format!("public/{}.badge", node.get_id()))
.expect("somehow failed to grab the lil badge thingies");
node.extension = proper_extension;
match download(badge_url, &node.get_label()) {
Ok(cached_name) => node.cached_badge_url = Some(cached_name),
Err(e) => println!(
"failed to grab the lil badge thingy for {}: {:?}",
node.get_label(),
e
),
};
}
}

Expand Down

0 comments on commit 129667e

Please sign in to comment.