-
Notifications
You must be signed in to change notification settings - Fork 0
/
hasura.py
224 lines (189 loc) · 5.86 KB
/
hasura.py
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
from datetime import datetime
from logging import getLogger
from typing import Iterable
from urllib.parse import urljoin
import httpx
from pydantic import BaseModel, ValidationError
from .base import (
LiveInboxApiNiconicoUserIconCacheMetadata,
LiveInboxApiNiconicoUserIconCacheMetadataManager,
)
logger = getLogger(__name__)
class GetNiconicoUserIconCacheByUrlsRequestVariables(BaseModel):
urls: list[str]
class GetNiconicoUserIconCacheByUrlsResponseNiconicoUserIconCache(BaseModel):
id: str
url: str
fetched_at: datetime
file_size: int
hash_md5: str
content_type: str
file_key: str
class GetNiconicoUserIconCacheByUrlsResponseData(BaseModel):
niconico_user_icon_caches: list[
GetNiconicoUserIconCacheByUrlsResponseNiconicoUserIconCache
]
class GetNiconicoUserIconCacheByUrlsResponseBody(BaseModel):
data: GetNiconicoUserIconCacheByUrlsResponseData
class InsertNiconicoUserIconRequestVariables(BaseModel):
url: str
fetched_at: datetime
file_size: int
hash_md5: str
content_type: str
file_key: str
class InsertNiconicoUserIconCacheResponseInsertNiconicoUserIconCache(BaseModel):
id: str
class InsertNiconicoUserIconCacheResponseData(BaseModel):
insert_niconico_user_icon_caches_one: (
InsertNiconicoUserIconCacheResponseInsertNiconicoUserIconCache
)
class InsertNiconicoUserIconCacheResponseBody(BaseModel):
data: InsertNiconicoUserIconCacheResponseData
class LiveInboxApiNiconicoUserIconCacheMetadataHasuraManager(
LiveInboxApiNiconicoUserIconCacheMetadataManager
):
def __init__(
self,
hasura_url: str,
hasura_token: str,
useragent: str,
):
self.hasura_url = hasura_url
self.hasura_token = hasura_token
self.useragent = useragent
def get_by_urls(
self,
urls: Iterable[str],
) -> list[LiveInboxApiNiconicoUserIconCacheMetadata]:
hasura_url = self.hasura_url
hasura_token = self.hasura_token
useragent = self.useragent
hasura_api_url = urljoin(hasura_url, "v1/graphql")
payload = {
"query": """
query GetNiconicoUserIconCacheByUrls(
$urls: [String!]!
) {
niconico_user_icon_caches(
where: {
url: {
_in: $urls
}
}
) {
id
url
fetched_at
file_size
hash_md5
content_type
file_key
}
}
""",
"variables": GetNiconicoUserIconCacheByUrlsRequestVariables(
urls=list(urls),
).model_dump(mode="json"),
}
res = httpx.post(
url=hasura_api_url,
headers={
"Authorization": f"Bearer {hasura_token}",
"User-Agent": useragent,
},
json=payload,
)
res.raise_for_status()
response_body_json = res.json()
try:
response_body = GetNiconicoUserIconCacheByUrlsResponseBody.model_validate(
response_body_json
)
except ValidationError:
logger.error(response_body_json)
raise
hasura_niconico_user_icon_caches = response_body.data.niconico_user_icon_caches
logger.info(
f"Fetched {len(hasura_niconico_user_icon_caches)} niconico_user_icon_caches"
)
niconico_user_icon_cache_metadatas: list[
LiveInboxApiNiconicoUserIconCacheMetadata
] = []
for hasura_niconico_user_icon_cache in hasura_niconico_user_icon_caches:
niconico_user_icon_cache_metadatas.append(
LiveInboxApiNiconicoUserIconCacheMetadata(
url=hasura_niconico_user_icon_cache.url,
fetched_at=hasura_niconico_user_icon_cache.fetched_at,
file_size=hasura_niconico_user_icon_cache.file_size,
hash_md5=hasura_niconico_user_icon_cache.hash_md5,
content_type=hasura_niconico_user_icon_cache.content_type,
file_key=hasura_niconico_user_icon_cache.file_key,
),
)
return niconico_user_icon_cache_metadatas
def save(
self,
url: str,
fetched_at: datetime,
file_size: int,
hash_md5: str,
content_type: str,
file_key: str,
) -> None:
hasura_url = self.hasura_url
hasura_token = self.hasura_token
useragent = self.useragent
hasura_api_url = urljoin(hasura_url, "v1/graphql")
payload = {
"query": """
mutation InsertNiconicoUserIconCache(
$url: String!
$fetched_at: timestamptz!
$file_size: Int!
$hash_md5: String!
$content_type: String!
$file_key: String!
) {
insert_niconico_user_icon_caches_one(
object: {
url: $url
fetched_at: $fetched_at
file_size: $file_size
hash_md5: $hash_md5
content_type: $content_type
file_key: $file_key
}
) {
id
}
}
""",
"variables": InsertNiconicoUserIconRequestVariables(
url=url,
fetched_at=fetched_at,
file_size=file_size,
hash_md5=hash_md5,
content_type=content_type,
file_key=file_key,
).model_dump(mode="json"),
}
res = httpx.post(
url=hasura_api_url,
headers={
"Authorization": f"Bearer {hasura_token}",
"User-Agent": useragent,
},
json=payload,
)
res.raise_for_status()
response_body_json = res.json()
try:
response_body = InsertNiconicoUserIconCacheResponseBody.model_validate(
response_body_json
)
except ValidationError:
logger.error(response_body_json)
raise
id = response_body.data.insert_niconico_user_icon_caches_one.id
logger.info(f"Created niconico_user_icon_caches[id={id}]")