a bot to post lewd lucario pics to mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

e621_mod.rs 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. use std::error::Error;
  2. use e621::{response::E621Post, E621Client};
  3. use e621_tags::{
  4. order::{Order, OrderKind},
  5. post::Post,
  6. range::Range,
  7. tags::Tags,
  8. };
  9. use super::State;
  10. pub(crate) fn get_newer_posts(
  11. state: &State,
  12. most_recent_post: i64,
  13. ) -> Result<Vec<E621Post>, Box<Error>> {
  14. get_posts(
  15. &state.e621,
  16. state
  17. .tags
  18. .clone()
  19. .add_tag(Post::Id(Range::GreaterThan(most_recent_post))),
  20. |post_id| most_recent_post < post_id,
  21. )
  22. }
  23. pub(crate) fn get_earlier_posts(
  24. state: &State,
  25. least_recent_post: i64,
  26. ) -> Result<Vec<E621Post>, Box<Error>> {
  27. get_posts(
  28. &state.e621,
  29. state
  30. .tags
  31. .clone()
  32. .add_tag(Post::Id(Range::LessThan(least_recent_post))),
  33. |post_id| least_recent_post > post_id,
  34. )
  35. }
  36. pub(crate) fn get_newest_posts(state: &State) -> Result<Vec<E621Post>, Box<Error>> {
  37. get_posts(
  38. &state.e621,
  39. state.tags.clone().add_tag(Order::Descending(OrderKind::Id)),
  40. |_| true,
  41. )
  42. }
  43. pub(crate) fn get_post_by_id(state: &State, id: i64) -> Result<Option<E621Post>, Box<Error>> {
  44. info!("Getting post {}", id);
  45. let tags = Tags::new()
  46. .add_tag(Post::Id(Range::Exactly(id)))
  47. .to_string();
  48. let posts = state.e621.list(tags, 1)?;
  49. Ok(posts.into_iter().next())
  50. }
  51. fn get_posts(
  52. e621_client: &E621Client,
  53. tags: impl ToString,
  54. compare: impl Fn(i64) -> bool,
  55. ) -> Result<Vec<E621Post>, Box<Error>> {
  56. let posts = e621_client.list(tags.to_string(), 50)?;
  57. let posts = posts
  58. .into_iter()
  59. .filter(|post| compare(post.id))
  60. .filter(|post| meets_criteria(&post))
  61. .collect();
  62. Ok(posts)
  63. }
  64. pub(crate) fn meets_criteria(post: &E621Post) -> bool {
  65. let filesize = if post.file_ext == "webm" {
  66. post.file_size < 20 * 1028 * 1028
  67. } else {
  68. post.file_size < 8 * 1028 * 1028
  69. };
  70. filesize && {
  71. let tags = post.tags.split(" ").collect::<Vec<_>>();
  72. !tags.contains(&"child") && !tags.contains(&"loli") && !tags.contains(&"cub")
  73. }
  74. }