def _get_media(self, api, mid): media = spawn(api.media, mid) likes = spawn(api.media_likes, media_id=mid) gevent.joinall([media, likes]) media, likes = media.get(), likes.get() errors = get_errors(media, likes) if errors: if any([e.error_type == 'APINotAllowedError' for e in errors]): return render('profile-noauth.html', ukey=request.ukey) app.logger.error([str(e) for e in errors]) return notfound(u'服务器暂时出问题了') ukey = media.user.id isfollow = False if request.ukey: try: isfollow = is_follow(ukey, api) except InstagramAPIError: return notfound(u'服务器暂时出问题了') isstar = False for i in likes: if request.ukey and request.ukey == i.id: isstar = True isme = False if request.ukey and ukey == request.ukey: isme = True return dict(media=media, isme=isme, isfollow=isfollow, likes=likes[:5], isstar=isstar)
def get(self, ukey): next_url = request.args.get('next_url', None) if next_url and 'instagram' not in next_url: next_url = signer.loads(next_url) api = InstagramAPI(access_token=request.access_token) user = gevent.spawn(wrap_errors(InstagramAPIError, api.user), user_id=ukey) feeds = gevent.spawn(wrap_errors(InstagramAPIError, api.user_recent_media), user_id=ukey, with_next_url=next_url) if request.ukey: isfollows = spawn(isfollow, ukey, api) else: isfollows = spawn(lambda x: False, ukey) gevent.joinall([user, feeds, isfollows]) user, feeds, isfollows = user.get(), feeds.get(), isfollows.get() errors = [e for e in (user, feeds, isfollows) if isinstance(e, InstagramAPIError)] if errors: if any([e.error_type == 'APINotAllowedError' for e in errors]): return render('profile-noauth.html', ukey=ukey) if any([e.error_type == 'APINotFoundError' for e in errors]): return notfound(u'用户不存在') app.logger.error([str(e) for e in errors]) return apierror(u'服务器暂时出问题了') next_url = feeds[1] if feeds else None next_url = signer.dumps(next_url) if next_url else next_url feeds = feeds[0] if feeds else [] isme = False if request.ukey and ukey == request.ukey: isme = True return render( 'profile.html', user=user, feeds=feeds, isme=isme, isfollow=isfollows, next_url=next_url )
def _get_users(self, ukey, user_type='followed'): next_url = request.args.get('next_url', None) if next_url and 'instagram' not in next_url: next_url = signer.loads(next_url) api = InstagramAPI(access_token=request.access_token) user = spawn(api.user, ukey) if user_type == 'following': users = spawn(api.user_follows, ukey, with_next_url=next_url) else: users = spawn(api.user_followed_by, ukey, with_next_url=next_url) isfollows = False if request.ukey: isfollows = spawn(isfollow, ukey, api) else: isfollows = spawn(lambda x: False, ukey) gevent.joinall([user, users, isfollows]) user, users, isfollows = user.get(), users.get(), isfollows.get() errors = get_errors(user, users, isfollows) if errors: app.logger.error([str(e) for e in errors]) return notfound(u'服务器暂时出问题了') next_url = users[1] next_url = signer.dumps(next_url) if next_url else next_url users = users[0] isme = False if request.ukey and ukey == request.ukey: isme = True context = { 'user': user, 'users': users, 'next_url': next_url, 'isfollows': isfollows, 'isme': isme, } return context