示例#1
0
def get_followers_or_followees(twitter_user_id,
                               twitter_url,
                               retry=0,
                               oauth=False,
                               access_token=''):
    """Return a list of followers or followees of a Twitter user"""
    x = []
    tw = None
    params = {'user_id': twitter_user_id, 'cursor': '-1'}
    try:
        px = urlencode(params)
        if oauth:
            if not access_token:
                tw_prof = TwitterProfile.objects.active().filter(
                    appuser_id=twitter_user_id).exclude(
                        access_token=u'').order_by("-updated_on")[:1].get()
                access_token = tw_prof.access_token
            tw = TwitterAPI()
            if PROTOCOL == 'https':
                twitter_url = twitter_url.replace('http:', 'https:')
            _url = twitter_url % px
            _log.debug("OAuth URL %s" % _url)
            ret = tw.get_url(access_token, _url)
            tw.close()
        else:
            _url = twitter_url % px
            _log.debug(_url)
            req = urllib2.Request(_url)
            req.add_header("Content-type", "application/x-www-form-urlencoded")
            req.add_header('User-Agent', settings.USER_AGENT)
            resp = urllib2.urlopen(req)
            ret = resp.read()
            resp.close()
        results = json.loads(ret)
        if type(results) == dict:
            x = results.get('ids', [])
        else:
            x = results
        time.sleep(settings.TWITTER_SLEEP)
        return x
    except urllib2.HTTPError, h:
        if h.code == 401 and retry < 2:
            # Try with OAuth
            return get_followers_or_followees(twitter_user_id,
                                              twitter_url,
                                              retry=retry + 1,
                                              oauth=True,
                                              access_token=access_token)
        _log.error("%s failed for twitter id: %s (HTTP Error)", twitter_url,
                   twitter_user_id)
        _log.exception(h)
        if h.code in (503, 502, 420):
            time.sleep(30 + retry * 30)  # throttle
        if x or retry > 1:
            return x
        return get_followers_or_followees(twitter_user_id,
                                          twitter_url,
                                          retry=retry + 1,
                                          oauth=oauth,
                                          access_token=access_token)
示例#2
0
文件: utils.py 项目: kabirh/riotvine
def get_followers_or_followees(twitter_user_id, twitter_url, retry=0, oauth=False, access_token=''):
    """Return a list of followers or followees of a Twitter user"""
    x = []
    tw = None
    params = {'user_id':twitter_user_id, 'cursor':'-1'}
    try:
        px = urlencode(params)
        if oauth:
            if not access_token:
                tw_prof = TwitterProfile.objects.active().filter(
                    appuser_id=twitter_user_id
                ).exclude(access_token=u'').order_by("-updated_on")[:1].get()
                access_token = tw_prof.access_token
            tw = TwitterAPI()
            if PROTOCOL == 'https':
                twitter_url = twitter_url.replace('http:', 'https:')
            _url = twitter_url % px
            _log.debug("OAuth URL %s" % _url)
            ret = tw.get_url(access_token, _url)
            tw.close()
        else:
            _url = twitter_url % px
            _log.debug(_url)
            req = urllib2.Request(_url)
            req.add_header("Content-type", "application/x-www-form-urlencoded")
            req.add_header('User-Agent', settings.USER_AGENT)
            resp = urllib2.urlopen(req)
            ret = resp.read()
            resp.close()
        results = json.loads(ret)
        if type(results) == dict:
            x = results.get('ids', [])
        else:
            x = results
        time.sleep(settings.TWITTER_SLEEP)
        return x
    except urllib2.HTTPError, h:
        if h.code == 401 and retry < 2:
            # Try with OAuth
            return get_followers_or_followees(twitter_user_id, twitter_url, retry=retry+1, oauth=True, access_token=access_token)
        _log.error("%s failed for twitter id: %s (HTTP Error)", twitter_url, twitter_user_id)
        _log.exception(h)
        if h.code in (503, 502, 420):
            time.sleep(30 + retry*30) # throttle
        if x or retry > 1:
            return x
        return get_followers_or_followees(twitter_user_id, twitter_url, retry=retry+1, oauth=oauth, access_token=access_token)