Skip to content

Commit

Permalink
Add tags to telegram push message
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc authored Sep 21, 2024
1 parent 0d548c0 commit 468be0c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/db/push_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ pub struct TelegramPushConfig {
pub allow_failed: bool,
/// Download media first and send media to telegram server directly.
pub download_media: Option<bool>,
/// Add pixiv tag link to tag
#[serde(default = "default_false")]
pub add_link_to_tag: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down
75 changes: 75 additions & 0 deletions src/server/push/task/pixiv_send_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,78 @@ impl RunContext {
}
}

pub fn add_tags_tg(&self, text: &mut String, cfg: &TelegramPushConfig) {
if cfg.add_ai_tag && self.is_ai() {
text.push_str("#");
text.push_str(&gettext("AI generated").replace(' ', "_"));
text.push_str(" ");
}
if let Some(i) = self.illust.as_ref() {
for tag in i.tags() {
if let Some(name) = tag.name() {
if cfg.add_link_to_tag {
text.push_str(&format!(
"<a href=\"https://www.pixiv.net/tags/{}\">#{}</a> ",
name, name
));
} else {
text.push('#');
text.push_str(&name.replace(' ', "_"));
text.push(' ');
}
if cfg.add_translated_tag {
if let Some(t) = tag.translated_name() {
if cfg.add_link_to_tag {
text.push_str(&format!(
"<a href=\"https://www.pixiv.net/tags/{}\">#{}</a> ",
name, t
));
} else {
text.push('#');
text.push_str(&t.replace(' ', "_"));
text.push(' ');
}
}
}
}
}
text.push_str("\n");
return;
}
if let Some(d) = self.data.as_ref() {
for tag in d["tags"]["tags"].members() {
if let Some(name) = &tag["tag"].as_str() {
if cfg.add_link_to_tag {
text.push_str(&format!(
"<a href=\"https://www.pixiv.net/tags/{}\">#{}</a> ",
name, name
));
} else {
text.push('#');
text.push_str(&name.replace(' ', "_"));
text.push(' ');
}
if self.add_translated_tag() {
if let Some(t) = &tag["translation"]["en"].as_str() {
if cfg.add_link_to_tag {
text.push_str(&format!(
"<a href=\"https://www.pixiv.net/tags/{}\">#{}</a> ",
name, t
));
} else {
text.push('#');
text.push_str(&t.replace(' ', "_"));
text.push(' ');
}
}
}
}
}
text.push_str("\n");
return;
}
}

pub async fn send_every_push(&self, cfg: &EveryPushConfig) -> Result<(), PixivDownloaderError> {
let client = EveryPushClient::new(&cfg.push_server);
match cfg.typ {
Expand Down Expand Up @@ -737,6 +809,9 @@ impl RunContext {
}
text += &convert_description_to_tg_html(self.desc().unwrap_or(""))?;
text.push('\n');
if cfg.add_tags {
self.add_tags_tg(&mut text, cfg);
}
if cfg.author_locations.contains(&AuthorLocation::Bottom) {
if let Some(a) = &author {
text.push_str(gettext("by "));
Expand Down

0 comments on commit 468be0c

Please sign in to comment.