e62rs 1.5.0

An in-terminal E621/926 browser.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
//! All data types used by e621 for deserializing/serializing API responses.
//! (documentation for all models was AI generated)
use {
    crate::utils::{deserialize_bool_from_str, deserialize_post_ids},
    hashbrown::{HashMap, HashSet},
    serde::{Deserialize, Serialize},
};

/// Response from e621 API containing multiple posts, typically from a search or listing endpoint.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct E6PostsResponse {
    /// List of posts returned by the API.
    #[serde(default)]
    pub posts: Vec<E6Post>,
}

/// Response from e621 API containing a single post.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct E6PostResponse {
    /// The post object returned by the API.
    #[serde(default)]
    pub post: E6Post,
}

/// Represents a complete e621 post with all associated metadata.
/// This is the main data structure for individual posts on e621.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct E6Post {
    /// Unique identifier for the post.
    #[serde(default)]
    pub id: i64,
    /// ISO 8601 timestamp of when the post was created.
    #[serde(default)]
    pub created_at: String,
    /// ISO 8601 timestamp of when the post was last updated.
    #[serde(default)]
    pub updated_at: String,
    /// Information about the original uploaded file.
    #[serde(default)]
    pub file: File,
    /// Information about the preview/thumbnail image.
    #[serde(default)]
    pub preview: Preview,
    /// Information about the sample/resized version (if available).
    #[serde(default)]
    pub sample: Sample,
    /// Vote score statistics for the post.
    #[serde(default)]
    pub score: Score,
    /// Categorized tags associated with the post.
    #[serde(default)]
    pub tags: Tags,
    /// Tags that are locked and cannot be removed from the post.
    #[serde(default)]
    pub locked_tags: Vec<String>,
    /// Sequence number indicating the revision of the post (used for change tracking).
    #[serde(default)]
    pub change_seq: i64,
    /// Various status flags for the post (pending, flagged, deleted, etc.).
    #[serde(default)]
    pub flags: Flags,
    /// Content rating: "s" (safe), "q" (questionable), or "e" (explicit).
    #[serde(default)]
    pub rating: String,
    /// Number of users who have favorited this post.
    #[serde(default)]
    pub fav_count: i64,
    /// List of source URLs where the content originated from.
    #[serde(default)]
    pub sources: Vec<String>,
    /// IDs of pools that this post belongs to.
    #[serde(default)]
    pub pools: Vec<i64>,
    /// Parent/child relationship data for post hierarchies.
    #[serde(default)]
    pub relationships: Relationships,
    /// ID of the user who approved the post (if applicable).
    #[serde(default)]
    pub approver_id: Option<i64>,
    /// ID of the user who uploaded the post.
    #[serde(default)]
    pub uploader_id: i64,
    /// Username of the user who uploaded the post.
    #[serde(default)]
    pub uploader_name: String,
    /// Description or commentary for the post (may contain HTML).
    #[serde(default)]
    pub description: String,
    /// Number of comments on the post.
    #[serde(default)]
    pub comment_count: i64,
    /// Whether the currently authenticated user has favorited this post.
    #[serde(default)]
    pub is_favorited: bool,
    /// Whether the post has annotations/notes.
    #[serde(default)]
    pub has_notes: bool,
    /// Duration in seconds for video/audio posts (null for static images).
    #[serde(default)]
    pub duration: Option<f64>,
}

/// Contains metadata about the original uploaded file.
#[derive(Deserialize, Serialize, Debug, Clone, Default, PartialEq)]
pub struct File {
    /// Width of the original file in pixels.
    #[serde(default)]
    pub width: i64,
    /// Height of the original file in pixels.
    #[serde(default)]
    pub height: i64,
    /// File extension (e.g., "jpg", "png", "webm", "mp4").
    #[serde(default)]
    pub ext: String,
    /// File size in bytes.
    #[serde(default)]
    pub size: i64,
    /// MD5 hash of the file.
    #[serde(default)]
    pub md5: String,
    /// Direct URL to the original file (null if not available or requires higher privileges).
    #[serde(default)]
    pub url: Option<String>,
}

