示例#1
0
def get_video_data(day, num, type, all_site = True):
	bili_api = bili.BiliAPI(day, type, all_site)
	video_set = bili_api.fetch()
	#video_set = video_json['list']
	video_list = []
	cnt = 0
	for video in video_set:
		#print video
		aid = int(video['aid'])
		#print aid
		current_video = Video.query.filter_by(id = aid).first()
		if current_video is None:
			current_video = Video(id = aid)
			db_session.add(current_video)
		current_video.play = video['play']
		current_video.title = video['title']
		current_video.author = video['author']
		current_video.description = video['description']
		current_video.pic = video['pic']
		current_video.pts = video['pts']
		current_video.type = type
		current_video.detail = video
		db_session.commit()
		#print current_video.id
		if cnt < num:
			video_list.append(current_video)
			cnt = cnt + 1
		else:
			break
	return video_list
def callback():
	try:
		code = request.args['code']
		api = sina.WeiboOAuth(const.APP_KEY, const.APP_SECRET)

		tokens = api.exchange_access_token(const.CALLBACK_URL, code)

		# assumpt uid in tokens...
		uid = int(tokens['uid'])
		user = WeiboUser.query.get(uid)
		#return user.id
		need_fetch = True
		if user is None:
			user = WeiboUser(id=uid)
			db_session.add(user)
		else:
			limit = arrow.utcnow().replace(hours=-1)
			if user.last_update and user.last_update > limit:
				need_fetch = False
		user.update_token(tokens)
		#Need to add command
		if need_fetch:
			user.update()
			get_weibo.get_weibo_data(user.access_token, user.id)
			db_session.commit()
		login_user(user)
		return redirect(url_for('.index', type='all'))
	except:
		raise
		db_session.rollback()
		return render_template('frontend/error.html')
def store_into_database(uid, vid, algorithm_name):
    recommend = RecommendRelation.query.filter_by(
        weibo_user_id=uid, bili_video_id=vid,
        algorithm=algorithm_name).first()
    if recommend is None:
        current_recommend = RecommendRelation(weibo_user_id=uid,
                                              bili_video_id=vid,
                                              algorithm=algorithm_name)
        db_session.add(current_recommend)
def dislike_video():
    aid = request.args.get('aid')
    user = g.user
    video = Video.query.filter_by(id=aid).first()
    cur_play = LikeRelation.query.filter_by(weibo_user_id=g.user.id).filter_by(
        video_id=aid).first()
    if cur_play is None:
        cur_play = LikeRelation(weibo_user_id=g.user.id, video_id=aid)
        db_session.add(cur_play)
    cur_play.score = const.DISLIKE
    db_session.commit()
    return redirect(url_for('.index', aid=aid))
def index():
    aid = request.args.get('aid')
    play_video = Video.query.filter_by(id=aid).first()
    if play_video is None:
        return render_template('frontend/error.html')
    cur_play = LikeRelation.query.filter_by(weibo_user_id=g.user.id).filter_by(
        video_id=aid).first()
    if cur_play is None:
        cur_play = LikeRelation(weibo_user_id=g.user.id, video_id=aid)
        db_session.add(cur_play)
    if cur_play.score is None:
        cur_play.score = const.PLAY
    db_session.commit()
    recommend_videos = video.show_video_data(6, u'')
    return render_template('play/play.html',
                           video=play_video,
                           recommend_videos=recommend_videos)
def get_weibo_data(access_token, uid):
    weibo_api = sina.WeiboAPI(access_token, uid)
    weibo_json = weibo_api.get_weibo_data()
    weibo_set = weibo_json['statuses']
    for weibo in weibo_set:
        w_id = weibo['id']
        current_weibo = WeiboTweet.query.filter_by(id=w_id).first()
        if current_weibo is None:
            current_weibo = WeiboTweet(id=w_id)
            db_session.add(current_weibo)
        current_weibo.text = weibo['text']
        timestr = weibo['created_at']
        timeinfo = basic.analyze_timestr(timestr)
        current_weibo.created_at = timeinfo['time']
        current_weibo.utc = timeinfo['utc']
        current_weibo.source = weibo['source']
        current_weibo.reposts_cnt = weibo['reposts_count']
        current_weibo.comments_cnt = weibo['comments_count']
        current_weibo.uid = uid
        current_weibo.data_set = str(weibo)
        db_session.commit()
示例#7
0
def get_friends_data(access_token, uid):
    friends_api = sina.WeiboAPI(access_token, uid)
    followers_json = friends_api.get_follower_list()
    followers_set = followers_json['users']
    current_user = WeiboUser.query.filter_by(id=uid).first()
    for friend in followers_set:
        f_id = friend['id']
        current_friend = WeiboUser.query.filter_by(id=f_id).first()
        if current_friend is None:
            current_friend = WeiboUser(id=f_id)
            db_session.add(current_friend)
            db_session.commit()
            follower = WeiboRelation(follower_id=f_id, followed_id=uid)
            db_session.add(follower)
        current_friend.uid = uid
        current_friend.screen_name = friend['screen_name']
        current_friend.description = friend['description']
        current_friend.profile_url = friend['profile_url']
        current_friend.gender = basic.analyze_gender(friend['gender'])
        current_friend.follow_me = friend['follow_me']
        current_friend.data_set = str(friend)
        db_session.commit()
    followed_json = friends_api.get_followed_list()
    followed_set = followed_json['users']
    for friend in followed_set:
        f_id = friend['id']
        current_friend = WeiboUser.query.filter_by(id=f_id).first()
        if current_friend is None:
            current_friend = WeiboUser(id=f_id)
            db_session.add(current_friend)
            db_session.commit()
            followed = WeiboRelation(follower_id=uid, followed_id=f_id)
            db_session.add(followed)
        current_friend.uid = uid
        current_friend.screen_name = friend['screen_name']
        current_friend.description = friend['description']
        current_friend.profile_url = friend['profile_url']
        current_friend.gender = basic.analyze_gender(friend['gender'])
        current_friend.follow_me = friend['follow_me']
        current_friend.data_set = str(friend)
        current_user.followed.append(current_friend)
        db_session.commit()
    return