-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
176 lines (130 loc) · 5.3 KB
/
api.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
from xml.etree import ElementTree
from config import CONSUMER_KEY
from service import goodreads_service
class AuthError(Exception):
pass
class ApiError(Exception):
pass
def session_decorator(func):
def wrapper(self, *args, **kwargs):
session = goodreads_service.get_db_tokens_session(args[0])
if not session:
raise AuthError("""Попробуйте авторизоваться через /authorize, """
"""либо используйте /logout и /authorize для повторной авторизации, """
"""в случае проблем с доступом""")
kwargs['session'] = session
return func(self, *args[1:], **kwargs)
return wrapper
class GoodreadsAPI():
def me(self, session=None):
response = session.get('/api/auth_user')
root = ElementTree.fromstring(response.content)
goodreads_id = root.find('user').attrib['id']
return goodreads_id
@session_decorator
def get_search_books(self, search_query, page=1, per_page=5, session=None):
params = {
"q": search_query,
"key": CONSUMER_KEY,
"page": page,
"per_page": per_page,
}
response = session.get("/search/index.xml",
params=params)
root = ElementTree.fromstring(response.content)
books = []
book_fields = ['title', 'id', 'image_url']
for entry in root.iter('best_book'):
book = {field: entry.find(field).text for field in book_fields}
authors = [auth.find('name').text for auth in entry.iter('author')]
book['authors'] = authors
books.append(book)
return books
@session_decorator
def get_shelves(self, page=1, per_page=5, session=None):
goodreads_id = self.me(session=session)
params = {
"key": CONSUMER_KEY,
"user_id": goodreads_id,
"format": "xml",
}
response = session.get("/shelf/list.xml",
params=params)
root = ElementTree.fromstring(response.content)
shelf_fields = frozenset(['book_count', 'name'])
shelves = []
for shelf in root.iter('user_shelf'):
shelf = {field: shelf.find(field).text for field in shelf_fields}
shelf['show_name'] = " ".join(shelf['name'].split("-")).title()
shelves.append(shelf)
return shelves[::-1]
@session_decorator
def get_books(self, page=1, per_page=5, shelf="etc", session=None):
params = {
"v": 2,
"key": CONSUMER_KEY,
"format": "xml",
"page": page,
"shelf": shelf,
"per_page": per_page,
}
response = session.get("/review/list",
params=params)
root = ElementTree.fromstring(response.content)
book_fields = frozenset(['id', 'title', 'publication_year', 'link'])
books = []
for entry in root.iter('book'):
book = {field: entry.find(field).text for field in book_fields}
authors = [auth.find('name').text for auth in entry.iter('author')]
book['authors'] = authors
books.append(book)
return books
@session_decorator
def get_book(self, book_id, session=None):
params = {
"key": CONSUMER_KEY,
"format": "xml",
}
response = session.get(f'/book/show/{book_id}.xml',
params=params)
root = ElementTree.fromstring(response.content)
book_xml = root.find('book')
book_fields = [
'id', 'title', 'description',
'image_url', 'small_image_url', 'link'
]
book = {field: book_xml.find(field).text for field in book_fields}
authors = book_xml.find("authors")
book['authors'] = [a.find('name').text for a in authors.iter('author')]
shelf_xml = root.find("./book/my_review/shelves/shelf")
response = {
"title": book["title"],
"authors": book["authors"],
"description": book.get("description"),
"link": book["link"],
"image": book['image_url'] or book['small_image_url']
}
if shelf_xml is not None:
response['shelf'] = shelf_xml.attrib['name']
return response
@session_decorator
def add_to_shelf(self, shelf, book_id, remove=False, session=None):
# can also remove book from shelf (tnx for greads developers)
data = {
'name': shelf,
'book_id': book_id,
}
if remove:
data['name'] = 'to-read'
response = session.post("shelf/add_to_shelf.xml",
data=data)
data['a'] = 'remove'
response = session.post("shelf/add_to_shelf",
data=data)
if response.status_code not in (200, 201):
raise ApiError(f"Ошибка добавления! status: {response.status_code} data: {data}")
message = "Книга добавлена на полку!"
if remove:
message = "Книга удалена!"
return message
goodreads_api = GoodreadsAPI()