/// Contains metadata about the preview/thumbnail image.
#[derive(Deserialize, Serialize, Debug, Clone, Default, PartialEq)]
pub struct Preview {
    /// Width of the preview image in pixels.
    #[serde(default)]
    pub width: i64,
    /// Height of the preview image in pixels.
    #[serde(default)]
    pub height: i64,
    /// Direct URL to the preview image.
    #[serde(default)]
    pub url: Option<String>,
    /// Alt text for the preview image (typically null).
    #[serde(default)]
    pub alt: Option<String>,
}

/// Contains metadata about the sample/resized version of the post.
/// Samples are larger than previews but smaller than the original file.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Sample {
    /// Whether a sample version is available for this post.
    #[serde(default)]
    pub has: bool,
    /// Width of the sample image in pixels.
    #[serde(default)]
    pub width: i64,
    /// Height of the sample image in pixels.
    #[serde(default)]
    pub height: i64,
    /// Direct URL to the sample image.
    #[serde(default)]
    pub url: Option<String>,
    /// Alt text for the sample image (typically null).
    #[serde(default)]
    pub alt: Option<String>,
    /// Alternate versions and qualities available (primarily for video posts).
    #[serde(default)]
    pub alternates: Alternates,
}

/// Contains alternate versions of the post, such as original video files and different quality levels.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Alternates {
    /// Whether alternate versions are available.
    #[serde(default)]
    pub has: bool,
    /// Original video file information (for video posts).
    #[serde(default)]
    pub original: Option<Original>,
    /// Video variants in different formats (e.g., MP4).
    #[serde(default)]
    pub variants: Option<Variants>,
    /// Sample versions at different quality levels.
    #[serde(default)]
    pub samples: Option<Samples>,
}

/// Metadata for the original video file (when different from the main file).
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Original {
    /// Frames per second of the video.
    #[serde(default)]
    pub fps: f64,
    /// Video codec used (e.g., "vp8", "vp9", "h264").
    #[serde(default)]
    pub codec: String,
    /// File size in bytes.
    #[serde(default)]
    pub size: i64,
    /// Width in pixels.
    #[serde(default)]
    pub width: i64,
    /// Height in pixels.
    #[serde(default)]
    pub height: i64,
    /// Direct URL to the original video file.
    #[serde(default)]
    pub url: Option<String>,
}

/// Contains video variants in different formats.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Variants {
    /// MP4 variant of the video.
    #[serde(default)]
    pub mp4: Option<Mp4>,
}

/// Metadata for an MP4 variant of a video post.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Mp4 {
    /// Video codec used (e.g., "h264").
    #[serde(default)]
    pub codec: String,
    /// Frames per second.
    #[serde(default)]
    pub fps: f64,
    /// File size in bytes.
    #[serde(default)]
    pub size: i64,
    /// Width in pixels.
    #[serde(default)]
    pub width: i64,
    /// Height in pixels.
    #[serde(default)]
    pub height: i64,
    /// Direct URL to the MP4 file.
    #[serde(default)]
    pub url: Option<String>,
}

/// Map of sample quality names (e.g., "720p", "1080p") to their metadata.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Samples(pub HashMap<String, Quality>);

/// Metadata for a specific quality level of a sample.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Quality {
    /// Frames per second (for video samples).
    #[serde(default)]
    pub fps: f64,
    /// File size in bytes.
    #[serde(default)]
    pub size: i64,
    /// Video codec used.
    #[serde(default)]
    pub codec: String,
    /// Width in pixels.
    #[serde(default)]
    pub width: i64,
    /// Height in pixels.
    #[serde(default)]
    pub height: i64,
    /// Direct URL to the sample of this quality.
    #[serde(default)]
    pub url: Option<String>,
}

/// Contains vote score statistics for a post.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Score {
    /// Number of upvotes.
    #[serde(default)]
    pub up: i64,
    /// Number of downvotes.
    #[serde(default)]
    pub down: i64,
    /// Total score (upvotes minus downvotes).
    #[serde(default)]
    pub total: i64,
}

