def move_status(): STATUS_REDIS_KEY = "/status/text/%s" RAW_STATUS_REDIS_KEY = "/status/raw/%s" start = 3720000 limit = 100000 #r =db_conn.execute("select count(1) from status") #total = r.fetchone()[0] total = 4423725 print '----total status:', total sys.stdout.flush() ef = open("error.log", "a") #cf = open("cmd.txt", "w") while (start <= int(total)): f = open("./midfile.txt", "w") print '-------start ', start sys.stdout.flush() cursor = db_conn.execute( "select id from status order by id limit %s,%s", (start, limit)) rows = cursor.fetchall() for row in rows: text = mongo_conn.get(STATUS_REDIS_KEY % row[0]) raw = mongo_conn.get(RAW_STATUS_REDIS_KEY % row[0]) if text and raw: text = json_encode(text) if not isinstance( text, basestring) else text raw = json_encode(raw) if not isinstance(raw, basestring) else raw db_conn.execute( '''replace into raw_status (status_id, text, raw) values(%s,%s,%s)''', (row[0], text, raw)) db_conn.commit() start += limit
def add(cls, user_id, origin_id, create_time, site, category, title, text=None, raw=None): status = None cursor = None try: cursor = db_conn.execute( """insert into status (user_id, origin_id, create_time, site, category, title) values (%s,%s,%s,%s,%s,%s)""", (user_id, origin_id, create_time, site, category, title)) status_id = cursor.lastrowid if status_id > 0: text = json_encode(text) if text is not None else "" raw = json_encode(raw) if raw is not None else "" RawStatus.set(status_id, text, raw) db_conn.commit() status = cls.get(status_id) except IntegrityError: log.warning( "add status duplicated, uniq key is %s:%s:%s, ignore..." % (origin_id, site, category)) db_conn.rollback() finally: cls._clear_cache(user_id, None, cate=category) cursor and cursor.close() return status
def move_status(): STATUS_REDIS_KEY = "/status/text/%s" RAW_STATUS_REDIS_KEY = "/status/raw/%s" start = 3720000 limit = 100000 #r =db_conn.execute("select count(1) from status") #total = r.fetchone()[0] total = 4423725 print '----total status:', total sys.stdout.flush() ef = open("error.log", "a") #cf = open("cmd.txt", "w") while (start <= int(total)): f = open("./midfile.txt", "w") print '-------start ', start sys.stdout.flush() cursor = db_conn.execute("select id from status order by id limit %s,%s", (start, limit)) rows = cursor.fetchall() for row in rows: text = mongo_conn.get(STATUS_REDIS_KEY % row[0]) raw = mongo_conn.get(RAW_STATUS_REDIS_KEY% row[0]) if text and raw: text = json_encode(text) if not isinstance(text, basestring) else text raw = json_encode(raw) if not isinstance(raw, basestring) else raw db_conn.execute('''replace into raw_status (status_id, text, raw) values(%s,%s,%s)''', (row[0], text, raw)) db_conn.commit() start += limit
def myset(status_id, text, raw): cursor = None text = json_encode(text) if not isinstance(text, basestring) else text raw = json_encode(raw) if not isinstance(raw, basestring) else raw db_conn.execute('''replace into raw_status (status_id, text, raw) values(%s,%s,%s)''', (status_id, text, raw))
def myset(status_id, text, raw): cursor = None text = json_encode(text) if not isinstance(text, basestring) else text raw = json_encode(raw) if not isinstance(raw, basestring) else raw db_conn.execute( '''replace into raw_status (status_id, text, raw) values(%s,%s,%s)''', (status_id, text, raw))
def set(cls, status_id, text, raw): cursor = None text = json_encode(text) if not isinstance(text, basestring) else text raw = json_encode(raw) if not isinstance(raw, basestring) else raw try: cursor = db_conn.execute('''replace into raw_status (status_id, text, raw) values(%s,%s,%s)''', (status_id, text, raw)) db_conn.commit() cls.clear_cache(status_id) except IntegrityError: db_conn.rollback() cursor and cursor.close()
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 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 add(cls, user_id, origin_id, create_time, site, category, title, text=None, raw=None): status = None cursor = None try: cursor = db_conn.execute( """insert into status (user_id, origin_id, create_time, site, category, title) values (%s,%s,%s,%s,%s,%s)""", (user_id, origin_id, create_time, site, category, title), ) db_conn.commit() status_id = cursor.lastrowid if text is not None: mongo_conn.set(cls.STATUS_REDIS_KEY % status_id, json_encode(text)) if raw is not None: mongo_conn.set(cls.RAW_STATUS_REDIS_KEY % status_id, raw) cls._clear_cache(user_id, None, cate=category) status = cls.get(status_id) except IntegrityError: # log.warning("add status duplicated, ignore...") db_conn.rollback() finally: cursor and cursor.close() return status
def settings_set_uid(): ret = { "ok": False, "msg": "", } uid = request.form.get("uid") if not uid: ret["msg"] = "no uid" return json_encode(ret) r = g.user.update_uid(uid) if r: flag, msg = r ret['ok'] = flag ret['msg'] = msg return json_encode(ret)
def note_preview(): r = {} content = request.form.get("content", "") fmt = request.form.get("fmt", consts.NOTE_FMT_PLAIN) if fmt == consts.NOTE_FMT_MARKDOWN: r['data'] = markdown2.markdown(content, extras=["wiki-tables", "code-friendly"]) else: r['data'] = content return json_encode(r)
def note_preview(): r = {} content = request.form.get("content", "") fmt = request.form.get("fmt", consts.NOTE_FMT_PLAIN) if fmt == consts.NOTE_FMT_MARKDOWN: r['data'] = markdown2.markdown(content) else: r['data'] = content return json_encode(r)
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 set(cls, user_id, val): cursor = None val = json_encode(val) if not isinstance(val, basestring) else val try: cursor = db_conn.execute('''replace into user_profile (user_id, profile) values(%s,%s)''', (user_id, val)) db_conn.commit() cls.clear_cache(user_id) except IntegrityError: db_conn.rollback() cursor and cursor.close()
def add(cls, user_id, origin_id, create_time, site, category, title, text=None, raw=None): status = None cursor = None try: cursor = db_conn.execute("""insert into status (user_id, origin_id, create_time, site, category, title) values (%s,%s,%s,%s,%s,%s)""", (user_id, origin_id, create_time, site, category, title)) status_id = cursor.lastrowid if status_id > 0: text = json_encode(text) if text is not None else "" raw = json_encode(raw) if raw is not None else "" RawStatus.set(status_id, text, raw) db_conn.commit() status = cls.get(status_id) except IntegrityError: #log.warning("add status duplicated, ignore...") db_conn.rollback() finally: cls._clear_cache(user_id, None, cate=category) cursor and cursor.close() return status
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 add(cls, user_id, origin_id, create_time, site, category, title, text=None, raw=None): status = None cursor = None try: cursor = db_conn.execute("""insert into status (user_id, origin_id, create_time, site, category, title) values (%s,%s,%s,%s,%s,%s)""", (user_id, origin_id, create_time, site, category, title)) status_id = cursor.lastrowid try: if text is not None: mongo_conn.set(cls.STATUS_REDIS_KEY %status_id, json_encode(text)) if raw is not None: mongo_conn.set(cls.RAW_STATUS_REDIS_KEY %status_id, raw) except Exception, e: log.warning('ERROR_MONGODB:%s' % e) db_conn.rollback() else:
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 timeline_json(uid): limit = 50 u = User.get(uid) if not u: abort(404, "no such user") if uid != g.user.id and u.get_profile_item('user_privacy') == consts.USER_PRIVACY_PRIVATE: abort(403, "not allowed") cate = request.args.get("cate", None) ids = Status.get_ids(user_id=u.id, start=g.start, limit=limit, cate=g.cate) ids = ids[::-1] status_list = Status.gets(ids) if not status_list: return json_encode({}) date = [] for s in status_list: headline = s.summary or '' text = '' images = s.get_data().get_images() or [] if not (headline or text): continue t = s.create_time if s.category in [config.CATE_DOUBAN_STATUS, config.CATE_SINA_STATUS]: re_tweet = s.get_retweeted_data() re_images = re_tweet and re_tweet.get_images() or [] images.extend(re_images) text = re_tweet and re_tweet.get_content() or '' if s.category in [config.CATE_QQWEIBO_STATUS]: text = s.get_retweeted_data() or '' if s.category in [config.CATE_WORDPRESS_POST]: uri = s.get_origin_uri() headline = '<a href="%s" target="_blank">%s</a>' % (uri and uri[1], s.title) text = s.text or '' tmp = { 'startDate': t.strftime("%Y,%m,%d,%H,%M,%S"), 'headline': headline, 'text': text, 'asset': { 'media': images and images[0], 'credit': '', 'caption': '' }, } try: json_encode(tmp) date.append(tmp) except: pass if date: tmp = { 'startDate': datetime.now().strftime("%Y,%m,%d,%H,%M,%S"), 'headline': '<a href="/user/%s/visual?start=%s">查看更早的内容...</a>' % (u.id, g.start+limit), 'text': '', 'asset': { 'media': '', 'credit': '', 'caption': '' }, } date.insert(0, tmp) json_data = { 'timeline': { 'headline': 'The past of you', 'type': 'default', 'startDate': date[1]['startDate'], 'text': 'Storytelling about yourself...', 'asset':{ 'media': '', 'credit': '', 'caption': '' }, 'date':date } } return json_encode(json_data)
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 set_profile(self, profile): UserProfile.set(self.id, json_encode(profile)) return self.get_profile()
def update_detail(self, detail): k = self.__class__.kv_db_key_task % self.id redis_conn.set(k, json_encode(detail)) return self.get_detail()
def timeline_json(uid, start): limit = 50 start = int(start) u = User.get(uid) if not u: abort(404, "no such user") r = check_access_user(u) if r: abort(r[0], r[1]) ids = Status.get_ids(user_id=u.id, start=start, limit=limit, cate=g.cate) status_list = Status.gets(ids) if g.user and g.user.id == uid: pass elif g.user and g.user.id != uid: status_list = [x for x in status_list if x.privacy() != consts.STATUS_PRIVACY_PRIVATE] elif not g.user: status_list = [x for x in status_list if x.privacy() == consts.STATUS_PRIVACY_PUBLIC] if not status_list: return json_encode({}) date = [] for s in status_list: headline = s.summary or '' text = '' images = s.get_data().get_images() or [] if not (headline or text): continue t = s.create_time if s.category in [config.CATE_DOUBAN_STATUS, config.CATE_SINA_STATUS]: re_tweet = s.get_retweeted_data() re_images = re_tweet and re_tweet.get_images() or [] images.extend(re_images) text = re_tweet and re_tweet.get_content() or '' if s.category in [config.CATE_DOUBAN_STATUS]: atts = s.get_data() and s.get_data().get_attachments() if atts: for att in atts: text += att.get_title() + "\n" + att.get_description() if s.category in [config.CATE_QQWEIBO_STATUS]: text = s.get_retweeted_data() or '' if s.category in [config.CATE_WORDPRESS_POST]: uri = s.get_origin_uri() headline = '<a href="%s" target="_blank">%s</a>' % (uri and uri[1], s.title) text = s.text or '' if s.category in [config.CATE_THEPAST_NOTE]: uri = s.get_origin_uri() headline = '<a href="%s" target="_blank">%s</a>' % (uri and uri[1], s.title) text = s.text or '' tmp = { 'startDate': t.strftime("%Y,%m,%d,%H,%M,%S"), 'headline': headline, 'text': text, 'asset': { 'media': images and images[0], 'credit': '', 'caption': '' }, } try: json_encode(tmp) date.append(tmp) except: pass if date: more = { 'startDate': (status_list[-1].create_time - timedelta(0, 0, 1)).strftime("%Y,%m,%d,%H,%M,%S"), 'headline': '<a href="/visual/%s?start=%s">查看更早的内容...</a>' % (u.id, start+limit), 'text': '', 'asset': { 'media': '', 'credit': '', 'caption': '' }, } date.append(more) if start == 0: cover = { 'startDate': datetime.now().strftime("%Y,%m,%d,%H,%M,%S"), 'headline': 'The past of you', 'text': 'Storytelling about yourself...', 'asset': { 'media': '', 'credit': '', 'caption': '' }, } date.insert(0, cover) json_data = { 'timeline': { 'headline': 'The past of you', 'type': 'default', 'startDate': date[0]['startDate'], 'text': 'Storytelling about yourself...', 'asset':{ 'media': '', 'credit': '', 'caption': '' }, 'date':date, } } return json_encode(json_data)
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 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"
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 save_request_token_to_session(self, session_): t = { "key": self.token, "secret": self.token_secret, } session_['request_token'] = json_encode(t)
def set_profile(self, profile): mongo_conn.set('/profile/%s' %self.id, json_encode(profile)) return self.get_profile()
g.user.set_thirdparty_profile_item(p, "share", "Y") providers_.append(p) else: g.user.set_thirdparty_profile_item(p, "share", "N") failed_providers = [] for p in providers_: try: post_status(g.user, p, text + ",".join(images)) except OAuthError, e: log.warning("%s" % e) failed_providers.append(config.OPENID_TYPE_NAME_DICT.get(p, "")) if failed_providers: ret['ok'] = 0 ret['msg'] = "分享到" + ",".join(failed_providers) + "失败了,可能是授权过期了,重新授权就ok:)" return json_encode(ret) @app.route("/sync/<cates>", methods=["GET", "POST"]) @require_login() 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
providers_.append(p) else: g.user.set_thirdparty_profile_item(p, "share", "N") failed_providers = [] for p in providers_: try: post_status(g.user, p, text + ",".join(images)) except OAuthError, e: log.warning("%s" % e) failed_providers.append(config.OPENID_TYPE_NAME_DICT.get(p, "")) if failed_providers: ret['ok'] = 0 ret['msg'] = "分享到" + ",".join( failed_providers) + "失败了,可能是授权过期了,重新授权就ok:)" return json_encode(ret) @app.route("/sync/<cates>", methods=["GET", "POST"]) @require_login() 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
def timeline_json(uid, start): limit = 50 start = int(start) u = User.get(uid) if not u: abort(404, "no such user") r = check_access_user(u) if r: abort(r[0], r[1]) ids = Status.get_ids(user_id=u.id, start=start, limit=limit, cate=g.cate) status_list = Status.gets(ids) if g.user and g.user.id == uid: pass elif g.user and g.user.id != uid: status_list = [x for x in status_list if x.privacy() != consts.STATUS_PRIVACY_PRIVATE] elif not g.user: status_list = [x for x in status_list if x.privacy() == consts.STATUS_PRIVACY_PUBLIC] if not status_list: return json_encode({}) date = [] for s in status_list: headline = s.title or s.summary or "" text = s.text or "" data = s.get_data() images = data and data.get_images() or [] uri = s.get_origin_uri() if uri: headline = '<a href="%s" target="_blank">%s</a>' % (uri and uri[1], headline) if not (headline or text or images): continue t = s.create_time if s.category in [config.CATE_DOUBAN_STATUS, config.CATE_SINA_STATUS]: re_tweet = s.get_retweeted_data() re_images = re_tweet and re_tweet.get_images() or [] images.extend(re_images) text = re_tweet and re_tweet.get_content() or "" if s.category in [config.CATE_DOUBAN_STATUS]: atts = data and data.get_attachments() if atts: for att in atts: text += att.get_title() + "\n" + att.get_description() if s.category in [config.CATE_QQWEIBO_STATUS]: text = s.get_retweeted_data() or "" if s.category in [config.CATE_WORDPRESS_POST, config.CATE_THEPAST_NOTE, config.CATE_RENREN_BLOG]: uri = s.get_origin_uri() headline = '<a href="%s" target="_blank">%s</a>' % (uri and uri[1], s.title) text = s.text or "" if s.category in [config.CATE_RENREN_STATUS, config.CATE_RENREN_PHOTO, config.CATE_RENREN_ALBUM]: headline = s.title text = s.text or "" tmp = { "startDate": t.strftime("%Y,%m,%d,%H,%M,%S"), "headline": headline, "text": text, "asset": {"media": images and images[0], "credit": "", "caption": ""}, } try: json_encode(tmp) date.append(tmp) except: pass if date: more = { "startDate": (status_list[-1].create_time - timedelta(0, 0, 1)).strftime("%Y,%m,%d,%H,%M,%S"), "headline": '<a href="/visual/%s?start=%s">查看更早的内容...</a>' % (u.id, start + limit), "text": "", "asset": {"media": "", "credit": "", "caption": ""}, } date.append(more) if start == 0: cover = { "startDate": datetime.now().strftime("%Y,%m,%d,%H,%M,%S"), "headline": "The past of you", "text": "Storytelling about yourself...", "asset": {"media": "", "credit": "", "caption": ""}, } date.insert(0, cover) json_data = { "timeline": { "headline": "The past of you", "type": "default", "startDate": date[0]["startDate"], "text": "Storytelling about yourself...", "asset": {"media": "", "credit": "", "caption": ""}, "date": date, } } return json_encode(json_data)
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"
def set_profile(self, profile): mongo_conn.set("/profile/%s" % self.id, json_encode(profile)) mc.delete("user_profile:%s" % self.id) return self.get_profile()
def save_request_token_to_session(self, session_): t = {"key": self.token, "secret": self.token_secret,} session_['request_token'] = json_encode(t)
def timeline_json(uid, start): limit = 50 start = int(start) u = User.get(uid) if not u: abort(404, "no such user") r = check_access_user(u) if r: abort(r[0], r[1]) ids = Status.get_ids(user_id=u.id, start=start, limit=limit, cate=g.cate) status_list = Status.gets(ids) if g.user and g.user.id == uid: pass elif g.user and g.user.id != uid: status_list = [ x for x in status_list if x.privacy() != consts.STATUS_PRIVACY_PRIVATE ] elif not g.user: status_list = [ x for x in status_list if x.privacy() == consts.STATUS_PRIVACY_PUBLIC ] if not status_list: return json_encode({}) date = [] for s in status_list: headline = s.title or s.summary or "" text = s.text or "" data = s.get_data() images = data and data.get_images() or [] uri = s.get_origin_uri() if uri: headline = '<a href="%s" target="_blank">%s</a>' % (uri and uri[1], headline) if not (headline or text or images): continue t = s.create_time if s.category in [config.CATE_DOUBAN_STATUS, config.CATE_SINA_STATUS]: re_tweet = s.get_retweeted_data() re_images = re_tweet and re_tweet.get_images() or [] images.extend(re_images) text = re_tweet and re_tweet.get_content() or '' if s.category in [config.CATE_DOUBAN_STATUS]: atts = data and data.get_attachments() if atts: for att in atts: text += att.get_title() + "\n" + att.get_description() if s.category in [config.CATE_QQWEIBO_STATUS]: text = s.get_retweeted_data() or '' if s.category in [ config.CATE_WORDPRESS_POST, config.CATE_THEPAST_NOTE, config.CATE_RENREN_BLOG, ]: uri = s.get_origin_uri() headline = '<a href="%s" target="_blank">%s</a>' % (uri and uri[1], s.title) text = s.text or '' if s.category in [ config.CATE_RENREN_STATUS, config.CATE_RENREN_PHOTO, config.CATE_RENREN_ALBUM ]: headline = s.title text = s.text or '' tmp = { 'startDate': t.strftime("%Y,%m,%d,%H,%M,%S"), 'headline': headline, 'text': text, 'asset': { 'media': images and images[0], 'credit': '', 'caption': '' }, } try: json_encode(tmp) date.append(tmp) except: pass if date: more = { 'startDate': (status_list[-1].create_time - timedelta(0, 0, 1)).strftime("%Y,%m,%d,%H,%M,%S"), 'headline': '<a href="/visual/%s?start=%s">查看更早的内容...</a>' % (u.id, start + limit), 'text': '', 'asset': { 'media': '', 'credit': '', 'caption': '' }, } date.append(more) if start == 0: cover = { 'startDate': datetime.now().strftime("%Y,%m,%d,%H,%M,%S"), 'headline': 'The past of you', 'text': 'Storytelling about yourself...', 'asset': { 'media': '', 'credit': '', 'caption': '' }, } date.insert(0, cover) json_data = { 'timeline': { 'headline': 'The past of you', 'type': 'default', 'startDate': date[0]['startDate'], 'text': 'Storytelling about yourself...', 'asset': { 'media': '', 'credit': '', 'caption': '' }, 'date': date, } } return json_encode(json_data)