Change tag import wrapper to use service identifiers instead of names

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/9/head
trivernis 3 years ago
parent 53f29a33d5
commit 903acc8845
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -49,13 +49,13 @@ async fn main() {
.run().await.unwrap(); .run().await.unwrap();
for mut file in files { for mut file in files {
file.add_tags(ServiceName::my_tags(), vec![Tag::from("ark mage")]).await.unwrap(); file.add_tags(ServiceName::my_tags().into(), vec![Tag::from("ark mage")]).await.unwrap();
} }
let url = hydrus.import() let url = hydrus.import()
.url("https://www.pixiv.net/member_illust.php?illust_id=83406361&mode=medium") .url("https://www.pixiv.net/member_illust.php?illust_id=83406361&mode=medium")
.page(PageIdentifier::name("My Import Page")) .page(PageIdentifier::name("My Import Page"))
.add_additional_tag(ServiceName::my_tags(), Tag::from("character:megumin")) .add_additional_tag(ServiceName::my_tags().into(), Tag::from("character:megumin"))
.show_page(true) .show_page(true)
.run().await.unwrap(); .run().await.unwrap();
} }
@ -67,6 +67,7 @@ async fn main() {
use hydrus_api::Client; use hydrus_api::Client;
use hydrus_api::paths::adding_tags::{AddTagsRequestBuilder, TagAction}; use hydrus_api::paths::adding_tags::{AddTagsRequestBuilder, TagAction};
use std::env; use std::env;
use hydrus_api::api_core::common::ServiceIdentifier;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
@ -81,9 +82,9 @@ async fn main() {
let request = AddTagsRequestBuilder::default() let request = AddTagsRequestBuilder::default()
.add_hash(hash) .add_hash(hash)
// for each tag the service has to be specified // for each tag the service has to be specified
.add_tags("my tags", vec!["beach".into(), "summer".into()]) .add_tags(ServiceIdentifier::name("my tags"), vec!["beach".into(), "summer".into()])
// with tag actions tags can also be removed. It's especially useful for the PTR // with tag actions tags can also be removed. It's especially useful for the PTR
.add_tag_with_action("my tags", "rain", TagAction::DeleteFromLocalService) .add_tag_with_action(ServiceIdentifier::name("my tags"), "rain", TagAction::DeleteFromLocalService)
.build(); .build();
client.add_tags(request).await.unwrap(); client.add_tags(request).await.unwrap();

@ -6,7 +6,7 @@
//! ## Hydrus Usage Example //! ## Hydrus Usage Example
//! //!
//! ``` //! ```
//! # use hydrus_api::{Hydrus, Client}; //! use hydrus_api::{Hydrus, Client};
//! use std::env; //! use std::env;
//! use hydrus_api::wrapper::tag::Tag; //! use hydrus_api::wrapper::tag::Tag;
//! use hydrus_api::wrapper::service::ServiceName; //! use hydrus_api::wrapper::service::ServiceName;
@ -37,13 +37,13 @@
//! .run().await.unwrap(); //! .run().await.unwrap();
//! //!
//! for mut file in files { //! for mut file in files {
//! file.add_tags(ServiceName::my_tags(), vec![Tag::from("ark mage")]).await.unwrap(); //! file.add_tags(ServiceName::my_tags().into(), vec![Tag::from("ark mage")]).await.unwrap();
//! } //! }
//! //!
//! let url = hydrus.import() //! let url = hydrus.import()
//! .url("https://www.pixiv.net/member_illust.php?illust_id=83406361&mode=medium") //! .url("https://www.pixiv.net/member_illust.php?illust_id=83406361&mode=medium")
//! .page(PageIdentifier::name("My Import Page")) //! .page(PageIdentifier::name("My Import Page"))
//! .add_additional_tag(ServiceName::my_tags(), Tag::from("character:megumin")) //! .add_additional_tag(ServiceName::my_tags().into(), Tag::from("character:megumin"))
//! .show_page(true) //! .show_page(true)
//! .run().await.unwrap(); //! .run().await.unwrap();
//! # } //! # }
@ -54,6 +54,7 @@
//! use hydrus_api::Client; //! use hydrus_api::Client;
//! use hydrus_api::api_core::adding_tags::{AddTagsRequestBuilder, TagAction}; //! use hydrus_api::api_core::adding_tags::{AddTagsRequestBuilder, TagAction};
//! use std::env; //! use std::env;
//! use hydrus_api::api_core::common::ServiceIdentifier;
//! # #[tokio::test] //! # #[tokio::test]
//! # async fn doctest() { //! # async fn doctest() {
//! //!
@ -68,9 +69,9 @@
//! let request = AddTagsRequestBuilder::default() //! let request = AddTagsRequestBuilder::default()
//! .add_hash(hash) //! .add_hash(hash)
//! // for each tag the service has to be specified //! // for each tag the service has to be specified
//! .add_tags("my tags", vec!["beach".into(), "summer".into()]) //! .add_tags(ServiceIdentifier::name("my tags"), vec!["beach".into(), "summer".into()])
//! // with tag actions tags can also be removed. It's especially useful for the PTR //! // with tag actions tags can also be removed. It's especially useful for the PTR
//! .add_tag_with_action("my tags", "rain", TagAction::DeleteFromLocalService) //! .add_tag_with_action(ServiceIdentifier::name("my tags"), "rain", TagAction::DeleteFromLocalService)
//! .build(); //! .build();
//! //!
//! client.add_tags(request).await.unwrap(); //! client.add_tags(request).await.unwrap();

@ -5,7 +5,6 @@ use crate::error::{Error, Result};
use crate::utils::tag_list_to_string_list; use crate::utils::tag_list_to_string_list;
use crate::wrapper::hydrus_file::HydrusFile; use crate::wrapper::hydrus_file::HydrusFile;
use crate::wrapper::page::PageIdentifier; use crate::wrapper::page::PageIdentifier;
use crate::wrapper::service::ServiceName;
use crate::wrapper::tag::Tag; use crate::wrapper::tag::Tag;
use crate::wrapper::url::Url; use crate::wrapper::url::Url;
use crate::Client; use crate::Client;
@ -78,7 +77,7 @@ pub struct UrlImportBuilder {
page: Option<PageIdentifier>, page: Option<PageIdentifier>,
show_page: bool, show_page: bool,
filter_tags: Vec<Tag>, filter_tags: Vec<Tag>,
service_tag_mappings: HashMap<String, Vec<Tag>>, service_tag_mappings: HashMap<ServiceIdentifier, Vec<Tag>>,
} }
impl UrlImportBuilder { impl UrlImportBuilder {
@ -122,16 +121,16 @@ impl UrlImportBuilder {
} }
/// Adds an additional tag for the imported file /// Adds an additional tag for the imported file
pub fn add_additional_tag(self, service: ServiceName, tag: Tag) -> Self { pub fn add_additional_tag(self, service: ServiceIdentifier, tag: Tag) -> Self {
self.add_additional_tags(service, vec![tag]) self.add_additional_tags(service, vec![tag])
} }
/// Adds multiple additional tags for the import /// Adds multiple additional tags for the import
pub fn add_additional_tags(mut self, service: ServiceName, mut tags: Vec<Tag>) -> Self { pub fn add_additional_tags(mut self, service: ServiceIdentifier, mut tags: Vec<Tag>) -> Self {
if let Some(service_tags) = self.service_tag_mappings.get_mut(&service.0) { if let Some(service_tags) = self.service_tag_mappings.get_mut(&service) {
service_tags.append(&mut tags); service_tags.append(&mut tags);
} else { } else {
self.service_tag_mappings.insert(service.0, tags); self.service_tag_mappings.insert(service, tags);
} }
self self
@ -142,10 +141,7 @@ impl UrlImportBuilder {
let mut request = AddUrlRequestBuilder::default().url(&self.url); let mut request = AddUrlRequestBuilder::default().url(&self.url);
for (service, tags) in self.service_tag_mappings { for (service, tags) in self.service_tag_mappings {
request = request.add_tags( request = request.add_tags(service, tag_list_to_string_list(tags));
ServiceIdentifier::name(service),
tag_list_to_string_list(tags),
);
} }
request = request.add_filter_tags(tag_list_to_string_list(self.filter_tags)); request = request.add_filter_tags(tag_list_to_string_list(self.filter_tags));
if let Some(page) = self.page { if let Some(page) = self.page {

@ -212,6 +212,7 @@ impl HydrusFile {
Ok(naive_time_imported) Ok(naive_time_imported)
} }
/// Returns the time the file was deleted for a specified file service
pub async fn time_deleted<S: AsRef<str>>( pub async fn time_deleted<S: AsRef<str>>(
&mut self, &mut self,
service_key: S, service_key: S,

@ -39,8 +39,11 @@ async fn it_imports_urls() {
.url("https://www.pixiv.net/member_illust.php?illust_id=83406361&mode=medium") .url("https://www.pixiv.net/member_illust.php?illust_id=83406361&mode=medium")
.page(PageIdentifier::name("Rusty Import")) .page(PageIdentifier::name("Rusty Import"))
.show_page(true) .show_page(true)
.add_additional_tag(ServiceName::my_tags(), Tag::from("ark mage")) .add_additional_tag(ServiceName::my_tags().into(), Tag::from("ark mage"))
.add_additional_tag(ServiceName::my_tags(), Tag::from("character:megumin")) .add_additional_tag(
ServiceName::my_tags().into(),
Tag::from("character:megumin"),
)
.run() .run()
.await .await
.unwrap(); .unwrap();

@ -18,7 +18,10 @@ async fn it_imports() {
url.import() url.import()
.page(PageIdentifier::name("Rusty Import")) .page(PageIdentifier::name("Rusty Import"))
.add_additional_tag(ServiceName::my_tags(), Tag::from("character:megumin")) .add_additional_tag(
ServiceName::my_tags().into(),
Tag::from("character:megumin"),
)
.run() .run()
.await .await
.unwrap(); .unwrap();

Loading…
Cancel
Save