def get_rate_results(request, lex): results = {'success':'False'} u = request.user rating = lex[u'rating'] if(lex.has_key(u'status')): sid = int(lex[u'status']) try: status = Status.objects.get(id=sid) except Status.DoesNotExist: api = get_authorized_twython(request.session['twitter_tokens']) status_json = api.showStatus(id=lex[u'status']) status = Status.construct_from_dict(status_json) else: api = get_authorized_twython(request.session['twitter_tokens']) status_json = api.showStatus(id=lex[u'id']) status = Status.construct_from_dict(status_json) # Show user if tweet delivered from Search API, which does not have correct userid # TODO: a more elegant solution if not status.user.id: api = get_authorized_twython(request.session['twitter_tokens']) api_user = api.showUser(screen_name=status.user.screen_name) setattr(status, 'user', TwitterUserProfile.construct_from_dict(api_user)) tp = TwitterUserProfile.objects.get(id=request.session['twitter_tokens']['user_id']) prof = u.get_profile() status.save_with_user(is_cached=False) try: details = CachedStatus.objects.get(user=tp.id, status=status.id) details.prediction -= 2.0 details.save() except CachedStatus.DoesNotExist: pass if rating == 'up': rating_int = 1 elif rating == 'down': rating_int = -1 try: r = Rating.objects.get(status=status, user=tp) except: r = Rating(status=status, user=tp) # prof.whale.exp += 1 # if prof.whale.exp == prof.whale.species.evolution.minExp: prof.whale.species = prof.whale.species.evolution # prof.whale.save() r.rating = rating_int r.save() results['success'] = 'True' # results['exp'] = prof.whale.exp # results['min-exp'] = prof.whale.species.minExp # results['max-exp'] = prof.whale.species.evolution.minExp # results['species'] = prof.whale.species.img.url # results['speciesName'] = prof.whale.species.name return results
def ajax_timeline(request): g = request.GET if not request.user.is_authenticated() or 'twitter_tokens' not in request.session: return HttpResponseBadRequest('not authenticated') twitter_tokens = request.session['twitter_tokens'] api = get_authorized_twython(twitter_tokens) tp = TwitterUserProfile.objects.get(id=twitter_tokens['user_id']) if not g.has_key(u'page'): return HttpResponseBadRequest('page number missing') page, feedtype = int(g[u'page']), g[u'timeline'] # Get maxid for certain types of timelines try: maxid = int(g[u'id']) except KeyError: maxid = None # Force cache fill if no statuses loaded originally if page == 1: cache_timeline_backfill(tp, twitter_tokens, None) if feedtype == 'normal': statuses = normal_timeline(api, tp, page, maxid) elif feedtype == 'reorder': statuses = reorder_timeline(api, tp, page) elif feedtype == 'filter': statuses = filter_timeline(api, tp, page, maxid) elif feedtype == 'predict': statuses = predict_timeline(api, tp, page, maxid) if len(statuses) == 0: return HttpResponse('Loading') Rating.appendTo(statuses, tp) return render_to_response('twitter/status_list.html', { 'statuses': statuses }, context_instance=RequestContext(request))
def ajax_user_timeline(request): if not request.user.is_authenticated() or 'twitter_tokens' not in request.session: return HttpResponse("") results = {'success': 'False'} if request.method != u'GET': return HttpResponseBadRequest('Must be GET request') if not request.GET.has_key(u'screenname'): return HttpResponseBadRequest('screenname missing') if not request.GET.has_key(u'max_id'): return HttpResponseBadRequest('start id missing') if not request.GET.has_key(u'page'): return HttpResponseBadRequest('page number missing') screenname = request.GET[u'screenname'] max_id = request.GET[u'max_id'] page = request.GET[u'page'] if 'twitter_tokens' in request.session: twitter_tokens = request.session['twitter_tokens'] api = get_authorized_twython(twitter_tokens) else: # Get public api if no authentication possible api = Twython() results['statuses'] = api.getUserTimeline(screen_name=screenname, max_id=max_id, page=page) t = get_template('twitter/status_list.html') results['success'] = 'True' html = t.render(RequestContext(request, results)) return HttpResponse(html)
def filtered_friends_timeline(request): twitter_tokens = request.session['twitter_tokens'] api = get_authorized_twython(twitter_tokens) response = get_filtered_friends_timeline(request) friends = api.getFriendsStatus() response['friends'] = friends results = dict(response, **{'feedtype': 'filter'}) return results
def reordered_friends_timeline(request): prof = request.user.get_profile() tp = TwitterUserProfile.objects.get(user=prof) twitter_tokens = request.session['twitter_tokens'] api = get_authorized_twython(twitter_tokens) statuses = reorder_timeline(api, tp, 1) friends = api.getFriendsStatus() Rating.appendTo(statuses, tp) return {'statuses': statuses, 'friends': friends, 'feedtype': 'reorder'}
def get_filtered_friends_timeline(request): prof = request.user.get_profile() tp = TwitterUserProfile.objects.get(user=prof) twitter_tokens = request.session['twitter_tokens'] api = get_authorized_twython(twitter_tokens) statuses = Status.construct_from_dicts(api.getFriendsTimeline()) predictions = get_predictions(tp, statuses) filtered_statuses = [] for s, r in zip(statuses, predictions): if r >= 0: filtered_statuses.append(s) Rating.appendTo(statuses, tp) return {'statuses': filtered_statuses, 'feedtype': 'filter'}
def ajax_search(request): if not request.user.is_authenticated() or 'twitter_tokens' not in request.session: return HttpResponse("") if request.is_ajax(): term = request.GET.get('q') page = request.GET.get('page') if (term is not None and page is not None): api = get_authorized_twython(request.session['twitter_tokens']) statuses = Status.construct_from_search_dicts(api.searchTwitter(q=term, page=page)[u'results']) return render_to_response('twitter/status_list.html', { 'statuses': statuses }, context_instance=RequestContext(request)) return HttpResponse('')
def destroy_friendship(request): results = {'success':'False'} if not request.user.is_authenticated() or 'twitter_tokens' not in request.session: return HttpResponse("") twitter_tokens = request.session['twitter_tokens'] API = get_authorized_twython(twitter_tokens) username = request.POST[u'friend_username'] try: API.destroyFriendship(user_id=username) results['success'] = 'True' except TwythonError: pass jsonResults = json.dumps(results) return HttpResponse(jsonResults, mimetype='application/json')
def post_status(request): results = {'success':'False'} if not request.user.is_authenticated() or 'twitter_tokens' not in request.session: return HttpResponse("") twitter_tokens = request.session['twitter_tokens'] API = get_authorized_twython(twitter_tokens) status = request.POST[u'status'] try: API.updateStatus(status=status) results['success'] = 'True' except TwythonError: pass jsonResults = json.dumps(results) return HttpResponse(jsonResults, mimetype='application/json')
def index(request): if not request.user.is_authenticated() or 'twitter_tokens' not in request.session: return HttpResponseRedirect("/") term = request.GET.get('q') if term is not None: api = get_authorized_twython(request.session['twitter_tokens']) statuses = Status.construct_from_search_dicts(api.searchTwitter(q=term)[u'results']) return render_to_response('search_index.html', { 'statuses': statuses, 'term': term }, context_instance=RequestContext(request)) else: return HttpResponseRedirect("/")
def predicted_friends_timeline(request): prof = request.user.get_profile() tp = TwitterUserProfile.objects.get(user=prof) twitter_tokens = request.session['twitter_tokens'] api = get_authorized_twython(twitter_tokens) statuses = Status.construct_from_dicts(api.getFriendsTimeline()) friends = api.getFriendsStatus() predictions = get_predictions(tp, statuses) for s, r in zip(statuses, predictions): if r >= 0: s.likeClass = ' active' s.dislikeClass = ' inactive' if r < 0: s.likeClass = ' inactive' s.dislikeClass = ' active' return {'statuses': statuses, 'friends': friends, 'feedtype': 'predict'}
def timeline(request): user = request.user if not user.is_authenticated() or 'twitter_tokens' not in request.session: return render_to_response('landing.html') twitter_tokens = request.session['twitter_tokens'] tp = TwitterUserProfile.objects.get(id=twitter_tokens['user_id']) api = get_authorized_twython(twitter_tokens) statuses = Status.construct_from_dicts(api.getFriendsTimeline(include_rts=True)) cache_timeline_backfill.delay(tp, twitter_tokens, statuses) friends = api.getFriendsStatus() Rating.appendTo(statuses, tp) return render_to_response('twitter/timeline.html', { 'statuses': statuses, 'friends': friends, 'feedtype': 'normal' }, context_instance=RequestContext(request))
def search(request): if not request.user.is_authenticated() or 'twitter_tokens' not in request.session: return HttpResponseRedirect("/") term = request.GET.get('q') if term is not None: prof = request.user.get_profile() twitter_tokens = request.session['twitter_tokens'] api = get_authorized_twython(twitter_tokens) tp = TwitterUserProfile.objects.get(id=twitter_tokens['user_id']) statuses = Status.construct_from_search_dicts(api.searchTwitter(q=term)[u'results']) friends = api.getFriendsStatus() Rating.appendTo(statuses, tp) return render_to_response('twitter/search_index.html', { # 'whale': prof.whale, 'friends': friends, 'statuses': statuses, 'term': term }, context_instance=RequestContext(request)) else: return HttpResponseRedirect("/")
def public_profile(request, username): if request.user.is_authenticated() and 'twitter_tokens' in request.session: twitter_tokens = request.session['twitter_tokens'] api = get_authorized_twython(twitter_tokens) else: # Require login return HttpResponseRedirect("/") friend = api.showUser(screen_name=username) friends = api.getFriendsStatus() prof = request.user.get_profile() tp = TwitterUserProfile.objects.get(user=prof) follow_request_sent = True is_true_friend = friend['following'] is_me = tp.id == friend['id'] if not is_true_friend: is_true_friend = False outgoing = api.friendshipsOutgoing() follow_request_sent = False if friend['id'] in outgoing['ids']: # if we have already requested to follow this person follow_request_sent = True if friend['protected'] and not is_true_friend: statuses = None else: try: statuses = Status.construct_from_dicts(api.getUserTimeline(screen_name=username)) Rating.appendTo(statuses, tp) except TwythonError: statuses = None return render_to_response('twitter/public_profile.html', { 'friends': friends, 'username': username, 'friend': friend, 'is_true_friend' : is_true_friend, 'is_me' : is_me, 'profile_protected' : friend['protected'], 'follow_request_sent': follow_request_sent, 'statuses' : statuses, }, context_instance=RequestContext(request))
def cache_timeline_backfill_callback(sender, **kwargs): """ Backfill cached timeline from the oldest tweet in statuses to the cached_time in TwitterUserProfile or 72 hours, whichever is sooner""" statuses, tp = kwargs['statuses'], kwargs['twitter_user_profile'] twitter_tokens = kwargs['twitter_tokens'] api = get_authorized_twython(twitter_tokens) oldest_time = datetime.now()-timedelta(hours=72) """ backfill_start = min(statuses, key=lambda x: x.created_at) backfill_end = max([tp.cached_time, oldest_time]) if backfill_start < backfill_end: return """ backfill_maxid = min(statuses, key=lambda x: x.id).id try: backfill_minid = max(tp.cached_statuses.filter(created_at__gt=oldest_time), key=lambda x: x.id).id if backfill_maxid < backfill_minid: return except IndexError: backfill_minid = None # print "backfill minid: " + str(backfill_minid) # print "backfill maxid: " + str(backfill_maxid) cache_timeline_signal.send(sender=sender, statuses=statuses, twitter_user_profile=tp) finished = False total_num_statuses = len(statuses) while not finished: recieved_statuses = Status.construct_from_dicts( api.getFriendsTimeline(count=200, include_rts=True, max_id=backfill_maxid, min_id=backfill_minid)) total_num_statuses += len(recieved_statuses) cache_timeline_signal.send(sender=sender, statuses=recieved_statuses, twitter_user_profile=tp) if total_num_statuses >= 600 or len(recieved_statuses) < 200: finished = True else: backfill_maxid = statuses[-1].id
def twitter_return(request, window_type): """ A user gets redirected here after hitting Twitter and authorizing your app to use their data. This is the view that stores the tokens you want for querying data. Pay attention to this. """ # Now that we've got the magic tokens back from Twitter, we need to exchange # for permanent ones and store them... api = Twython( twitter_token = CONSUMER_KEY, twitter_secret = CONSUMER_SECRET, oauth_token = request.session['request_token']['oauth_token'], oauth_token_secret = request.session['request_token']['oauth_token_secret'] ) # Retrieve the tokens we want... twitter_tokens = api.get_authorized_tokens() api = get_authorized_twython(twitter_tokens) username = '******'.join([str(twitter_tokens['user_id']), 'twitter']) request.session['twitter_tokens'] = twitter_tokens # No need to call authorize because of get_authorized_tokens() # Probable bug - need to fix when TwitterUserProfile already exists without # user having logged in. try: user = User.objects.get(username=username) up = UserProfile.objects.get(user=user) twitter_user = TwitterUserProfile.objects.get(user=up) except: # Create User, UserProfile, TwitterUserProfile twitter_user = api.verifyCredentials() if 'error' in twitter_user: del request.session['access_token'] del request.session['request_token'] return HttpResponse("Unable to authenticate you!") name = twitter_user['name'].split() if(len(name) > 0): first_name = name[0] else: first_name = "Unknown User" if(len(name) > 1): last_name = name[1] else: last_name = "" user = User(username=username, first_name=first_name, last_name=last_name) user.set_unusable_password() user.save() up = UserProfile(user=user) # whale = Whale(species=WhaleSpecies.getDefaultSpecies()) # whale.save() # up.whale = whale up.save() try: tp = TwitterUserProfile.objects.get(user_id=user.id) except TwitterUserProfile.DoesNotExist: tp = TwitterUserProfile() tp.__dict__.update(twitter_user) tp.user = up tp.oauth_token = twitter_tokens['oauth_token'] tp.oauth_secret = twitter_tokens['oauth_token_secret'] tp.active_classifier = DEFAULT_CLASSIFIER tp.classifier_version = DEFAULT_CLASSIFIER_VERSION tp.save() # Hack so don't have to call authenticate user.backend = 'twitter.auth.TwitterAuthentication' login(request, user) if window_type == 'popup': return render_to_response("twitter/twitter_return.html", {'twitter_user': twitter_user}, context_instance=RequestContext(request)) elif window_type == 'api': return HttpResponse() else: return HttpResponseRedirect("/")
def get_user_timeline_object(request, identifier, pageNum=0): twitter_tokens = request.session['twitter_tokens'] api = get_authorized_twython(twitter_tokens) return api.getUserTimeline(id=identifier, page=pageNum)
def cache_timeline_backfill(tp, twitter_tokens, statuses): """ Backfill cached timeline from the oldest tweet in statuses to the cached_time in TwitterUserProfile or 72 hours, whichever is sooner""" api = get_authorized_twython(twitter_tokens) cutoff_time = datetime.utcnow()-timedelta(hours=72) if not statuses: statuses = Status.construct_from_dicts(api.getFriendsTimeline(include_rts=True)) backfill_maxid = min(statuses, key=lambda x: x.id).id backfill_newestid = max(statuses, key=lambda x: x.id).id # Maxid and minid indicate contiguous cached status ids minid = getattr(tp, 'cached_minid', 0) maxid = getattr(tp, 'cached_maxid', 0) # No new tweets at all if backfill_newestid == maxid: return # Only one page of new tweets - just cache these if backfill_maxid < maxid: cache_statuses(statuses, tp) return # Cache as far back as 800 tweets or 72 hours worth num_apicalls = 1 finished = False total_num_statuses = len(statuses) while not finished: print "backfill minid: " + str(maxid) print "backfill maxid: " + str(backfill_maxid) recieved_statuses = Status.construct_from_dicts( api.getFriendsTimeline(count=200, include_rts=True, max_id=backfill_maxid, min_id=maxid)) num_apicalls += 1 total_num_statuses += len(recieved_statuses) statuses.extend(recieved_statuses) oldest_status = min(recieved_statuses, key=lambda x: x.id) if (oldest_status.created_at < cutoff_time or oldest_status.id <= maxid or num_apicalls >= 5): finished = True else: backfill_maxid = oldest_status.id # Set new minid, maxid for contiguous cached statuses if oldest_status.id <= maxid: tp.cached_minid = minid else: tp.cached_minid = oldest_status.id tp.cached_maxid = backfill_newestid tp.save() # If less than 50 rated statuses: force train user classifier if Rating.objects.filter(user=tp).count() < 50: force_train(tp) # print "num apicalls: " + str(num_apicalls) cache_statuses(statuses, tp)