e621_bot/e621.py

146 lines
2.8 KiB
Python
Raw Normal View History

2023-11-03 12:51:41 +00:00
import json
import logging
2023-03-22 15:07:42 +00:00
from dataclasses import dataclass
from dataclasses_json import dataclass_json
from enum import Enum
from typing import List, Optional
import httpx
class Rating(Enum):
E = "e"
Q = "q"
S = "s"
@dataclass_json
@dataclass
class E621PostFile:
width: int
height: int
ext: str
size: int
md5: str
2023-03-30 20:10:08 +00:00
url: Optional[str]
2023-03-22 15:07:42 +00:00
@dataclass_json
@dataclass
class E621PostFlags:
pending: bool
flagged: bool
note_locked: bool
status_locked: bool
rating_locked: bool
2023-05-13 01:23:40 +00:00
# comment_disabled: Optional[bool]
2023-03-22 15:07:42 +00:00
deleted: bool
@dataclass_json
@dataclass
class E621PostPreview:
width: int
height: int
2023-03-30 20:10:08 +00:00
url: Optional[str]
2023-03-22 15:07:42 +00:00
@dataclass_json
@dataclass
class E621PostRelationships:
parent_id: Optional[int]
has_children: bool
has_active_children: bool
children: List[int]
@dataclass_json
@dataclass
class E621PostOriginal:
type: str
height: int
width: int
urls: List[Optional[str]]
@dataclass_json
@dataclass
class E621PostAlternates:
original: Optional[E621PostOriginal]
@dataclass_json
@dataclass
class E621PostSample:
has: bool
height: int
width: int
2023-03-30 20:10:08 +00:00
url: Optional[str]
2023-03-22 15:07:42 +00:00
# alternates: E621PostAlternates
@dataclass_json
@dataclass
class E621PostScore:
up: int
down: int
total: int
@dataclass_json
@dataclass
class E621PostTags:
general: List[str]
species: List[str]
character: List[str]
copyright: List[str]
artist: List[str]
invalid: List[str]
lore: List[str]
meta: List[str]
def flatten(self):
return self.general + self.species + self.character + self.copyright + self.artist + \
self.invalid + self.lore + self.meta
@dataclass_json
@dataclass
class E621Post:
id: int
created_at: str
updated_at: str
file: E621PostFile
preview: E621PostPreview
sample: E621PostSample
score: E621PostScore
tags: E621PostTags
locked_tags: List[str]
change_seq: int
flags: E621PostFlags
rating: Rating
fav_count: int
sources: List[str]
pools: List[int]
relationships: E621PostRelationships
2023-03-30 20:10:08 +00:00
approver_id: Optional[int]
2023-03-22 15:07:42 +00:00
uploader_id: int
description: str
comment_count: int
is_favorited: bool
has_notes: bool
duration: Optional[float]
class E621:
def __init__(self):
self.client = httpx.AsyncClient(headers={'user-agent': 'bot/1.0 (bakatrouble)'}, base_url='https://e621.net')
2023-08-13 02:50:28 +00:00
async def get_posts(self, tags='', page=1, limit=320) -> List[E621Post]:
2023-03-22 15:07:42 +00:00
r = (await self.client.get('/posts.json', params={'tags': tags, 'page': page, 'limit': limit})).json()
2023-11-03 12:51:41 +00:00
logging.warning(json.dumps(r))
2023-03-22 15:07:42 +00:00
return [E621Post.from_dict(p) for p in r['posts']]
2023-05-31 21:04:20 +00:00
async def get_post(self, post_id: str) -> E621Post:
return (await self.get_posts(f'id:{post_id}'))[0]