/// Contains all tags for a post, organized by category.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Tags {
    /// General descriptive tags.
    #[serde(default)]
    pub general: Vec<String>,
    /// Artist tags (creators of the content).
    #[serde(default)]
    pub artist: Vec<String>,
    /// Contributor tags (users who uploaded or edited the post).
    #[serde(default)]
    pub contributor: Vec<String>,
    /// Copyright tags (franchises, series, or original works).
    #[serde(default)]
    pub copyright: Vec<String>,
    /// Character tags (names of characters depicted).
    #[serde(default)]
    pub character: Vec<String>,
    /// Species tags (for furry content, the species of characters).
    #[serde(default)]
    pub species: Vec<String>,
    /// Invalid or deprecated tags that need to be corrected.
    #[serde(default)]
    pub invalid: Vec<String>,
    /// Meta tags (tags about the post itself, like "comic" or "high_res").
    #[serde(default)]
    pub meta: Vec<String>,
    /// Lore tags for background information and worldbuilding.
    #[serde(default)]
    pub lore: Vec<String>,
}

/// Contains various status flags for a post.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Flags {
    /// Whether the post is pending approval.
    #[serde(default)]
    pub pending: bool,
    /// Whether the post has been flagged for review.
    #[serde(default)]
    pub flagged: bool,
    /// Whether notes are locked (cannot be edited).
    #[serde(default)]
    pub note_locked: bool,
    /// Whether the status is locked (cannot be changed).
    #[serde(default)]
    pub status_locked: bool,
    /// Whether the rating is locked (cannot be changed).
    #[serde(default)]
    pub rating_locked: bool,
    /// Whether the post has been deleted.
    #[serde(default)]
    pub deleted: bool,
}

/// Contains parent/child relationship data for post hierarchies.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Relationships {
    /// ID of the parent post (if this is a child post).
    #[serde(default)]
    pub parent_id: Option<i64>,
    /// Whether this post has any child posts.
    #[serde(default)]
    pub has_children: bool,
    /// Whether this post has active (non-deleted) child posts.
    #[serde(default)]
    pub has_active_children: bool,
    /// IDs of child posts (if any).
    #[serde(default)]
    pub children: Option<Vec<i64>>,
}

/// Represents a tag entry from e621's tag database.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TagEntry {
    /// Unique identifier for the tag.
    #[serde(default)]
    pub id: i64,
    /// The tag name.
    #[serde(default)]
    pub name: String,
    /// Category ID for the tag (0=general, 1=artist, 3=copyright, 4=character, 5=species, etc.).
    #[serde(default)]
    pub category: i64,
    /// Number of posts that have this tag.
    #[serde(default)]
    pub post_count: i64,
}

/// Response from e621 API containing multiple pools.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct E6PoolsResponse {
    /// List of pools returned by the API.
    #[serde(default)]
    pub pools: Vec<E6Pool>,
}

/// Response from e621 API containing a single pool.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct E6PoolResponse {
    /// The pool object returned by the API.
    #[serde(default)]
    pub pool: E6Pool,
}

/// Represents a pool (collection) of posts on e621.
/// Pools are used to group related posts, such as comic pages or themed collections.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct E6Pool {
    /// Unique identifier for the pool.
    #[serde(default)]
    pub id: i64,
    /// Name of the pool.
    #[serde(default)]
    pub name: String,
    /// ISO 8601 timestamp of when the pool was created.
    #[serde(default)]
    pub created_at: String,
    /// ISO 8601 timestamp of when the pool was last updated.
    #[serde(default)]
    pub updated_at: String,
    /// ID of the user who created the pool.
    #[serde(default)]
    pub creator_id: i64,
    /// Username of the user who created the pool.
    #[serde(default)]
    pub creator_name: String,
    /// Description of the pool's purpose or contents.
    #[serde(default)]
    pub description: String,
    /// Whether the pool is active (not deleted).
    #[serde(default)]
    pub is_active: bool,
    /// Category of the pool ("series", "collection", etc.).
    #[serde(default)]
    pub category: String,
    /// IDs of posts in the pool, in order.
    #[serde(default)]
    pub post_ids: Vec<i64>,
    /// Number of posts in the pool.
    #[serde(default)]
    pub post_count: i64,
}

