Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 58 lines (47 sloc) 1.702 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
import logging
import tornado.escape
import tornado.auth
import tornado.ioloop
from celery.task import task
from celery import conf
import settings
from models import Tweeter, connection



@task
def refresh_user_info(*args, **kwargs):
    try:
        _refresh_user_info(*args, **kwargs)
    except:
        logging.error("_refresh_user_info() failed", exc_info=True)
        if conf.ALWAYS_EAGER:
            raise

def _refresh_user_info(username, access_token):
    #from time import sleep; sleep(5)
    uu = UserUpdate()
    def cb(r, *args, **kwargs):
        try:
            uu.callback(username, r)
        finally:
            if not conf.ALWAYS_EAGER:
                tornado.ioloop.IOLoop.instance().stop()
    uu.twitter_request("/users/show", cb, access_token=access_token,
                       screen_name=username)
    if not conf.ALWAYS_EAGER:
        tornado.ioloop.IOLoop.instance().start()


class UserUpdate(tornado.auth.TwitterMixin):
    def __init__(self):
        self.settings = dict(
            twitter_consumer_key=settings.TWITTER_CONSUMER_KEY,
            twitter_consumer_secret=settings.TWITTER_CONSUMER_SECRET,
        )

    @property
    def db(self):
        return connection[settings.DATABASE_NAME]

    def require_setting(self, key, error):
        assert key in self.settings, "%s (%s)" % (error, key)

    def async_callback(self, func, callback):
        return callback

    def callback(self, username, response):
        result = tornado.escape.json_decode(response.body)
        tweeter = self.db.Tweeter.find_one({'user_id': result['id']})
        assert tweeter['username'].lower() == username.lower()
        Tweeter.update_tweeter(tweeter, result)
Something went wrong with that request. Please try again.