예제 #1
0
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))
예제 #2
0
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))
예제 #3
0
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
예제 #4
0
def normal_timeline(api, tp, page, maxid):
    if tp.cached_maxid >= maxid >= tp.cached_minid:
        return tp.cached_statuses.filter(id__lt=maxid)[:20]
    else:
        return Status.construct_from_dicts(api.getFriendsTimeline(page=page))
예제 #5
0
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)