Skip to content

Commit

Permalink
Merge pull request #16 from Stridsvagn69420/danbooru
Browse files Browse the repository at this point in the history
Add support for Danbooru
Fix missing newline on output
Change file name format
  • Loading branch information
Stridsvagn69420 authored Jun 18, 2024
2 parents ef343b4 + 2730d03 commit bfe7e9a
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 32 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wallpaper-dl"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
license = "EUPL-1.2"
readme = "README.md"
Expand All @@ -14,7 +14,7 @@ publish = true

[dependencies]
apputils = "0.1"
url = "2.5"
url = { version = "2.5", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
reqwest = { version = "0.12", features = ["blocking", "rustls-tls-native-roots", "deflate", "gzip", "brotli", "json", "charset"], default-features = false }
scraper = "0.19"
Expand Down
2 changes: 1 addition & 1 deletion src/downloaders/alphacoders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ impl Alphacoders {
/// Downloader designed for [Wallpaper Abyss](https://wall.alphacoders.com/)
pub struct WallpaperAbyss(Alphacoders);

#[allow(refining_impl_trait)]
impl Downloader for WallpaperAbyss {
#[allow(refining_impl_trait)]
fn new(client: &Client, url: Url) -> DownloaderResult<Self> {
let id = match url.query() {
Some(x) => x.replace("i=", ""),
Expand Down
2 changes: 1 addition & 1 deletion src/downloaders/artstation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub struct ArtStation {
assets: Vec<Asset>
}

#[allow(refining_impl_trait)]
impl Downloader for ArtStation {
#[allow(refining_impl_trait)]
fn new(client: &Client, mut url: Url) -> DownloaderResult<Self> {
let id_path = url.path().replace("/artwork/", "/projects/");
let api_path = format!("{}.json", id_path);
Expand Down
43 changes: 43 additions & 0 deletions src/downloaders/danbooru.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use super::{quick_get, Downloader, DownloaderResult, Urls};
use reqwest::blocking::Client;
use serde::Deserialize;
use url::Url;

#[derive(Deserialize)]
pub struct DanbooruApi {
id: u32,
file_url: Url
}

pub struct Danbooru {
id: String,
file_url: Url
}

impl From<DanbooruApi> for Danbooru {
fn from(value: DanbooruApi) -> Danbooru {
Danbooru {
file_url: value.file_url,
id: value.id.to_string()
}
}
}

#[allow(refining_impl_trait)]
impl Downloader for Danbooru {
fn new(client: &Client, mut url: Url) -> DownloaderResult<Self> {
// Append .json extension
let newpath = format!("{}.json", url.path());
url.set_path(&newpath);

// Make API Request
let api_res = quick_get(client, url)?.json::<DanbooruApi>()?;
Ok(api_res.into())
}
fn image_id(&self) -> &str {
&self.id
}
fn image_url(&self) -> DownloaderResult<Urls> {
Ok(self.file_url.clone().into())
}
}
5 changes: 4 additions & 1 deletion src/downloaders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ use url::{ParseError, Url};
mod alphacoders;
mod artstation;
mod wallhaven;
mod danbooru;

pub use alphacoders::{ArtAbyss, ImageAbyss, WallpaperAbyss};
pub use artstation::ArtStation;
pub use wallhaven::Wallhaven;

pub use danbooru::Danbooru;

macro_rules! ok_box {
($dl:expr) => {
Expand All @@ -29,6 +30,7 @@ const WALLPAPER_ABYSS: &str = "wall.alphacoders.com";
const ART_ABYSS: &str = "art.alphacoders.com";
const IMAGE_ABYSS: &str = "pics.alphacoders.com";
const ARTSTATION: &str = "www.artstation.com";
const DANBOORU: &str = "danbooru.donmai.us";

/// [Downloader] from URL
///
Expand All @@ -42,6 +44,7 @@ pub fn from_url(c: &Client, url: Url) -> DownloaderResult<Box<dyn Downloader>> {
ART_ABYSS => ok_box!(ArtAbyss::new(c, url)),
IMAGE_ABYSS => ok_box!(ImageAbyss::new(c, url)),
ARTSTATION => ok_box!(ArtStation::new(c, url)),
DANBOORU => ok_box!(Danbooru::new(c, url)),
_ => Err(DownloaderError::Other)
}
}
Expand Down
25 changes: 3 additions & 22 deletions src/downloaders/wallhaven.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,6 @@ use reqwest::blocking::Client;
use serde::Deserialize;
use url::Url;

#[derive(Deserialize, Clone)]
struct Tag {
category: String,
name: String,
alias: String,
}

impl From<Tag> for Vec<String> {
fn from(value: Tag) -> Vec<String> {
let mut tags = vec![value.category, value.name];
value.alias
.split(", ")
.for_each(|x| tags.push(x.to_string()));
tags
}
}

#[derive(Deserialize)]
struct WallhavenApi {
data: Wallhaven,
Expand All @@ -30,24 +13,22 @@ struct WallhavenApi {
/// Downloader designed for [Wallhaven](https://wallhaven.cc/)
#[derive(Deserialize)]
pub struct Wallhaven {
path: String,
path: Url,
id: String
}

#[allow(refining_impl_trait)]
impl Downloader for Wallhaven {
#[allow(refining_impl_trait)]
fn new(client: &Client, mut url: Url) -> DownloaderResult<Self> {
let api_path = format!("/api/v1{}", url.path());
url.set_path(&api_path);
let api_res = quick_get(client, url)?.json::<WallhavenApi>()?;
Ok(api_res.data)
}

fn image_id(&self) -> &str {
&self.id
}
fn image_url(&self) -> DownloaderResult<Urls> {
let url = Url::parse(&self.path)?;
Ok(url.into())
Ok(self.path.clone().into())
}
}
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn action(urls: Vec<Url>) -> ExitCode {
Ok(x) => x,
Err(err) => {
match err {
DownloaderError::Other => paint!(Colors::YellowBold, " Not Supported!"),
DownloaderError::Other => paintln!(Colors::YellowBold, " Not Supported!"),
_ => paintln!(Colors::RedBold, " Failed: {}{err}", Colors::Red)
};
continue;
Expand Down Expand Up @@ -135,9 +135,9 @@ fn download(client: &Client, url: Url, id: &str, host: &str, ctr: (bool, u8)) ->

// Build filename
let fname = if ctr.0 {
format!("{id}_{host}.{ext}")
format!("{host}_{id}.{ext}")
} else {
format!("{id}_{}_{host}.{ext}", ctr.1)
format!("{host}_{id}_{}.{ext}", ctr.1)
};

// Download file data
Expand Down
2 changes: 1 addition & 1 deletion src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub const APP_NAME: &str = env!("CARGO_PKG_NAME");
const APP_VERSION: &str = env!("CARGO_PKG_VERSION");
const APP_DESC: &str = env!("CARGO_PKG_DESCRIPTION");

// HTTP Constants
// User-Agent
pub const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), '/', env!("CARGO_PKG_VERSION"));

pub struct Info;
Expand Down

0 comments on commit bfe7e9a

Please sign in to comment.