| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2026 Mike Fährmann |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License version 2 as |
| 7 |
# published by the Free Software Foundation. |
| 8 |
|
| 9 |
"""Extractors for https://anime-pictures.net/""" |
| 10 |
|
| 11 |
from . import booru |
| 12 |
from .. import text |
| 13 |
import collections |
| 14 |
|
| 15 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?anime\-pictures\.net" |
| 16 |
|
| 17 |
|
| 18 |
class AnimepicturesExtractor(booru.BooruExtractor): |
| 19 |
"""Base class for animepictures extractors""" |
| 20 |
category = "animepictures" |
| 21 |
root = "https://anime-pictures.net" |
| 22 |
root_api = "https://api.anime-pictures.net/api" |
| 23 |
per_page = 80 |
| 24 |
download_interval = 5.0 |
| 25 |
request_interval = 1.0 |
| 26 |
|
| 27 |
TAG_TYPES = { |
| 28 |
1: "character", |
| 29 |
2: "reference", |
| 30 |
3: "copyright", |
| 31 |
4: "author", |
| 32 |
5: "", |
| 33 |
6: "copyright_other", |
| 34 |
7: "object", |
| 35 |
} |
| 36 |
|
| 37 |
def _init(self): |
| 38 |
self.cookies.set("kira", "2", domain=".anime-pictures.net") |
| 39 |
|
| 40 |
def _file_url(self, post): |
| 41 |
md5 = post["md5"] |
| 42 |
return (f"https://oimages.anime-pictures.net/" |
| 43 |
f"{md5[:3]}/{md5}{post['ext']}") |
| 44 |
|
| 45 |
def _prepare(self, post): |
| 46 |
post["date"] = self.parse_datetime_iso(post["datetime"]) |
| 47 |
|
| 48 |
def _tags(self, post, _): |
| 49 |
if "tags" not in post: |
| 50 |
data = self.request_json(f"{self.root_api}/v3/posts/{post['id']}") |
| 51 |
data.pop("post", None) |
| 52 |
post.update(data) |
| 53 |
|
| 54 |
tags = collections.defaultdict(list) |
| 55 |
for tag in post["tags"]: |
| 56 |
tag = tag["tag"] |
| 57 |
tags[tag["type"]].append(tag["tag"]) |
| 58 |
types = self.TAG_TYPES |
| 59 |
for key, value in tags.items(): |
| 60 |
post["tags_" + types[key]] = value |
| 61 |
|
| 62 |
def _pagination(self, url, params): |
| 63 |
params["page"] = text.parse_int(params.get("page"), self.page_start) |
| 64 |
|
| 65 |
while True: |
| 66 |
data = self.request_json(url, params=params) |
| 67 |
|
| 68 |
yield from data["posts"] |
| 69 |
|
| 70 |
if params["page"] >= data["max_pages"]: |
| 71 |
break |
| 72 |
params["page"] += 1 |
| 73 |
|
| 74 |
def _pagination_scores(self, url, params): |
| 75 |
if "offset" in params: |
| 76 |
params["offset"] = text.parse_int(params["offset"]) |
| 77 |
elif "page" in params: |
| 78 |
params["offset"] = text.parse_int( |
| 79 |
params.pop("page")) * self.per_page |
| 80 |
else: |
| 81 |
params["offset"] = self.page_start * self.per_page |
| 82 |
params.setdefault("limit", self.per_page) |
| 83 |
|
| 84 |
while True: |
| 85 |
data = self.request_json(url, params=params) |
| 86 |
|
| 87 |
for score in data["scores"]: |
| 88 |
post = score.pop("post") |
| 89 |
post.update(score) |
| 90 |
yield post |
| 91 |
|
| 92 |
if len(data["scores"]) < data["limit"]: |
| 93 |
break |
| 94 |
params["offset"] += data["limit"] |
| 95 |
|
| 96 |
|
| 97 |
class AnimepicturesPostExtractor(AnimepicturesExtractor): |
| 98 |
subcategory = "post" |
| 99 |
archive_fmt = "{id}" |
| 100 |
pattern = BASE_PATTERN + r"/posts/(\d+)" |
| 101 |
example = "https://anime-pictures.net/posts/12345" |
| 102 |
|
| 103 |
def posts(self): |
| 104 |
url = f"{self.root_api}/v3/posts/{self.groups[0]}" |
| 105 |
data = self.request_json(url) |
| 106 |
post = data.pop("post") |
| 107 |
post.update(data) |
| 108 |
return (post,) |
| 109 |
|
| 110 |
|
| 111 |
class AnimepicturesFavoriteExtractor(AnimepicturesExtractor): |
| 112 |
subcategory = "favorite" |
| 113 |
directory_fmt = ("{category}", "Favorites", "{favorite_id}") |
| 114 |
archive_fmt = "f_{favorite_id}_{id}" |
| 115 |
pattern = BASE_PATTERN + r"/posts\?(favorite_by=[^#]+)" |
| 116 |
example = "https://anime-pictures.net/posts?favorite_by=12345" |
| 117 |
|
| 118 |
def posts(self): |
| 119 |
url = self.root_api + "/v3/posts" |
| 120 |
params = text.parse_query(self.groups[0]) |
| 121 |
self.kwdict["favorite_id"] = params["favorite_by"] |
| 122 |
self.kwdict["favorite_folder"] = params.get("favorite_folder", "") |
| 123 |
return self._pagination(url, params) |
| 124 |
|
| 125 |
|
| 126 |
class AnimepicturesTagExtractor(AnimepicturesExtractor): |
| 127 |
subcategory = "tag" |
| 128 |
directory_fmt = ("{category}", "{search_tags}") |
| 129 |
archive_fmt = "t_{search_tags}_{id}" |
| 130 |
pattern = BASE_PATTERN + r"/posts\?([^#]+)" |
| 131 |
example = "https://anime-pictures.net/posts?search_tag=TAG" |
| 132 |
|
| 133 |
def posts(self): |
| 134 |
url = self.root_api + "/v3/posts" |
| 135 |
params = text.parse_query(self.groups[0]) |
| 136 |
self.kwdict["search_tags"] = params.get("search_tag", "") |
| 137 |
return self._pagination(url, params) |
| 138 |
|
| 139 |
|
| 140 |
class AnimepicturesStarsExtractor(AnimepicturesExtractor): |
| 141 |
subcategory = "stars" |
| 142 |
per_page = 50 |
| 143 |
directory_fmt = ("{category}", "Stars") |
| 144 |
archive_fmt = "s_{id}" |
| 145 |
pattern = BASE_PATTERN + r"/stars(?:\?([^#]+))?" |
| 146 |
example = "https://anime-pictures.net/stars" |
| 147 |
|
| 148 |
def posts(self): |
| 149 |
url = self.root_api + "/v3/scores" |
| 150 |
params = text.parse_query(self.groups[0]) |
| 151 |
return self._pagination_scores(url, params) |