/// Represents a pool entry, typically used in search results or listings.
/// Similar to E6Pool but with slightly different field conventions.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PoolEntry {
    /// Unique identifier for the pool.
    #[serde(default)]
    pub id: i64,
    /// Name of the pool.
    #[serde(default)]
    pub name: String,
    /// ISO 8601 timestamp of when the pool was created.
    #[serde(default)]
    pub created_at: String,
    /// ISO 8601 timestamp of when the pool was last updated.
    #[serde(default)]
    pub updated_at: String,
    /// ID of the user who created the pool.
    #[serde(default)]
    pub creator_id: i64,
    /// Description of the pool's purpose or contents.
    #[serde(default)]
    pub description: String,
    /// Whether the pool is active (not deleted).
    #[serde(default, deserialize_with = "deserialize_bool_from_str")]
    pub is_active: bool,
    /// Category of the pool ("series", "collection", etc.).
    #[serde(default)]
    pub category: String,
    /// IDs of posts in the pool, in order.
    #[serde(default, deserialize_with = "deserialize_post_ids")]
    pub post_ids: Vec<i64>,
}

/// Represents a tag alias entry from e621.
/// Tag aliases automatically rename one tag to another.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TagAliasEntry {
    /// Unique identifier for the alias.
    #[serde(default)]
    pub id: i64,
    /// The source tag name that will be aliased.
    #[serde(default)]
    pub antecedent_name: String,
    /// The destination tag name that the antecedent will be aliased to.
    #[serde(default)]
    pub consequent_name: String,
    /// ISO 8601 timestamp of when the alias was created.
    #[serde(default)]
    pub created_at: String,
    /// Status of the alias ("active", "pending", etc.).
    #[serde(default)]
    pub status: String,
}

/// Represents a tag implication entry from e621.
/// Tag implications automatically add additional tags when a tag is used.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TagImplicationEntry {
    /// Unique identifier for the implication.
    #[serde(default)]
    pub id: i64,
    /// The source tag name that implies other tags.
    #[serde(default)]
    pub antecedent_name: String,
    /// The destination tag name that is implied.
    #[serde(default)]
    pub consequent_name: String,
    /// ISO 8601 timestamp of when the implication was created.
    #[serde(default)]
    pub created_at: String,
    /// Status of the implication ("active", "pending", etc.).
    #[serde(default)]
    pub status: String,
}

impl E6Post {
    /// parses a blacklist rule string into included, excluded, and wildcard tags
    ///
    /// # Arguments
    ///
    /// * `rule` - the rule to parse
    pub fn parse_blacklist_rule(rule: &str) -> (Vec<String>, Vec<String>) {
        let mut includes = Vec::new();
        let mut excludes = Vec::new();

        for part in rule.split_whitespace() {
            if let Some(tag) = part.strip_prefix('-') {
                if !tag.is_empty() {
                    excludes.push(tag.to_string());
                }
            } else if !part.is_empty() {
                includes.push(part.to_string());
            }
        }

        (includes, excludes)
    }

    /// checks if this post matches a given blacklist rule
    ///
    /// # Arguments
    ///
    /// * `rule` - check if a given post matches a rule in the blacklist
    pub fn matches_blacklist_rule(&self, rule: &str) -> bool {
        let (includes, excludes) = Self::parse_blacklist_rule(rule);

        let all: HashSet<&String> = self
            .tags
            .general
            .iter()
            .chain(self.tags.artist.iter())
            .chain(self.tags.contributor.iter())
            .chain(self.tags.copyright.iter())
            .chain(self.tags.character.iter())
            .chain(self.tags.species.iter())
            .chain(self.tags.meta.iter())
            .chain(self.tags.lore.iter())
            .collect();

        let includes_matched = includes.is_empty() || includes.iter().all(|tag| all.contains(tag));
        let excludes_matched = excludes.iter().any(|tag| all.contains(tag));

        if includes.is_empty() && !excludes.is_empty() {
            !excludes_matched
        } else {
            includes_matched && !excludes_matched
        }
    }

    /// checks if this post is blacklisted against the given blacklist rules
    ///
    /// # Arguments
    ///
    /// * `blacklist` - the blacklist rules to check against
    pub fn is_blacklisted_by(&self, blacklist: &[String]) -> bool {
        if blacklist.is_empty() {
            return false;
        }

        for rule in blacklist {
            if self.matches_blacklist_rule(rule) {
                return true;
            }
        }

        false
    }

    /// checks if this post is blacklisted using the configured blacklist
    #[cfg(feature = "cli")]
    pub fn is_blacklisted(&self) -> bool {
        self.is_blacklisted_by(&crate::getopt!(search.blacklist))
    }

    /// checks if any of the search tags are blacklisted
    ///
    /// # Arguments
    ///
    /// * `search_tags` - a list of tags to compare to the blacklist
    /// * `blacklist` - the blacklist rules to check against
    pub fn search_includes_blacklisted_by(search_tags: &[String], blacklist: &[String]) -> bool {
        for search_tag in search_tags {
            for rule in blacklist {
                let (include_tags, exclude_tags) = Self::parse_blacklist_rule(rule);
                if include_tags.len() == 1
                    && exclude_tags.is_empty()
                    && include_tags[0] == *search_tag
                {
                    return true;
                }
            }
        }

        false
    }

    /// checks if any of the search tags are blacklisted using the configured blacklist
    #[cfg(feature = "cli")]
    pub fn search_includes_blacklisted(search_tags: &[String]) -> bool {
        Self::search_includes_blacklisted_by(search_tags, &crate::getopt!(search.blacklist))
    }

    /// returns whether the post meets the given score requirements
    ///
    /// # Arguments
    ///
    /// * `min_score` - the minimum score threshold
    /// * `max_score` - the maximum score threshold
    pub fn meets_score_requirements_with(&self, min_score: i64, max_score: i64) -> bool {
        self.score.total >= min_score && self.score.total <= max_score
    }

    /// returns whether the post meets the configured score requirements
    #[cfg(feature = "cli")]
    pub fn meets_score_requirements(&self) -> bool {
        self.meets_score_requirements_with(
            crate::getopt!(search.min_post_score),
            crate::getopt!(search.max_post_score),
        )
    }
}

impl E6PostsResponse {
    /// filter blacklisted posts from the api response unless explicitly searched for
    ///
    /// # Arguments
    ///
    /// * `search_tags` - the tags included in the search
    /// * `blacklist` - the blacklist rules to check against
    pub fn filter_blacklisted_by(mut self, search_tags: &[String], blacklist: &[String]) -> Self {
        if E6Post::search_includes_blacklisted_by(search_tags, blacklist) {
            return self;
        }

        self.posts.retain(|post| !post.is_blacklisted_by(blacklist));
        self
    }

    /// filter blacklisted posts using the configured blacklist
    #[cfg(feature = "cli")]
    pub fn filter_blacklisted(self, search_tags: &[String]) -> Self {
        self.filter_blacklisted_by(search_tags, &crate::getopt!(search.blacklist))
    }

    /// filters out posts that don't meet the given score requirements
    ///
    /// # Arguments
    ///
    /// * `min_score` - the minimum score threshold
    /// * `max_score` - the maximum score threshold
    pub fn filter_score_with(mut self, min_score: i64, max_score: i64) -> Self {
        self.posts.retain(|post| post.meets_score_requirements_with(min_score, max_score));
        self
    }

    /// filters out posts that don't meet the configured score requirements
    #[cfg(feature = "cli")]
    pub fn filter_score(self) -> Self {
        self.filter_score_with(
            crate::getopt!(search.min_post_score),
            crate::getopt!(search.max_post_score),
        )
    }
}