Add importing functions to url type
Signed-off-by: trivernis <trivernis@protonmail.com>pull/1/head
parent
798e37141e
commit
fe01a4ae5c
@ -1,8 +1,40 @@
|
||||
use crate::endpoints::common::FileIdentifier;
|
||||
use crate::Client;
|
||||
|
||||
#[derive(Clone, Debug, PartialOrd, PartialEq)]
|
||||
pub enum FileStatus {
|
||||
ReadyForImport,
|
||||
InDatabase,
|
||||
Deleted,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl Eq for FileStatus {}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct HydrusFile {
|
||||
pub(crate) client: Client,
|
||||
pub id: FileIdentifier,
|
||||
pub status: FileStatus,
|
||||
}
|
||||
|
||||
impl HydrusFile {
|
||||
pub(crate) fn from_raw_status_and_hash<S: ToString>(
|
||||
client: Client,
|
||||
status: u8,
|
||||
hash: S,
|
||||
) -> Self {
|
||||
let status = if status == 3 {
|
||||
FileStatus::Deleted
|
||||
} else if status == 0 {
|
||||
FileStatus::ReadyForImport
|
||||
} else {
|
||||
FileStatus::InDatabase
|
||||
};
|
||||
Self {
|
||||
client,
|
||||
id: FileIdentifier::Hash(hash.to_string()),
|
||||
status,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,61 @@
|
||||
use crate::builders::import_builder::UrlImportBuilder;
|
||||
use crate::endpoints::adding_urls::{
|
||||
URL_TYPE_FILE, URL_TYPE_GALLERY, URL_TYPE_POST, URL_TYPE_WATCHABLE,
|
||||
};
|
||||
use crate::error::Result;
|
||||
use crate::hydrus_file::HydrusFile;
|
||||
use crate::Client;
|
||||
|
||||
#[derive(Clone, Debug, PartialOrd, PartialEq)]
|
||||
pub enum UrlType {
|
||||
Post,
|
||||
File,
|
||||
Gallery,
|
||||
Watchable,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl Eq for UrlType {}
|
||||
|
||||
impl From<u8> for UrlType {
|
||||
fn from(value: u8) -> Self {
|
||||
match value {
|
||||
v if v == URL_TYPE_POST => Self::Post,
|
||||
v if v == URL_TYPE_FILE => Self::File,
|
||||
v if v == URL_TYPE_GALLERY => Self::Gallery,
|
||||
v if v == URL_TYPE_WATCHABLE => Self::Watchable,
|
||||
_ => Self::Unknown,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Url {
|
||||
pub url: String,
|
||||
pub normalised_url: Option<String>,
|
||||
pub(crate) client: Client,
|
||||
pub normalised_url: String,
|
||||
pub url_type: UrlType,
|
||||
pub match_name: String,
|
||||
pub can_parse: bool,
|
||||
}
|
||||
|
||||
impl Url {
|
||||
/// Returns a list of files associated with the url
|
||||
pub async fn files(&mut self) -> Result<Vec<HydrusFile>> {
|
||||
let response = self.client.get_url_files(&self.url).await?;
|
||||
let files = response
|
||||
.url_file_statuses
|
||||
.into_iter()
|
||||
.map(|file| {
|
||||
HydrusFile::from_raw_status_and_hash(self.client.clone(), file.status, file.hash)
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(files)
|
||||
}
|
||||
|
||||
/// Creates an import builder for the url
|
||||
pub fn import(&mut self) -> UrlImportBuilder {
|
||||
UrlImportBuilder::new(self.client.clone(), &self.url)
|
||||
}
|
||||
}
|
||||
|
@ -1,2 +1,3 @@
|
||||
mod test_hydrus;
|
||||
mod test_import;
|
||||
mod test_url;
|
||||
|
@ -0,0 +1,25 @@
|
||||
use super::super::common;
|
||||
use hydrus_api::page::PageIdentifier;
|
||||
use hydrus_api::service::ServiceName;
|
||||
use hydrus_api::tag::Tag;
|
||||
use hydrus_api::url::Url;
|
||||
|
||||
async fn get_url() -> Url {
|
||||
let mut hydrus = common::get_hydrus();
|
||||
hydrus
|
||||
.url("https://www.pixiv.net/member_illust.php?illust_id=83406361&mode=medium")
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_imports() {
|
||||
let mut url = get_url().await;
|
||||
|
||||
url.import()
|
||||
.page(PageIdentifier::name("Rusty Import"))
|
||||
.add_additional_tag(ServiceName::my_tags(), Tag::from("character:megumin"))
|
||||
.run()
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
Loading…
Reference in New Issue