WIP posts parameter
This commit is contained in:
parent
d7361ee5e5
commit
6ea49fc36a
7 changed files with 131 additions and 12 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
/target
|
||||
*.swp
|
||||
.idea
|
||||
|
|
32
Cargo.lock
generated
32
Cargo.lock
generated
|
@ -17,6 +17,12 @@ version = "1.0.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.77"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c9d19de80eff169429ac1e9f48fffb163916b448a44e8e046186232046d9e1f9"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
|
@ -521,7 +527,13 @@ dependencies = [
|
|||
name = "r621"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"base64",
|
||||
"reqwest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror",
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -735,6 +747,26 @@ dependencies = [
|
|||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.52"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "83a48fd946b02c0a526b2e9481c8e2a17755e47039164a86c4070446e3a4614d"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.52"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7fbe9b594d6568a6a1443250a7e67d80b74e1e96f6d1715e1e21cc1888291d3"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinyvec"
|
||||
version = "1.6.0"
|
||||
|
|
|
@ -6,4 +6,11 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
reqwest = "0.11.23"
|
||||
anyhow = "1.0.77"
|
||||
base64 = "0.21.5"
|
||||
reqwest = { version = "0.11.23", features = ["json"] }
|
||||
serde = { version = "1.0.193", features = ["derive", "std"] }
|
||||
serde_json = "1.0.108"
|
||||
thiserror = "1.0.52"
|
||||
url = "2.5.0"
|
||||
|
||||
|
|
|
@ -1,17 +1,68 @@
|
|||
#[derive(Clone, Copy, Debug)]
|
||||
use std::io::Write;
|
||||
use anyhow::Result;
|
||||
use base64::Engine;
|
||||
use base64::engine::{GeneralPurpose};
|
||||
use crate::post::{Post, Posts};
|
||||
use reqwest::header::{AUTHORIZATION, HeaderMap, HeaderValue, USER_AGENT};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Client<'a> {
|
||||
auth: Authentication<'a>,
|
||||
useragent: &'a str,
|
||||
url: &'static str
|
||||
host: &'static str,
|
||||
http_client: reqwest::Client
|
||||
}
|
||||
|
||||
impl<'a> Client<'a> {
|
||||
pub fn new(auth: Authentication<'a>, useragent: &'a str) -> Self {
|
||||
Client {
|
||||
pub fn new(auth: Authentication<'a>, useragent: &'a str) -> Result<Self> {
|
||||
let mut header_map = HeaderMap::new();
|
||||
header_map.append(USER_AGENT, HeaderValue::from_str(useragent)?);
|
||||
if let Authentication::Authorized { username, apikey } = auth {
|
||||
let authorization = Self::get_authorization_value(username, apikey);
|
||||
header_map.append(AUTHORIZATION, HeaderValue::from_str(&authorization)?);
|
||||
}
|
||||
|
||||
let http_client = reqwest::Client::builder()
|
||||
.default_headers(header_map)
|
||||
.build()?;
|
||||
|
||||
Ok(Client {
|
||||
auth,
|
||||
useragent,
|
||||
url: "https://e621.net"
|
||||
host: "https://e621.net",
|
||||
http_client
|
||||
})
|
||||
}
|
||||
|
||||
fn get_authorization_value(username: &'a str, apikey: &'a str) -> String {
|
||||
let base64_engine = GeneralPurpose::new(&base64::alphabet::STANDARD, Default::default());
|
||||
base64_engine.encode(format!("{username}:{apikey}"))
|
||||
}
|
||||
|
||||
pub async fn list_posts(&mut self, limit: Option<u16>, tags: Option<Vec<String>>, page: Option<u32>) -> Result<Vec<Post>> {
|
||||
let mut url = url::Url::parse(format!("{}/posts.json", self.host).as_str())?;
|
||||
let limit = limit.unwrap_or(320);
|
||||
let page = page.unwrap_or(0);
|
||||
|
||||
let mut query_params = String::new();
|
||||
query_params.push_str("limit=");
|
||||
query_params.push_str(limit.to_string().as_str());
|
||||
|
||||
query_params.push_str("page=");
|
||||
query_params.push_str(page.to_string().as_str());
|
||||
|
||||
if let Some(tags) = tags {
|
||||
if tags.len() > 0 {
|
||||
query_params.push_str("tags=");
|
||||
query_params.push_str(tags.join(",").as_str());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(self.http_client.get(url.as_str())
|
||||
.send()
|
||||
.await?
|
||||
.json::<Posts>()
|
||||
.await?.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
3
src/errors.rs
Normal file
3
src/errors.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
enum Error {
|
||||
|
||||
}
|
|
@ -1,2 +1,3 @@
|
|||
mod client;
|
||||
mod post;
|
||||
pub mod client;
|
||||
pub mod post;
|
||||
mod errors;
|
||||
|
|
30
src/post.rs
30
src/post.rs
|
@ -2,6 +2,20 @@ pub type Id = u64;
|
|||
pub type FileSize = u64;
|
||||
pub type ImageDimension = u64;
|
||||
|
||||
#[derive(serde::Deserialize, Clone, Debug)]
|
||||
pub struct Posts {
|
||||
#[serde(alias = "posts")]
|
||||
inner: Vec<Post>
|
||||
}
|
||||
|
||||
impl Into<Vec<Post>> for Posts {
|
||||
fn into(self) -> Vec<Post> {
|
||||
self.inner
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(serde::Deserialize, Clone, Debug)]
|
||||
pub struct Post {
|
||||
id: Id,
|
||||
created_at: String,
|
||||
|
@ -11,15 +25,17 @@ pub struct Post {
|
|||
rating: String,
|
||||
fav_count: u16,
|
||||
sources: Vec<String>,
|
||||
approver_id: Id,
|
||||
uploader_ud: Id,
|
||||
approver_id: Option<Id>,
|
||||
uploader_id: Id,
|
||||
description: String,
|
||||
comment_count: u16,
|
||||
has_notes: bool,
|
||||
is_favorited: bool,
|
||||
change_seq: u128
|
||||
change_seq: Option<u128>,
|
||||
relationships: PostRelationships
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, Clone, Debug)]
|
||||
pub struct PostTags {
|
||||
general: Vec<String>,
|
||||
artist: Vec<String>,
|
||||
|
@ -30,6 +46,7 @@ pub struct PostTags {
|
|||
lore: Vec<String>
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, Clone, Debug)]
|
||||
pub struct PostFile {
|
||||
width: ImageDimension,
|
||||
height: ImageDimension,
|
||||
|
@ -38,3 +55,10 @@ pub struct PostFile {
|
|||
md5: String,
|
||||
url: String
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, Clone, Debug)]
|
||||
pub struct PostRelationships {
|
||||
parent_id: Option<Id>,
|
||||
has_children: bool,
|
||||
has_active_children: bool,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue