-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.py
More file actions
44 lines (38 loc) · 1.29 KB
/
upload.py
File metadata and controls
44 lines (38 loc) · 1.29 KB
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
from auth import get_auth
from requests import Response, post
import json
from typing import List
class Poem:
def __init__(self, title: str, poet: str, content: str) -> None:
if not title or not content:
raise Exception("invalid content or title")
self.title = title
self.poet = poet
self.content = content
def to_dict(self) -> dict:
return {"title": self.title, "poet": self.poet, "poem": self.content}
def __str__(self) -> str:
return json.dumps(self.to_dict())
def upload(poems: List[Poem]):
if not poems or not len(poems):
pass
auth = get_auth()
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer {}".format(auth),
}
poem_dict: List[dict] = list(map(lambda p: p.to_dict(), poems))
res: Response = post(
"https://poemwiki.org/api/v1/poem/import",
headers=headers,
data=json.dumps({"poems": poem_dict}),
)
status_code = res.status_code
if res.status_code != 200:
raise Exception(
"Failed with status {}, content={}".format(status_code, res.content)
)
else:
uploaded = json.dumps(list(map(lambda p: p.title, poems)))
print("upload: {}".format(uploaded))