This repository has been archived by the owner on Mar 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubanreader.py
187 lines (160 loc) · 5.96 KB
/
doubanreader.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
# -*- coding: utf-8 -*-
import re, json, os, urllib
import requests
class DBUser:
name = ''
id = ''
uid = ''
authorization_code = ''
access_token = ''
refresh_token = ''
def __init__(self):
if os.path.isfile(USER_INFO_FILE):
pre_file = open(USER_INFO_FILE, 'r')
else:
pre_file = open(USER_INFO_FILE, 'w+r')
pre_file_read = pre_file.read()
pre_file.close()
if pre_file_read.strip():
user_info = json.loads(pre_file_read.strip())
self.name = user_info['name']
self.id = user_info['id']
self.uid = user_info['uid']
self.authorization_code = user_info['authorization_code']
self.access_token = user_info['access_token']
self.refresh_token = user_info['refresh_token']
def save(self):
pre_file = open(USER_INFO_FILE, 'w')
user_info = {}
user_info['name'] = self.name
user_info['id'] = self.id
user_info['uid'] = self.uid
user_info['authorization_code'] = self.authorization_code
user_info['access_token'] = self.access_token
user_info['refresh_token'] = self.refresh_token
pre_file.write(json.dumps(user_info))
pre_file.close()
class DBRClient:
user = None
def __init__(self, user):
self.user = user
def isAuth(self):
if self.user.authorization_code and self.user.access_token:
return True
else:
return False
def auth(self):
params = {}
params['client_id'] = CLIENT_ID
params['redirect_uri'] = REDIRECT_URI
params['response_type'] = 'code'
params['scope'] = SCOPE
print u'请访问如下地址,点击确认后,复制跳转的网址到下面:' + AUTHORIZATION_CODE_URL \
+ '?' + urllib.urlencode(params)
authorization_code = re.match(\
u'(.*)code=(.*)', \
raw_input(u'跳转网址:'.encode('utf-8')).strip()\
).group(2)
params = {'client_id': CLIENT_ID, 'client_secret': SECRET, \
'redirect_uri': REDIRECT_URI, 'grant_type': 'authorization_code', \
'code': authorization_code}
res = requests.post(\
url=ACCESS_TOKEN_URL, data=params, \
headers={'Content-Type': 'application/x-www-form-urlencoded'} \
).json()
self.user.authorization_code = authorization_code
self.user.access_token = res['access_token']
self.user.refresh_token = res['refresh_token']
res = requests.get(\
url=AUTH_USER_INFO_URL, \
headers={\
'Authorization': 'Bearer ' + self.user.access_token\
}\
).json()
self.user.id = res['id']
self.user.uid = res['uid']
self.user.name = res['name']
self.user.save()
def getUserBookCollections(self, year, month):
if month == 0:
params = {\
'status': 'read', \
'from': '%d-01-01T00:00:00+08:00' \
% (year),
'to': '%d-01-01T00:00:00+08:00' \
% (year + 1),
'count': 100
}
else:
params = {\
'status': 'read',\
'from': '%d-%02d-01T00:00:00+08:00' \
% (year, month),\
'to': '%d-%02d-01T00:00:00+08:00' \
% (year if month < 12 else year + 1, \
month + 1 if month < 12 else 1)\
}
res = requests.get(USER_BOOK_COLLECTIONS_URL % self.user.id, \
params=params
)
return res.json()['collections']
def getUserBookReview(self, book_id):
review_content = ''
start = 0
total = 100
while start < total:
res = requests.get(BOOK_REVIEWS_URL % book_id, \
params={'count': 100, 'start': start}).json()
review_alt = ''
for review in res['reviews']:
if review['author']['id'] == self.user.id:
review_alt = review['alt']
else:
continue
if review_alt:
review_content = self.getReivew(review_alt)
break
total = res['total']
start = start + 100
return review_content
def getReivew(self, url):
res = requests.get(url)
res_text = res.text.replace(u'\u3000', '')
c = re.compile('\s+')
res_text = re.sub(c, '', res_text)
pattern = u'(.*)<spanproperty="v:description"class="">(.*?)</span>(.*)'
content = re.match(pattern, res_text).group(2)
content = content.replace('<br/>', '\n')
ind = content.find('<divclass')
if ind == -1:
return content
else:
return content[:ind]
def getBookTags(self, book_id):
res = requests.get(BOOK_TAGS_URL % book_id).json()
tags_list = []
for tag in res['tags']:
tags_list.append(tag['name'])
if len(tags_list) == 10:
break
return tags_list
def convertToUTF8(self, content):
return content.encode('utf-8')
USER_INFO_FILE = 'user_info.txt'
CLIENT_ID = '022edc4b51cf759d068c94e1f56e60d7'
API_KEY = CLIENT_ID
SECRET = 'bfeee4fbd1b29e6c'
REDIRECT_URI = 'http://findingsea.github.io'
AUTHORIZATION_CODE = 'authorization_code'
ACCESS_TOKEN = 'access_token'
REFRESH_TOEKN = 'refresh_token'
DOUBAN_USER_ID = 'id'
DOUBAN_USER_UID = 'uid'
DOUBAN_USER_NAME = 'name'
ACCESS_TOKEN_URL = 'https://www.douban.com/service/auth2/token'
AUTHORIZATION_CODE_URL = 'https://www.douban.com/service/auth2/auth'
SCOPE = 'shuo_basic_r,shuo_basic_w,douban_basic_common'
AUTH_USER_INFO_URL = 'https://api.douban.com/v2/user/~me'
USER_BOOK_COLLECTIONS_URL = 'https://api.douban.com/v2/book/user/%s/collections'
BOOK_REVIEWS_URL = 'https://api.douban.com/v2/book/%s/reviews'
BOOK_TAGS_URL = 'https://api.douban.com/v2/book/%s/tags'