def search(request): auth_status = check_auth(request) if auth_status: query_string = request.GET.get('q') user_id = request.session['buzz_user_id'] conn = get_redis_conn() user_dao = UserDao(conn) user = user_dao.get_user(user_id) oauth_token = user.oauth_token oauth_token_secret = user.oauth_token_secret authorized_token = oauth.Token(oauth_token, oauth_token_secret) user_client =oauth.Client(consumer, authorized_token) q = urllib.urlencode({"q":query_string}) search_url = SEARCH_URL+"&"+q resp, content = user_client.request(search_url) try: if resp['status'] != '200': error_message = "Invalid response received: %s" % resp['status'] return HttpResponseRedirect(settings.BUZZFIRE_ERROR) except KeyError: return HttpResponseRedirect(settings.BUZZFIRE_ERROR) return HttpResponse(content, content_type = "application/json") else: return HttpResponseRedirect(settings.BUZZFIRE_LOGIN_URL)
def get_timeline(request): auth_status = check_auth(request) if auth_status: user_id = request.session['buzz_user_id'] conn = get_redis_conn() user_dao = UserDao(conn) user = user_dao.get_user(user_id) oauth_token = user.oauth_token oauth_token_secret = user.oauth_token_secret authorized_token = oauth.Token(oauth_token, oauth_token_secret) user_client =oauth.Client(consumer, authorized_token) resp, content = user_client.request(USER_TIMELINE_URL) try: if resp['status'] != '200': error_message = "Invalid response received: %s" % resp['status'] return HttpResponseRedirect(settings.BUZZFIRE_ERROR) except KeyError: return HttpResponseRedirect(settings.BUZZFIRE_ERROR) return HttpResponse(content, content_type = "application/json") else: return HttpResponseRedirect(settings.BUZZFIRE_LOGIN_URL)
def auth_user(request): token = oauth.Token(request.session['request_token']['oauth_token'], request.session['request_token']['oauth_token_secret']) auth_client = oauth.Client(consumer, token) resp, content = auth_client.request(ACCESS_TOKEN_URL, "GET") if resp['status'] != '200': #raise Exception("Invalid response from Twitter.") return HttpResponseRedirect(settings.BUZZFIRE_ERROR) access_token = dict(urlparse.parse_qsl(content)) # Lookup user or create conn = get_redis_conn() user_dao = UserDao(conn) user_id = user_dao.get_user_id(access_token['screen_name']) if user_id == None: # create user screen_name = access_token['screen_name'] new_user = User(screen_name, user_id, oauth_token_secret=access_token['oauth_token_secret'], oauth_token=access_token['oauth_token']) user_id=user_dao.save(new_user) else: # update existing user user = user_dao.get_user(user_id) user.oauth_token_secret = access_token['oauth_token_secret'] user.oauth_token = oauth_token=access_token['oauth_token'] user_id=user_dao.save(user) request.session['buzz_user_id'] = user_id # redirect to user homepage return HttpResponseRedirect(settings.BUZZFIRE_USER_PAGE)
def update_status(request): auth_status = check_auth(request) if auth_status: user_id = request.session['buzz_user_id'] if request.method == 'POST': status = request.POST['status'] if request.POST.has_key('tweet_id'): tweet_id = request.POST['tweet_id'] else: tweet_id =None conn = get_redis_conn() user_dao = UserDao(conn) user = user_dao.get_user(user_id) oauth_token = user.oauth_token oauth_token_secret = user.oauth_token_secret authorized_token = oauth.Token(oauth_token, oauth_token_secret) user_client =oauth.Client(consumer, authorized_token) if tweet_id: q = urllib.urlencode({"status":status, "in_reply_to_status_id":tweet_id}) else: q =urllib.urlencode({"status":status}) resp, content = user_client.request(STATUS_UPDATE_URL, method="POST", body=q) try: if resp['status'] != '200': error_message = "Invalid response received: %s" % resp['status'] else: return HttpResponse() except KeyError: raise Exception('Did not get a proper response') return HttpResponse('{"status":"Success"}') else: return HttpResponseRedirect(settings.BUZZFIRE_LOGIN_URL)