e621_bot/e621.py

140 lines
2.6 KiB
Python
Raw Normal View History

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
url: str
@dataclass_json
@dataclass
class E621PostFlags:
pending: bool
flagged: bool
note_locked: bool
status_locked: bool
rating_locked: bool
comment_disabled: bool
deleted: bool
@dataclass_json
@dataclass
class E621PostPreview:
width: int
height: int
url: str
@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
url: str
# 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
approver_id: None
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')
async def get_posts(self, tags='', page=1, limit=50) -> List[E621Post]:
r = (await self.client.get('/posts.json', params={'tags': tags, 'page': page, 'limit': limit})).json()
return [E621Post.from_dict(p) for p in r['posts']]