-
Notifications
You must be signed in to change notification settings - Fork 0
/
setting.py
192 lines (156 loc) · 7.68 KB
/
setting.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
import os
import codecs
import logging
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util
from google.appengine.ext import webapp
from google.appengine.ext import db
from urllib2 import urlopen, URLError, HTTPError
from config import *
from login import LoginHandler
from userinfo import UserInfo
from weibo import APIClient
import auth
import sys
if sys.getdefaultencoding() != 'utf8':
reload(sys)
sys.setdefaultencoding('utf8')
_DEBUG=True
class SettingHandler(webapp.RequestHandler):
def get(self):
nickname = LoginHandler().checkLogin()
if not nickname:
self.redirect("/")
#if True:
try:
userInfo = db.GqlQuery("SELECT * FROM UserInfo WHERE nickname=:1", nickname).get()
if not userInfo:
self.response.out.write("no such a user")
return
#client = APIClient(app_key=WEIBO_APP_KEY, app_secret=WEIBO_APP_SECRET, redirect_uri=WEIBO_CALLBACK_URL)
weibo = auth.WeiboAuth(None)
#client = OAuthHandler(WEIBO_APP_KEY, WEIBO_APP_SECRET)
#weibo_auth_url = client.get_authorization_url()
weibo_auth_url = None
if V2:
weibo_auth_url = weibo.get_authorize_url()
if not userInfo.nickname:
userInfo.nickname = ""
if not userInfo.flickr_id :
userInfo.flickr_id = ""
if not userInfo.flickr_api_key:
userInfo.flickr_api_key = ""
if not userInfo.flickr_api_secret:
userInfo.flickr_api_secret = ""
if not userInfo.flickr_browse_type:
userInfo.flickr_browse_type = "stream"
if not userInfo.flickr_browse_typename:
userInfo.flickr_browse_typename = ""
if not userInfo.weibo_id:
userInfo.weibo_id = ""
if not userInfo.weibo_access_token:
userInfo.weibo_access_token = ""
if not userInfo.weibo_access_token_secret:
userInfo.weibo_access_token_secret = ""
if not userInfo.weibo_avatar:
userInfo.weibo_avatar = ""
#flick_login_url = None
#if not auth.GetFlickrToken(userInfo.nickname):
flick_login_url = auth.GetFlickrLoginUrl(userInfo)
template_values = {
'nickname': userInfo.nickname,
'flickr_id': userInfo.flickr_id,
'flickr_api_key': userInfo.flickr_api_key,
'flickr_api_secret': userInfo.flickr_api_secret,
'flickr_browse_type': userInfo.flickr_browse_type,
'flickr_browse_typename': userInfo.flickr_browse_typename,
'flickr_browse_setid': userInfo.flickr_browse_setid,
'flickr_max_days': userInfo.flickr_max_days,
'flick_login_url':flick_login_url,
'weibo_id': userInfo.weibo_id,
'weibo_access_token': userInfo.weibo_access_token,
'weibo_access_token_secret': userInfo.weibo_access_token_secret,
'weibo_auth_url' : weibo_auth_url,
'weibo_avatar' : userInfo.weibo_avatar
}
cwd = os.path.dirname(__file__)
path = os.path.join(cwd, 'templates', 'setting.html')
self.response.out.write(template.render(path, template_values, debug=_DEBUG))
except Exception as e:
self.response.out.write( "Unexpected error: %s"% (e))
def post(self):
if not LoginHandler().checkLogin():
self.redirect("login.html")
nickname = self.request.get('nickname')
message = ""
#if True:
try:
#client = APIClient(app_key=WEIBO_APP_KEY, app_secret=WEIBO_APP_SECRET, redirect_uri=WEIBO_CALLBACK_URL)
userInfo = UserInfo.gql("WHERE nickname=:1",nickname).get()
if not userInfo:
self.response.out.write( "No such a user: %s"% (nickname))
return
weibo = auth.WeiboAuth(userInfo)
flickr = auth.FlickrAuth(userInfo)
weibo_auth_url = None
if V2:
weibo_auth_url = weibo.get_authorize_url()
userInfo.flickr_id = self.request.get('flickr_id')
userInfo.flickr_api_key = self.request.get('flickr_api_key')
userInfo.flickr_api_secret = self.request.get('flickr_api_secret')
userInfo.flickr_browse_type = self.request.get('flickr_browse_type')
userInfo.flickr_browse_typename = self.request.get('flickr_browse_typename')
userInfo.weibo_id = self.request.get('weibo_id')
userInfo.weibo_access_token = self.request.get('weibo_access_token')
userInfo.weibo_access_token_secret = self.request.get('weibo_access_token_secret')
if userInfo.flickr_browse_type == "set" :
#check flickr photoset
sets = flickr.photosets_getList(user_id=userInfo.flickr_id)
set_id = -1
found = False
for set in sets.findall('photosets/photoset'):
set_title = set.find('title').text
if set_title == userInfo.flickr_browse_typename:
set_id = int(set.attrib['id'])
found = True
break
if found == False:
self.response.out.write( "No such photoset: %s"% (userInfo.flickr_browse_typename))
return
userInfo.flickr_browse_setid = set_id
userInfo.flickr_max_days = int(self.request.get('flickr_max_days'))
userInfo.put()
message = "user information updated"
flick_login_url = None
if not auth.GetFlickrToken(userInfo.nickname):
flick_login_url = auth.GetFlickrLoginUrl(userInfo)
template_values = {
'nickname': userInfo.nickname,
'flickr_id': userInfo.flickr_id,
'flickr_api_key': userInfo.flickr_api_key,
'flickr_api_secret': userInfo.flickr_api_secret,
'flickr_browse_type': userInfo.flickr_browse_type,
'flickr_browse_typename': userInfo.flickr_browse_typename,
'flickr_browse_setid': userInfo.flickr_browse_setid,
'flickr_max_days': userInfo.flickr_max_days,
'flick_login_url':flick_login_url,
'weibo_id': userInfo.weibo_id,
'weibo_access_token': userInfo.weibo_access_token,
'weibo_access_token_secret': userInfo.weibo_access_token_secret,
'weibo_avatar' : userInfo.weibo_avatar,
'weibo_auth_url' : weibo_auth_url,
'message' : u"用户信息已经更新完毕"
}
cwd = os.path.dirname(__file__)
path = os.path.join(cwd, 'templates', 'setting.html')
self.response.out.write(template.render(path, template_values, debug=_DEBUG))
except Exception as e:
self.response.out.write( "Unexpected error: %s"% (e))
application = webapp.WSGIApplication([
('/setting', SettingHandler)
],
debug=True)
def main():
util.run_wsgi_app(application)
if __name__ == '__main__':
main()