Пример #1
0
def TwitterUserTimelineHandler(request, screen_name):
    if request.method == 'GET':
        site = GetSite()
        member = CheckAuth(request)
        if member:
            if member.twitter_oauth == 1:
                template_values = {}
                template_values['site'] = site
                template_values['rnd'] = random.randrange(1, 100)
                template_values['member'] = member
                l10n = GetMessages(member, site)
                template_values['l10n'] = l10n
                template_values[
                    'page_title'] = site.title + u' › Twitter › ' + screen_name
                template_values['screen_name'] = screen_name
                access_token = OAuthToken.from_string(
                    member.twitter_oauth_string)
                twitter = OAuthApi(config.twitter_consumer_key,
                                   config.twitter_consumer_secret,
                                   access_token)
                rate_limit = memcache.get(
                    str(member.twitter_id) + '::rate_limit')
                if rate_limit is None:
                    try:
                        rate_limit = twitter.GetRateLimit()
                        memcache.set(
                            str(member.twitter_id) + '::rate_limit',
                            rate_limit, 60)
                    except:
                        logging.info('Failed to get rate limit for @' +
                                     member.twitter_screen_name)
                template_values['rate_limit'] = rate_limit
                cache_tag = 'twitter::' + screen_name + '::home'
                statuses = memcache.get(cache_tag)
                if statuses is None:
                    statuses = twitter.GetUserTimeline(user=screen_name,
                                                       count=50)
                    i = 0
                    for status in statuses:
                        statuses[i].source = statuses[i].source.replace(
                            '<a', '<a def="dark"')
                        statuses[i].datetime = datetime.datetime.fromtimestamp(
                            time.mktime(
                                time.strptime(status.created_at,
                                              '%a %b %d %H:%M:%S +0000 %Y')))
                        statuses[i].text = twitter.ConvertMentions(status.text)
                        #statuses[i].text = twitter.ExpandBitly(status.text)
                        i = i + 1
                    memcache.set(cache_tag, statuses, 120)
                template_values['statuses'] = statuses
                path = os.path.join('desktop', 'twitter_user.html')
                return render_to_response(path, template_values)
            else:
                return HttpResponseRedirect('/settings/')
        else:
            return HttpResponseRedirect('/')
Пример #2
0
def TwitterDMInboxHandler(request):
    if request.method == 'GET':
        member = CheckAuth(request)
        site = GetSite()
        if member:
            if member.twitter_oauth == 1:
                template_values = {}
                template_values['site'] = site
                template_values['rnd'] = random.randrange(1, 100)
                template_values['member'] = member
                l10n = GetMessages(member, site)
                template_values['l10n'] = l10n
                template_values[
                    'page_title'] = site.title + u' › Twitter › Direct Messages › Inbox'
                access_token = OAuthToken.from_string(
                    member.twitter_oauth_string)
                twitter = OAuthApi(config.twitter_consumer_key,
                                   config.twitter_consumer_secret,
                                   access_token)
                rate_limit = memcache.get(
                    str(member.twitter_id) + '::rate_limit')
                if rate_limit is None:
                    try:
                        rate_limit = twitter.GetRateLimit()
                        memcache.set(
                            str(member.twitter_id) + '::rate_limit',
                            rate_limit, 60)
                    except:
                        logging.info('Failed to get rate limit for @' +
                                     member.twitter_screen_name)
                template_values['rate_limit'] = rate_limit
                cache_tag = 'member::' + str(
                    member.num) + '::twitter::dm::inbox'
                messages = memcache.get(cache_tag)
                if messages is None:
                    messages = twitter.GetDirectMessages()
                    i = 0
                    for message in messages:
                        messages[i].datetime = datetime.datetime.fromtimestamp(
                            time.mktime(
                                time.strptime(message.created_at,
                                              '%a %b %d %H:%M:%S +0000 %Y')))
                        messages[i].text = twitter.ConvertMentions(
                            message.text)
                        #statuses[i].text = twitter.ExpandBitly(status.text)
                        i = i + 1
                    memcache.set(cache_tag, messages, 120)
                template_values['messages'] = messages
                path = os.path.join('desktop', 'twitter_dm_inbox.html')
                return render_to_response(path, template_values)
            else:
                return HttpResponseRedirect('/settings')
        else:
            return HttpResponseRedirect('/')