def _save_user_and_token(token_dict, user_info, openid_type): ua = UserAlias.get(openid_type, user_info.get_user_id()) if not ua: if not g.user: ua = UserAlias.create_new_user(openid_type, user_info.get_user_id(), user_info.get_nickname()) else: ua = UserAlias.bind_to_exists_user(g.user, openid_type, user_info.get_user_id()) if not ua: return None ##设置个人资料(头像等等) u = User.get(ua.user_id) u.set_avatar_url(user_info.get_avatar()) u.set_icon_url(user_info.get_icon()) ##保存access token if openid_type == config.OPENID_TYPE_DICT[config.OPENID_TWITTER]: OAuth2Token.add(ua.id, token_dict.get("access_token"), token_dict.get("access_token_secret", "")) else: OAuth2Token.add(ua.id, token_dict.get("access_token"), token_dict.get("refresh_token", "")) ##set cookie,保持登录状态 if not g.user: g.user = User.get(ua.user_id) set_user_cookie(g.user, session) return g.user
def sync_wordpress(t, refresh=False): if not t: log.warning('no_wordpress_sync_task') return #一个人可以有多个wordpress的rss源地址 rs = UserAlias.gets_by_user_id(t.user_id) uas = [] for x in rs: if x.type == config.OPENID_TYPE_DICT[config.OPENID_WORDPRESS]: uas.append(x) if not uas: log.warning('no_wordpress_alias') return for ua in uas: try: client = Wordpress(ua.alias) rs = client.get_feeds(refresh) if rs: log.info("get wordpress succ, result length is:%s" % len(rs)) for x in rs: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return except: import traceback; print traceback.format_exc()
def get_client(cls, user_id): alias = UserAlias.get_by_user_and_type( user_id, config.OPENID_TYPE_DICT[config.OPENID_QQ]) if not alias: return None return cls(alias.alias)
def generate(user_id, date, order='asc'): try: uas = UserAlias.gets_by_user_id(user_id) if not uas: return start_date = datetime.datetime(date.year, date.month, 1) end_date = datetime.datetime(date.year, date.month, calendar.monthrange(date.year, date.month)[1], 23, 59, 59) pdf_filename = get_pdf_filename(user_id, date.strftime("%Y%m")) print '----generate pdf:', start_date, ' to ', end_date, ' file is', pdf_filename if is_pdf_file_exists(pdf_filename): print '---- %s exists, so ignore...' % pdf_filename return status_ids = Status.get_ids_by_date(user_id, start_date, end_date)[:900] if order == 'asc': status_ids = status_ids[::-1] if not status_ids: print '----- status ids is none', status_ids return generate_pdf(pdf_filename, user_id, status_ids, capacity=-1) if not is_pdf_file_exists(pdf_filename): print '----%s generate pdf for user:%s fail' % (datetime.datetime.now(), user_id) else: print '----%s generate pdf for user:%s succ' % (datetime.datetime.now(), user_id) except Exception, e: import traceback print '%s %s' % (datetime.datetime.now(), traceback.format_exc())
def before_request(): g.user = auth_user_from_session(session) #g.user = User.get(2) if g.user: g.user_alias = UserAlias.gets_by_user_id(g.user.id) else: g.user_alias = None if g.user: unbinded = list( set(config.OPENID_TYPE_DICT.values()) - set([ua.type for ua in g.user.get_alias()]) - set([config.OPENID_TYPE_DICT[config.OPENID_THEPAST]])) tmp = {} for k, v in config.OPENID_TYPE_DICT.items(): tmp[v] = k g.unbinded = [[x, tmp[x], config.OPENID_TYPE_NAME_DICT[x]] for x in unbinded] else: g.unbinded = None g.start = int(request.args.get('start', 0)) g.count = int(request.args.get('count', 30)) g.cate = request.args.get("cate", "") if not g.cate.isdigit(): g.cate = ""
def before_request(): g.user = auth_user_from_session(session) #g.user = User.get(2) if g.user: g.user_alias = UserAlias.gets_by_user_id(g.user.id) else: g.user_alias = None if g.user: unbinded = list(set(config.OPENID_TYPE_DICT.values()) - set([ua.type for ua in g.user.get_alias()]) - set([config.OPENID_TYPE_DICT[config.OPENID_THEPAST]])) tmp = {} for k, v in config.OPENID_TYPE_DICT.items(): tmp[v] = k g.unbinded = [[x, tmp[x], config.OPENID_TYPE_NAME_DICT[x]] for x in unbinded] expired_providers = [] for t in [ua.type for ua in g.user.get_alias()]: p = g.user.get_thirdparty_profile(t) if p and p.get("expired"): _ = [t, config.OPENID_TYPE_DICT_REVERSE.get(t), config.OPENID_TYPE_NAME_DICT.get(t, "")] expired_providers.append(_) if expired_providers: msg = " ".join([x[-1] for x in expired_providers]) flash(u"你的 %s 授权已经过期了,会影响数据同步,你可以重新授权 :)", "tip") else: g.unbinded = None g.start = int(request.args.get('start', 0)) g.count = int(request.args.get('count', 30)) g.cate = request.args.get("cate", "") if not g.cate.isdigit(): g.cate = ""
def generate(user_id, date, order='asc'): try: uas = UserAlias.gets_by_user_id(user_id) if not uas: return start_date = datetime.datetime(date.year, date.month, 1) end_date = datetime.datetime( date.year, date.month, calendar.monthrange(date.year, date.month)[1], 23, 59, 59) pdf_filename = get_pdf_filename(user_id, date.strftime("%Y%m")) print '----generate pdf:', start_date, ' to ', end_date, ' file is', pdf_filename if is_pdf_file_exists(pdf_filename): print '---- %s exists, so ignore...' % pdf_filename return status_ids = Status.get_ids_by_date(user_id, start_date, end_date)[:900] if order == 'asc': status_ids = status_ids[::-1] if not status_ids: print '----- status ids is none', status_ids return generate_pdf(pdf_filename, user_id, status_ids, capacity=-1) if not is_pdf_file_exists(pdf_filename): print '----%s generate pdf for user:%s fail' % ( datetime.datetime.now(), user_id) else: print '----%s generate pdf for user:%s succ' % ( datetime.datetime.now(), user_id) except Exception, e: import traceback print '%s %s' % (datetime.datetime.now(), traceback.format_exc())
def get_client(cls, user_id): alias = UserAlias.get_by_user_and_type(user_id, config.OPENID_TYPE_DICT[config.OPENID_QQ]) if not alias: return None return cls(alias.alias)
def __init__(self, alias=None, apikey=None, apikey_secret=None, redirect_uri=None, token=None, token_secret=None): d = config.APIKEY_DICT[config.OPENID_TWITTER] self.consumer_key = apikey or d['key'] self.consumer_secret = apikey_secret or d['secret'] self.callback = redirect_uri or d['redirect_uri'] self.token = token self.token_secret = token_secret self.alias = alias if alias: self.user_alias = UserAlias.get( config.OPENID_TYPE_DICT[config.OPENID_TWITTER], alias) else: self.user_alias = None self.auth = tweepy.OAuthHandler(self.consumer_key, self.consumer_secret, self.callback) if self.token and self.token_secret and self.auth: self.auth.set_access_token(self.token, self.token_secret)
def sync_wordpress(t, refresh=False): if not t: log.warning('no_wordpress_sync_task') return #一个人可以有多个wordpress的rss源地址 rs = UserAlias.gets_by_user_id(t.user_id) uas = [] for x in rs: if x.type == config.OPENID_TYPE_DICT[config.OPENID_WORDPRESS]: uas.append(x) if not uas: log.warning('no_wordpress_alias') return for ua in uas: try: client = Wordpress(ua.alias) rs = client.get_feeds(refresh) if rs: log.info("get wordpress succ, result length is:%s" % len(rs)) for x in rs: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return except: import traceback print traceback.format_exc()
def __init__(self, alias=None, apikey=None, apikey_secret=None, redirect_uri=None, token=None, token_secret=None, openid=None, openkey=None): self.consumer_key = apikey or config.APIKEY_DICT[ config.OPENID_QQ]['key'] self.consumer_secret = apikey_secret or config.APIKEY_DICT[ config.OPENID_QQ]['secret'] self.callback = redirect_uri or config.APIKEY_DICT[ config.OPENID_QQ]['redirect_uri'] self.token = token self.token_secret = token_secret #XXX:no use? self.openid = openid self.openkey = openkey self.alias = alias if alias: self.user_alias = UserAlias.get( config.OPENID_TYPE_DICT[config.OPENID_QQ], alias) else: self.user_alias = None
def _save_user_and_token(token_dict, thirdparty_user, openid_type): first_connect = False ua = UserAlias.get(openid_type, thirdparty_user.get_user_id()) if not ua: if not g.user: ua = UserAlias.create_new_user(openid_type, thirdparty_user.get_user_id(), thirdparty_user.get_nickname()) else: ua = UserAlias.bind_to_exists_user(g.user, openid_type, thirdparty_user.get_user_id()) first_connect = True if not ua: return None ##设置个人资料(头像等等) u = User.get(ua.user_id) u.set_avatar_url(thirdparty_user.get_avatar()) u.set_icon_url(thirdparty_user.get_icon()) ##把各个第三方的uid保存到profile里面 k = openid_type v = { "uid": thirdparty_user.get_uid(), "name": thirdparty_user.get_nickname(), "intro": thirdparty_user.get_intro(), "signature": thirdparty_user.get_signature(), "avatar": thirdparty_user.get_avatar(), "icon": thirdparty_user.get_icon(), "email": thirdparty_user.get_email(), "first_connect": "Y" if first_connect else "N", } u.set_profile_item(k, json_encode(v)) ##保存access token if openid_type == config.OPENID_TYPE_DICT[config.OPENID_TWITTER]: OAuth2Token.add(ua.id, token_dict.get("access_token"), token_dict.get("access_token_secret", "")) else: OAuth2Token.add(ua.id, token_dict.get("access_token"), token_dict.get("refresh_token", "")) ##set cookie,保持登录状态 if not g.user: g.user = User.get(ua.user_id) set_user_cookie(g.user, session) return g.user
def before_request(): g.user = auth_user_from_session(session) if g.user: g.user_alias = UserAlias.gets_by_user_id(g.user.id) else: g.user_alias = None g.start = int(request.args.get("start", 0)) g.count = int(request.args.get("count", 30)) g.cate = request.args.get("cate", None)
def get_client(cls, user_id): alias = UserAlias.get_by_user_and_type(user_id, config.OPENID_TYPE_DICT[config.OPENID_DOUBAN]) if not alias: return None token = OAuth2Token.get(alias.id) if not token: return None return cls(alias.alias, token.access_token, token.refresh_token)
def before_request(): g.config = config g.user = auth_user_from_session(session) #g.user = User.get(2) g.user_alias = UserAlias.gets_by_user_id(g.user.id) if g.user else None if request.method == 'POST': try: g.start = int(request.form.get('start', 0)) except ValueError: g.start = 0 try: g.count = int(request.form.get('count', 24)) except ValueError: g.count = 0 g.cate = request.form.get("cate", "") else: try: g.start = int(request.args.get('start', 0)) except ValueError: g.start = 0 try: g.count = int(request.args.get('count', 24)) except ValueError: g.count = 0 g.cate = request.args.get("cate", "") g.cate = int(g.cate) if g.cate.isdigit() else "" if g.user: g.binds = [ua.type for ua in g.user.get_alias()] unbinded = list( set(config.OPENID_TYPE_DICT.values()) - set(g.binds) - set([config.OPENID_TYPE_DICT[config.OPENID_THEPAST]])) tmp = {} for k, v in config.OPENID_TYPE_DICT.items(): tmp[v] = k g.unbinded = [[x, tmp[x], config.OPENID_TYPE_NAME_DICT[x]] for x in unbinded] expired_providers = [] for t in [ua.type for ua in g.user.get_alias()]: p = g.user.get_thirdparty_profile(t) if p and p.get("expired"): _ = [ t, config.OPENID_TYPE_DICT_REVERSE.get(t), config.OPENID_TYPE_NAME_DICT.get(t, "") ] expired_providers.append(_) g.expired = expired_providers if expired_providers: msg = " ".join([x[-1] for x in expired_providers]) flash(u"你的 %s 授权已经过期了,会影响数据同步,你可以重新授权 :)" % msg, "tip") else: g.unbinded = None
def get_client(cls, user_id): alias = UserAlias.get_by_user_and_type( user_id, config.OPENID_TYPE_DICT[config.OPENID_DOUBAN]) if not alias: return None token = OAuth2Token.get(alias.id) if not token: return None return cls(alias.alias, token.access_token, token.refresh_token)
def __init__(self, alias, apikey=None, apikey_secret=None, access_token=None, access_token_secret=None): ua = UserAlias.get(config.OPENID_TYPE_DICT[config.OPENID_TWITTER], alias) self.apikey = apikey if apikey is not None else config.APIKEY_DICT[config.OPENID_TWITTER].get("key") self.apikey_secret = apikey_secret if apikey_secret is not None else config.APIKEY_DICT[config.OPENID_TWITTER].get("secret") token = OAuth2Token.get(ua.id) self.access_token = access_token if access_token is not None else token.access_token self.access_token_secret = access_token_secret if access_token_secret is not None else token.refresh_token self.auth = tweepy.OAuthHandler(self.apikey, self.apikey_secret) self.auth.set_access_token(self.access_token, self.access_token_secret)
def __init__(self, alias, apikey=None, apikey_secret=None, access_token=None, access_token_secret=None): ua = UserAlias.get(config.OPENID_TYPE_DICT[config.OPENID_QQ], alias) self.apikey = apikey if apikey is not None else config.APIKEY_DICT[config.OPENID_QQ].get("key") self.apikey_secret = apikey_secret if apikey_secret is not None else config.APIKEY_DICT[config.OPENID_QQ].get("secret") ##TODO:这里的OAuth2Token也变相的存储了OAuth1的token和secret,需要后续改一改 token = OAuth2Token.get(ua.id) self.access_token = access_token if access_token is not None else token.access_token self.access_token_secret = access_token_secret if access_token_secret is not None else token.refresh_token self.auth = QQOAuth1Login(self.apikey, self.apikey_secret, token=self.access_token, token_secret=self.access_token_secret)
def before_request(): g.config = config g.user = auth_user_from_session(session) #g.user = User.get(2) g.user_alias = UserAlias.gets_by_user_id(g.user.id) if g.user else None if request.method == 'POST': try: g.start = int(request.form.get('start', 0)) except ValueError: g.start = 0 try: g.count = int(request.form.get('count', 24)) except ValueError: g.count = 0 g.cate = request.form.get("cate", "") else: try: g.start = int(request.args.get('start', 0)) except ValueError: g.start = 0 try: g.count = int(request.args.get('count', 24)) except ValueError: g.count = 0 g.cate = request.args.get("cate", "") g.cate = int(g.cate) if g.cate.isdigit() else "" if g.user: g.binds = [ua.type for ua in g.user.get_alias()] unbinded = list(set(config.OPENID_TYPE_DICT.values()) - set(g.binds) - set([config.OPENID_TYPE_DICT[config.OPENID_THEPAST]])) tmp = {} for k, v in config.OPENID_TYPE_DICT.items(): tmp[v] = k g.unbinded = [[x, tmp[x], config.OPENID_TYPE_NAME_DICT[x]] for x in unbinded] expired_providers = [] for t in [ua.type for ua in g.user.get_alias()]: p = g.user.get_thirdparty_profile(t) if p and p.get("expired"): _ = [t, config.OPENID_TYPE_DICT_REVERSE.get(t), config.OPENID_TYPE_NAME_DICT.get(t, "")] expired_providers.append(_) g.expired = expired_providers if expired_providers: msg = " ".join([x[-1] for x in expired_providers]) flash(u"你的 %s 授权已经过期了,会影响数据同步,你可以重新授权 :)" % msg, "tip") else: g.unbinded = None
def bind(uid, feed_uri): user = User.get(uid) if not user: print 'no user' return ua = UserAlias.bind_to_exists_user(user, config.OPENID_TYPE_DICT[config.OPENID_WORDPRESS], feed_uri) if not ua: print "no user alias" else: ##添加同步任务 t = SyncTask.add(config.CATE_WORDPRESS_POST, user.id) t and TaskQueue.add(t.id, t.kind) ##删除confiration记录 mc.delete("wordpress_bind:%s" %user.id)
def bind(uid, feed_uri): user = User.get(uid) if not user: print 'no user' return ua = UserAlias.bind_to_exists_user( user, config.OPENID_TYPE_DICT[config.OPENID_WORDPRESS], feed_uri) if not ua: print "no user alias" else: ##添加同步任务 t = SyncTask.add(config.CATE_WORDPRESS_POST, user.id) t and TaskQueue.add(t.id, t.kind) ##删除confiration记录 mc.delete("wordpress_bind:%s" % user.id)
def __init__(self, alias, apikey=None, apikey_secret=None, access_token=None, access_token_secret=None): ua = UserAlias.get(config.OPENID_TYPE_DICT[config.OPENID_TWITTER], alias) self.apikey = apikey if apikey is not None else config.APIKEY_DICT[ config.OPENID_TWITTER].get("key") self.apikey_secret = apikey_secret if apikey_secret is not None else config.APIKEY_DICT[ config.OPENID_TWITTER].get("secret") token = OAuth2Token.get(ua.id) self.access_token = access_token if access_token is not None else token.access_token self.access_token_secret = access_token_secret if access_token_secret is not None else token.refresh_token self.auth = tweepy.OAuthHandler(self.apikey, self.apikey_secret) self.auth.set_access_token(self.access_token, self.access_token_secret)
def before_request(): g.user = auth_user_from_session(session) if g.user: g.user_alias = UserAlias.gets_by_user_id(g.user.id) else: g.user_alias = None if g.user: unbinded = list(set(config.OPENID_TYPE_DICT.values()) - set([ua.type for ua in g.user.get_alias()])) tmp = {} for k, v in config.OPENID_TYPE_DICT.items(): tmp[v] = k g.unbinded = [[x, tmp[x], config.OPENID_TYPE_NAME_DICT[x]] for x in unbinded] else: g.unbinded = None g.start = int(request.args.get("start", 0)) g.count = int(request.args.get("count", 30)) g.cate = request.args.get("cate", None)
def generate(user_id, date, order='asc'): try: uas = UserAlias.gets_by_user_id(user_id) if not uas: return start_date = datetime.datetime(date.year, date.month, 1) end_date = datetime.datetime( date.year, date.month, calendar.monthrange(date.year, date.month)[1], 23, 59, 59) pdf_filename = get_pdf_filename(user_id, date.strftime("%Y%m"), "") pdf_filename_compressed = get_pdf_filename(user_id, date.strftime("%Y%m")) print '----generate pdf:', start_date, ' to ', end_date, ' file is', pdf_filename if is_pdf_file_exists(pdf_filename_compressed): print '---- %s exists, so ignore...' % pdf_filename_compressed return status_ids = Status.get_ids_by_date(user_id, start_date, end_date)[:900] if order == 'asc': status_ids = status_ids[::-1] if not status_ids: print '----- status ids is none', status_ids return generate_pdf(pdf_filename, user_id, status_ids) if not is_pdf_file_exists(pdf_filename): print '----%s generate pdf for user:%s fail' % ( datetime.datetime.now(), user_id) else: commands.getoutput( "cd %s && tar -zcf %s %s && rm %s" % (config.PDF_FILE_DOWNLOAD_DIR, pdf_filename_compressed, pdf_filename, pdf_filename)) print '----%s generate pdf for user:%s succ' % ( datetime.datetime.now(), user_id) except Exception, e: import traceback print '%s %s' % (datetime.datetime.now(), traceback.format_exc())
def __init__(self, alias=None, apikey=None, apikey_secret=None, redirect_uri=None, token=None, token_secret=None, openid=None, openkey=None): self.consumer_key = apikey or config.APIKEY_DICT[config.OPENID_QQ]['key'] self.consumer_secret = apikey_secret or config.APIKEY_DICT[config.OPENID_QQ]['secret'] self.callback = redirect_uri or config.APIKEY_DICT[config.OPENID_QQ]['redirect_uri'] self.token = token self.token_secret = token_secret #XXX:no use? self.openid = openid self.openkey = openkey self.alias=alias if alias: self.user_alias = UserAlias.get( config.OPENID_TYPE_DICT[config.OPENID_QQ], alias) else: self.user_alias = None
def sync(cates): cates = cates.split("|") if not (cates and isinstance(cates, list)): return "no cates" cates = filter(lambda x: x in [str(y) for y in config.CATE_LIST], cates) if not cates: abort(400, "not support such cates") provider = category2provider(int(cates[0])) redir = "/connect/%s" % provider if not g.user: print '--- no g.user...' return redirect(redir) if request.form.get("remove"): for c in cates: r = SyncTask.gets_by_user_and_cate(g.user, str(c)) for x in r: x.remove() return json_encode({'ok':'true'}) uas = UserAlias.gets_by_user_id(g.user.id) r = filter(lambda x: x.type == config.OPENID_TYPE_DICT[provider], uas) user_alias = r and r[0] if not user_alias: print '--- no user_alias...' return json_encode({'ok':'false', 'redir':redir}) token = OAuth2Token.get(user_alias.id) if not token: print '--- no token...' return json_encode({'ok':'false', 'redir':redir}) for c in cates: SyncTask.add(c, g.user.id) return json_encode({'ok':'true'})
def sync(cates): cates = cates.split("|") if not (cates and isinstance(cates, list)): return "no cates" cates = filter(lambda x: x in [str(y) for y in config.CATE_LIST], cates) if not cates: abort(400, "not support such cates") provider = category2provider(int(cates[0])) redir = "/connect/%s" % provider if not g.user: print '--- no g.user...' return redirect(redir) if request.form.get("remove"): for c in cates: r = SyncTask.gets_by_user_and_cate(g.user, str(c)) for x in r: x.remove() return json_encode({'ok': 'true'}) uas = UserAlias.gets_by_user_id(g.user.id) r = filter(lambda x: x.type == config.OPENID_TYPE_DICT[provider], uas) user_alias = r and r[0] if not user_alias: print '--- no user_alias...' return json_encode({'ok': 'false', 'redir': redir}) token = OAuth2Token.get(user_alias.id) if not token: print '--- no token...' return json_encode({'ok': 'false', 'redir': redir}) for c in cates: SyncTask.add(c, g.user.id) return json_encode({'ok': 'true'})
def __init__(self, provider=None, apikey=None, apikey_secret=None, redirect_uri=None, scope=None, state=None, display=None, alias=None, access_token=None, refresh_token=None): self.provider = provider self.apikey = apikey self.apikey_secret = apikey_secret self.redirect_uri = redirect_uri self.scope = scope self.state = state self.display = display self.alias = alias if alias: self.user_alias = UserAlias.get( config.OPENID_TYPE_DICT[provider], alias) else: self.user_alias = None self.access_token = access_token self.refresh_token = refresh_token
def __init__(self, alias, apikey=None, apikey_secret=None, access_token=None, access_token_secret=None): ua = UserAlias.get(config.OPENID_TYPE_DICT[config.OPENID_QQ], alias) self.apikey = apikey if apikey is not None else config.APIKEY_DICT[ config.OPENID_QQ].get("key") self.apikey_secret = apikey_secret if apikey_secret is not None else config.APIKEY_DICT[ config.OPENID_QQ].get("secret") ##TODO:这里的OAuth2Token也变相的存储了OAuth1的token和secret,需要后续改一改 token = OAuth2Token.get(ua.id) self.access_token = access_token if access_token is not None else token.access_token self.access_token_secret = access_token_secret if access_token_secret is not None else token.refresh_token self.auth = QQOAuth1Login(self.apikey, self.apikey_secret, token=self.access_token, token_secret=self.access_token_secret)
def sync(t, old=False): alias = None provider = category2provider(t.category) if provider == config.OPENID_DOUBAN: alias = UserAlias.get_by_user_and_type(t.user_id, config.OPENID_TYPE_DICT[config.OPENID_DOUBAN]) elif provider == config.OPENID_SINA: alias = UserAlias.get_by_user_and_type(t.user_id, config.OPENID_TYPE_DICT[config.OPENID_SINA]) elif provider == config.OPENID_TWITTER: alias = UserAlias.get_by_user_and_type(t.user_id, config.OPENID_TYPE_DICT[config.OPENID_TWITTER]) elif provider == config.OPENID_QQ: alias = UserAlias.get_by_user_and_type(t.user_id, config.OPENID_TYPE_DICT[config.OPENID_QQ]) if not alias: log.warn("no alias...") return 0 token = OAuth2Token.get(alias.id) if not token: log.warn("no access token, break...") return 0 client = None if provider == config.OPENID_DOUBAN: client = Douban(alias.alias, token.access_token, token.refresh_token) elif provider == config.OPENID_SINA: client = SinaWeibo(alias.alias, token.access_token) elif provider == config.OPENID_TWITTER: client = Twitter(alias.alias) elif provider == config.OPENID_QQ: client = QQWeibo(alias.alias) if not client: log.warn("get client fail, break...") return 0 if t.category == config.CATE_DOUBAN_NOTE: if old: start = Status.get_count_by_cate(t.category, t.user_id) else: start = 0 note_list = client.get_notes(start, 50) if note_list: for x in note_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(note_list) elif t.category == config.CATE_DOUBAN_MINIBLOG: if old: start = Status.get_count_by_cate(t.category, t.user_id) else: start = 0 miniblog_list = client.get_miniblogs(start, 50) if miniblog_list: for x in miniblog_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(miniblog_list) elif t.category == config.CATE_DOUBAN_STATUS or t.category == config.CATE_SINA_STATUS: if old: until_id = Status.get_min_origin_id(t.category, t.user_id) #means max_id status_list = client.get_timeline(until_id=until_id) else: since_id = Status.get_min_origin_id(t.category, t.user_id) status_list = client.get_timeline(since_id=since_id) if status_list: for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(status_list) elif t.category == config.CATE_TWITTER_STATUS: if old: until_id = Status.get_min_origin_id(t.category, t.user_id) #means max_id status_list = client.get_timeline(max_id=until_id) else: since_id = Status.get_min_origin_id(t.category, t.user_id) status_list = client.get_timeline(since_id=since_id) if status_list: for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(status_list) elif t.category == config.CATE_QQWEIBO_STATUS: if old: oldest_create_time = Status.get_oldest_create_time(t.category, t.user_id) oldest_create_time = datetime2timestamp(oldest_create_time) status_list = client.get_old_timeline(oldest_create_time, reqnum=200) else: status_list = client.get_new_timeline(reqnum=20) if status_list: for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(status_list) return 0
def sync(t, old=False): if not t: print 'no such task' return 0 log.info("the sync task is :%s" % t) try: alias = None provider = category2provider(t.category) if provider == config.OPENID_DOUBAN: alias = UserAlias.get_by_user_and_type( t.user_id, config.OPENID_TYPE_DICT[config.OPENID_DOUBAN]) elif provider == config.OPENID_SINA: alias = UserAlias.get_by_user_and_type( t.user_id, config.OPENID_TYPE_DICT[config.OPENID_SINA]) elif provider == config.OPENID_TWITTER: alias = UserAlias.get_by_user_and_type( t.user_id, config.OPENID_TYPE_DICT[config.OPENID_TWITTER]) elif provider == config.OPENID_QQ: alias = UserAlias.get_by_user_and_type( t.user_id, config.OPENID_TYPE_DICT[config.OPENID_QQ]) if not alias: log.warn("no alias...") return 0 token = OAuth2Token.get(alias.id) if not token: log.warn("no access token, break...") return 0 client = None if provider == config.OPENID_DOUBAN: client = Douban(alias.alias, token.access_token, token.refresh_token) elif provider == config.OPENID_SINA: client = SinaWeibo(alias.alias, token.access_token) elif provider == config.OPENID_TWITTER: client = Twitter(alias.alias) elif provider == config.OPENID_QQ: client = QQWeibo(alias.alias) if not client: log.warn("get client fail, break...") return 0 if t.category == config.CATE_DOUBAN_NOTE: if old: start = Status.get_count_by_cate(t.category, t.user_id) else: start = 0 note_list = client.get_notes(start, 50) if note_list: for x in note_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(note_list) elif t.category == config.CATE_DOUBAN_MINIBLOG: if old: start = Status.get_count_by_cate(t.category, t.user_id) else: start = 0 miniblog_list = client.get_miniblogs(start, 50) if miniblog_list: for x in miniblog_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(miniblog_list) elif t.category == config.CATE_DOUBAN_STATUS: origin_min_id = Status.get_min_origin_id(t.category, t.user_id) if old: log.info("will get douban status order than %s..." % origin_min_id) status_list = client.get_timeline(until_id=origin_min_id) else: log.info("will get douban status newer than %s..." % origin_min_id) status_list = client.get_timeline(since_id=origin_min_id, count=20) if status_list: log.info("get douban status succ, len is %s" % len(status_list)) for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) elif t.category == config.CATE_SINA_STATUS: origin_min_id = Status.get_min_origin_id(t.category, t.user_id) #means max_id if old: log.info("will get sinaweibo order than %s..." % origin_min_id) status_list = client.get_timeline(until_id=origin_min_id) ## 如果根据max_id拿不到数据,那么根据page再fetch一次或者until_id - 1 if len(status_list) < 20: log.info("again will get sinaweibo order than %s..." % (int(origin_min_id) - 1)) status_list = client.get_timeline( until_id=int(origin_min_id) - 1) #if len(status_list) <= 1: # downloaded_status_count = Status.get_count_by_user(t.user_id) # page = downloaded_status_count / 100 + 2 # status_list = client.get_timeline_by_page(page=page) else: log.info("will get sinaweibo newer than %s..." % origin_min_id) status_list = client.get_timeline(since_id=origin_min_id, count=20) if status_list: log.info("get sinaweibo succ, len is %s" % len(status_list)) for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(status_list) elif t.category == config.CATE_TWITTER_STATUS: origin_min_id = Status.get_min_origin_id(t.category, t.user_id) if old: log.info("will get tweets order than %s..." % origin_min_id) status_list = client.get_timeline(max_id=origin_min_id) else: log.info("will get tweets newer than %s..." % origin_min_id) status_list = client.get_timeline(since_id=origin_min_id, count=20) if status_list: log.info("get tweets succ, len is %s" % len(status_list)) for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(status_list) elif t.category == config.CATE_QQWEIBO_STATUS: if old: oldest_create_time = Status.get_oldest_create_time( t.category, t.user_id) log.info("will get qqweibo order than %s" % oldest_create_time) if oldest_create_time is not None: oldest_create_time = datetime2timestamp(oldest_create_time) status_list = client.get_old_timeline(oldest_create_time, reqnum=200) else: log.info("will get qqweibo new timeline") status_list = client.get_new_timeline(reqnum=20) if status_list: log.info("get qqweibo succ, result length is:%s" % len(status_list)) for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(status_list) except: import traceback print traceback.format_exc() return 0
def sync(t, old=False): if not t: print 'no such task' return 0 log.info("the sync task is :%s" % t) try: alias = None provider = category2provider(t.category) if provider == config.OPENID_DOUBAN: alias = UserAlias.get_by_user_and_type(t.user_id, config.OPENID_TYPE_DICT[config.OPENID_DOUBAN]) elif provider == config.OPENID_SINA: alias = UserAlias.get_by_user_and_type(t.user_id, config.OPENID_TYPE_DICT[config.OPENID_SINA]) elif provider == config.OPENID_TWITTER: alias = UserAlias.get_by_user_and_type(t.user_id, config.OPENID_TYPE_DICT[config.OPENID_TWITTER]) elif provider == config.OPENID_QQ: alias = UserAlias.get_by_user_and_type(t.user_id, config.OPENID_TYPE_DICT[config.OPENID_QQ]) if not alias: log.warn("no alias...") return 0 token = OAuth2Token.get(alias.id) if not token: log.warn("no access token, break...") return 0 client = None if provider == config.OPENID_DOUBAN: client = Douban(alias.alias, token.access_token, token.refresh_token) elif provider == config.OPENID_SINA: client = SinaWeibo(alias.alias, token.access_token) elif provider == config.OPENID_TWITTER: client = Twitter(alias.alias) elif provider == config.OPENID_QQ: client = QQWeibo(alias.alias) if not client: log.warn("get client fail, break...") return 0 if t.category == config.CATE_DOUBAN_NOTE: if old: start = Status.get_count_by_cate(t.category, t.user_id) else: start = 0 note_list = client.get_notes(start, 50) if note_list: for x in note_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(note_list) elif t.category == config.CATE_DOUBAN_MINIBLOG: if old: start = Status.get_count_by_cate(t.category, t.user_id) else: start = 0 miniblog_list = client.get_miniblogs(start, 50) if miniblog_list: for x in miniblog_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(miniblog_list) elif t.category == config.CATE_DOUBAN_STATUS: origin_min_id = Status.get_min_origin_id(t.category, t.user_id) if old: log.info("will get douban status order than %s..." % origin_min_id) status_list = client.get_timeline(until_id=origin_min_id) else: log.info("will get douban status newer than %s..." % origin_min_id) status_list = client.get_timeline(since_id=origin_min_id, count=20) if status_list: log.info("get douban status succ, len is %s" % len(status_list)) for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) elif t.category == config.CATE_SINA_STATUS: origin_min_id = Status.get_min_origin_id(t.category, t.user_id) #means max_id if old: log.info("will get sinaweibo order than %s..." % origin_min_id) status_list = client.get_timeline(until_id=origin_min_id) ## 如果根据max_id拿不到数据,那么根据page再拿一次 if len(status_list) <= 1: downloaded_status_count = Status.get_count_by_user(t.user_id) page = downloaded_status_count / 100 + 2 status_list = client.get_timeline_by_page(page=page) else: log.info("will get sinaweibo newer than %s..." % origin_min_id) status_list = client.get_timeline(since_id=origin_min_id, count=20) if status_list: log.info("get sinaweibo succ, len is %s" % len(status_list)) for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(status_list) elif t.category == config.CATE_TWITTER_STATUS: origin_min_id = Status.get_min_origin_id(t.category, t.user_id) if old: log.info("will get tweets order than %s..." % origin_min_id) status_list = client.get_timeline(max_id=origin_min_id) else: log.info("will get tweets newer than %s..." % origin_min_id) status_list = client.get_timeline(since_id=origin_min_id, count=20) if status_list: log.info("get tweets succ, len is %s" % len(status_list)) for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(status_list) elif t.category == config.CATE_QQWEIBO_STATUS: if old: oldest_create_time = Status.get_oldest_create_time(t.category, t.user_id) log.info("will get qqweibo order than %s" % oldest_create_time) if oldest_create_time is not None: oldest_create_time = datetime2timestamp(oldest_create_time) status_list = client.get_old_timeline(oldest_create_time, reqnum=200) else: log.info("will get qqweibo new timeline") status_list = client.get_new_timeline(reqnum=20) if status_list: log.info("get qqweibo succ, result length is:%s" % len(status_list)) for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(status_list) except: import traceback; print traceback.format_exc() return 0
def sync(t, old=False): if not t: print 'no such task' return 0 log.info("the sync task is :%s" % t) try: alias = None provider = category2provider(t.category) alias = UserAlias.get_by_user_and_type(t.user_id, config.OPENID_TYPE_DICT[provider]) if not alias: log.warn("no alias...") return 0 token = OAuth2Token.get(alias.id) if not token: log.warn("no access token, break...") return 0 client = None if provider == config.OPENID_DOUBAN: client = Douban.get_client(alias.user_id) elif provider == config.OPENID_SINA: client = SinaWeibo.get_client(alias.user_id) elif provider == config.OPENID_TWITTER: client = TwitterOAuth1.get_client(alias.user_id) elif provider == config.OPENID_QQ: client = QQWeibo.get_client(alias.user_id) elif provider == config.OPENID_RENREN: client = Renren.get_client(alias.user_id) elif provider == config.OPENID_INSTAGRAM: client = Instagram.get_client(alias.user_id) if not client: log.warn("get client fail, break...") return 0 if t.category == config.CATE_DOUBAN_NOTE: if old: start = Status.get_count_by_cate(t.category, t.user_id) else: start = 0 note_list = client.get_notes(start, 50) if note_list: for x in note_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(note_list) elif t.category == config.CATE_DOUBAN_MINIBLOG: if old: start = Status.get_count_by_cate(t.category, t.user_id) else: start = 0 miniblog_list = client.get_miniblogs(start, 50) if miniblog_list: for x in miniblog_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(miniblog_list) elif t.category == config.CATE_DOUBAN_STATUS: origin_min_id = Status.get_min_origin_id(t.category, t.user_id) if old: log.info("will get douban status order than %s..." % origin_min_id) status_list = client.get_timeline(until_id=origin_min_id) else: log.info("will get douban status newer than %s..." % origin_min_id) status_list = client.get_timeline(since_id=origin_min_id, count=20) if status_list: log.info("get douban status succ, len is %s" % len(status_list)) for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) elif t.category == config.CATE_SINA_STATUS: origin_min_id = Status.get_min_origin_id(t.category, t.user_id) #means the earliest id origin_max_id = Status.get_max_origin_id(t.category, t.user_id) #meas the latest id if old: log.info("will get sinaweibo order than %s..." % origin_min_id) status_list = client.get_timeline(until_id=origin_min_id) ## 如果根据max_id拿不到数据,那么根据page再fetch一次或者until_id - 1 if status_list and len(status_list) < 20 and origin_min_id is not None: log.info("again will get sinaweibo order than %s..." % (int(origin_min_id)-1)) status_list = client.get_timeline(until_id=int(origin_min_id)-1) else: log.info("will get sinaweibo newer than %s..." % origin_max_id) status_list = client.get_timeline(since_id=origin_max_id, count=50) if status_list: log.info("get sinaweibo succ, len is %s" % len(status_list)) for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(status_list) elif t.category == config.CATE_TWITTER_STATUS: origin_min_id = Status.get_min_origin_id(t.category, t.user_id) origin_max_id = Status.get_max_origin_id(t.category, t.user_id) if old: log.info("will get tweets order than %s..." % origin_min_id) status_list = client.get_timeline(max_id=origin_min_id) else: log.info("will get tweets newer than %s..." % origin_max_id) status_list = client.get_timeline(since_id=origin_max_id, count=50) if status_list: log.info("get tweets succ, len is %s" % len(status_list)) for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(status_list) elif t.category == config.CATE_QQWEIBO_STATUS: if old: oldest_create_time = Status.get_oldest_create_time(t.category, t.user_id) log.info("will get qqweibo order than %s" % oldest_create_time) if oldest_create_time is not None: oldest_create_time = datetime2timestamp(oldest_create_time) status_list = client.get_old_timeline(oldest_create_time, reqnum=200) else: log.info("will get qqweibo new timeline") status_list = client.get_new_timeline(reqnum=20) if status_list: log.info("get qqweibo succ, result length is:%s" % len(status_list)) for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(status_list) elif t.category == config.CATE_RENREN_STATUS: if old: count = 100 total_count = Status.get_count_by_cate(t.category, t.user_id) page = int(total_count / count) + 1 log.info("will get older renren status, page=%s, count=%s" %(page, count)) status_list = client.get_timeline(page, count) else: count = 20 page = 1 log.info("will get newest renren status, page=%s, count=%s" %(page, count)) status_list = client.get_timeline(page, count) if status_list: log.info("get renren status succ, result length is:%s" % len(status_list)) for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(status_list) elif t.category == config.CATE_RENREN_BLOG: if old: count = 50 total_count = Status.get_count_by_cate(t.category, t.user_id) page = int(total_count / count) + 1 log.info("will get older renren blog, page=%s, count=%s" %(page, count)) blogs = client.get_blogs(page, count) else: count = 20 page = 1 log.info("will get newest renren blog, page=%s, count=%s" %(page, count)) blogs = client.get_blogs(page, count) if blogs: uid = blogs.get("uid") blog_ids = filter(None, [v.get("id") for v in blogs.get("blogs", [])]) log.info("get renren blog ids succ, result length is:%s" % len(blog_ids)) for blog_id in blog_ids: blog = client.get_blog(blog_id, uid) if blog: Status.add_from_obj(t.user_id, blog, json_encode(blog.get_data())) return len(blog_ids) elif t.category == config.CATE_RENREN_ALBUM: status_list = client.get_albums() if status_list: log.info("get renren album succ, result length is:%s" % len(status_list)) for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(status_list) elif t.category == config.CATE_RENREN_PHOTO: albums_ids = Status.get_ids(user_id=t.user_id, limit=1000, cate=config.CATE_RENREN_ALBUM) albums = Status.gets(albums_ids) if not albums: return 0 for x in albums: d = x.get_data() if not d: continue aid = d.get_origin_id() size = int(d.get_size()) count = 50 for i in xrange(1, size/count + 2): status_list = client.get_photos(aid, i, count) if status_list: log.info("get renren photo of album %s succ, result length is:%s" \ % (aid, len(status_list))) for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) elif t.category == config.CATE_INSTAGRAM_STATUS: origin_min_id = Status.get_min_origin_id(t.category, t.user_id) #means the earliest id origin_max_id = Status.get_max_origin_id(t.category, t.user_id) #means the latest id if old: log.info("will get instagram earlier than %s..." % origin_min_id) status_list = client.get_timeline(max_id=origin_min_id) else: log.info("will get instagram later than %s..." % origin_max_id) status_list = client.get_timeline(min_id=origin_max_id, count=50) if status_list: log.info("get instagram succ, len is %s" % len(status_list)) for x in status_list: Status.add_from_obj(t.user_id, x, json_encode(x.get_data())) return len(status_list) except Exception, e: print "---sync_exception_catched:", e
def bind_wordpress(): if not g.user: flash(u"请先使用豆瓣、微博、QQ、Twitter任意一个帐号登录后,再来做绑定blog的操作^^", "tip") return redirect("/home") intros = [g.user.get_thirdparty_profile(x).get("intro") for x in config.OPENID_TYPE_DICT.values()] intros = filter(None, intros) uas = g.user.get_alias() wordpress_alias_list = [x for x in uas if x.type == config.OPENID_TYPE_DICT[config.OPENID_WORDPRESS]] step = "1" random_id = mc.get("wordpress_bind:%s" % g.user.id) c = random_id and Confirmation.get_by_random_id(random_id) if c: _, feed_uri = c.text.split(":", 1) step = "2" else: feed_uri = "" if request.method == "GET": return render_template("bind_wordpress.html", consts=consts, **locals()) elif request.method == "POST": ret = {} ret['ok'] = False if step == '1': feed_uri = request.form.get("feed_uri") if not feed_uri: ret['msg'] = 'feed地址不能为空' elif not (feed_uri.startswith("http://") or feed_uri.startswith("https://")): ret['msg'] = 'feed地址貌似不对' else: ua = UserAlias.get(config.OPENID_TYPE_DICT[config.OPENID_WORDPRESS], feed_uri) if ua: ret['msg'] = '该feed地址已被绑定' else: ##设置一个激活码 code = randbytes(16) val = "%s:%s" % (g.user.id, feed_uri) r = Confirmation.add(code, val) if r: ret['ok'] = True ret['msg'] = '为了验证blog的主人^^,请发一篇blog,内容为 %s,完成该步骤后,请点下一步完成绑定' \ % code mc.set("wordpress_bind:%s" %g.user.id, code) else: ret['msg'] = '抱歉,出错了,请重试, 或者给管理员捎个话:[email protected]' return json_encode(ret) elif step == '2': if not (random_id and c): ret['msg'] = '出错了,激活码不对^^' else: text = c.text user_id, feed_uri = text.split(":", 1) ## 同步一下,看看验证码的文章是否正确 client = Wordpress(feed_uri) rs = client.get_feeds(refresh=True) if not rs: ret['msg'] = '没有发现含有验证码的文章,请检查后再提交验证' else: latest_post = rs[0] if not latest_post: ret['msg'] = "你的feed地址可能无法访问,请检查下" else: content = latest_post.get_content() or latest_post.get_summary() if content and content.encode("utf8")[:100].find(str(random_id)) != -1: ua = UserAlias.bind_to_exists_user(g.user, config.OPENID_TYPE_DICT[config.OPENID_WORDPRESS], feed_uri) if not ua: ret['msg'] = '出错了,麻烦你重试一下吧^^' else: ##添加同步任务 t = SyncTask.add(config.CATE_WORDPRESS_POST, g.user.id) t and TaskQueue.add(t.id, t.kind) ##删除confiration记录 c.delete() mc.delete("wordpress_bind:%s" %g.user.id) ret['ok'] = True ret['msg'] = '恭喜,绑定成功啦' else: ret['msg'] = "没有发现含有验证码的文章,请检查后再提交验证" return json_encode(ret) else: return "method not allowed"
#-*- coding:utf-8 -*- import sys sys.path.append('../') import past from past import config from past.model.user import UserAlias from past.model.status import SyncTask all_alias_ids = UserAlias.get_ids() for id_ in all_alias_ids: print id_ ua = UserAlias.get_by_id(id_) if not ua: continue print ua if ua.type == 'D': SyncTask.add(config.CATE_DOUBAN_NOTE, ua.user_id) SyncTask.add(config.CATE_DOUBAN_MINIBLOG, ua.user_id) if ua.type == 'S': SyncTask.add(config.CATE_SINA_STATUS, ua.user_id) if ua.type == 'T': SyncTask.add(config.CATE_TWITTER_STATUS, ua.user_id)
import time import datetime activate_this = '../env/bin/activate_this.py' execfile(activate_this, dict(__file__=activate_this)) from past.utils.pdf import generate_pdf, get_pdf_filename, is_pdf_file_exists from past.model.user import User, UserAlias from past import config if __name__ == "__main__": #for uid in User.get_ids(0, 10000000): for uid in range(634,652): try: uas = UserAlias.gets_by_user_id(uid) if not uas: continue types = [x.type for x in uas] count = 300 if config.OPENID_TYPE_DICT[config.OPENID_SINA] in types \ or config.OPENID_TYPE_DICT[config.OPENID_QQ] in types: count = 250 pdf_filename = get_pdf_filename(uid) print pdf_filename generate_pdf(pdf_filename, uid, 0, count, capacity=-1) if not is_pdf_file_exists(pdf_filename): print '%s generate pdf for user:%s fail' % (datetime.datetime.now(), uid) else: print '%s generate pdf for user:%s succ' % (datetime.datetime.now(), uid) except Exception, e:
#-*- coding:utf-8 -*- import sys sys.path.append('../') import past from past import config from past.model.user import UserAlias from past.model.status import SyncTask all_alias_ids = UserAlias.get_ids() for id_ in all_alias_ids: print id_ ua = UserAlias.get_by_id(id_) if not ua: continue print ua if ua.type == 'D': SyncTask.add(config.CATE_DOUBAN_STATUS, ua.user_id) #SyncTask.add(config.CATE_DOUBAN_MINIBLOG, ua.user_id) #if ua.type == 'S': # SyncTask.add(config.CATE_SINA_STATUS, ua.user_id) #if ua.type == 'T': # SyncTask.add(config.CATE_TWITTER_STATUS, ua.user_id)
def bind_wordpress(): if not g.user: flash(u"请先使用豆瓣、微博、QQ、Twitter任意一个帐号登录后,再来做绑定blog的操作^^", "tip") return redirect("/home") user = g.user intros = [ g.user.get_thirdparty_profile(x).get("intro") for x in config.OPENID_TYPE_DICT.values() ] intros = filter(None, intros) uas = g.user.get_alias() wordpress_alias_list = [ x for x in uas if x.type == config.OPENID_TYPE_DICT[config.OPENID_WORDPRESS] ] step = "1" random_id = mc.get("wordpress_bind:%s" % g.user.id) c = random_id and Confirmation.get_by_random_id(random_id) if c: _, feed_uri = c.text.split(":", 1) step = "2" else: feed_uri = "" if request.method == "GET": return render_template("v2/bind_wordpress.html", consts=consts, **locals()) elif request.method == "POST": ret = {} ret['ok'] = False if step == '1': feed_uri = request.form.get("feed_uri") if not feed_uri: ret['msg'] = 'feed地址不能为空' elif not (feed_uri.startswith("http://") or feed_uri.startswith("https://")): ret['msg'] = 'feed地址貌似不对' else: ua = UserAlias.get( config.OPENID_TYPE_DICT[config.OPENID_WORDPRESS], feed_uri) if ua: ret['msg'] = '该feed地址已被绑定' else: ##设置一个激活码 code = randbytes(16) val = "%s:%s" % (g.user.id, feed_uri) r = Confirmation.add(code, val) if r: ret['ok'] = True ret['msg'] = '为了验证blog的主人^^,请发一篇blog,内容为 %s,完成该步骤后,请点下一步完成绑定' \ % code mc.set("wordpress_bind:%s" % g.user.id, code) else: ret['msg'] = '抱歉,出错了,请重试, 或者给管理员捎个话:[email protected]' return json_encode(ret) elif step == '2': if not (random_id and c): ret['msg'] = '出错了,激活码不对^^' else: text = c.text user_id, feed_uri = text.split(":", 1) ## 同步一下,看看验证码的文章是否正确 client = Wordpress(feed_uri) rs = client.get_feeds(refresh=True) if not rs: ret['msg'] = '没有发现含有验证码的文章,请检查后再提交验证' else: latest_post = rs[0] if not latest_post: ret['msg'] = "你的feed地址可能无法访问,请检查下" else: content = latest_post.get_content( ) or latest_post.get_summary() if content and content.encode("utf8")[:100].find( str(random_id)) != -1: ua = UserAlias.bind_to_exists_user( g.user, config.OPENID_TYPE_DICT[ config.OPENID_WORDPRESS], feed_uri) if not ua: ret['msg'] = '出错了,麻烦你重试一下吧^^' else: ##添加同步任务 t = SyncTask.add(config.CATE_WORDPRESS_POST, g.user.id) t and TaskQueue.add(t.id, t.kind) ##删除confiration记录 c.delete() mc.delete("wordpress_bind:%s" % g.user.id) ret['ok'] = True ret['msg'] = '恭喜,绑定成功啦' else: ret['msg'] = "没有发现含有验证码的文章,请检查后再提交验证" return json_encode(ret) else: return "method not allowed"