How to use the twitter.TwitterError function in twitter

To help you get started, we’ve selected a few twitter examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github codl / forget / libforget / twitter.py View on Github external
'count': 200,
                'trim_user': True,
                'tweet_mode': 'extended',
                }
        if cursor:
            kwargs.update(cursor)

        if 'max_id' not in kwargs:
            most_recent_post = (
                    Post.query.order_by(db.desc(Post.created_at))
                    .filter(Post.author_id == account.id).first())
            if most_recent_post:
                kwargs['since_id'] = most_recent_post.twitter_id

        tweets = t.statuses.user_timeline(**kwargs)
    except (TwitterError, URLError) as e:
        handle_error(e)

    print("processing {} tweets for {acc}".format(len(tweets), acc=account))

    if len(tweets) > 0:

        kwargs['max_id'] = +inf

        for tweet in tweets:
            db.session.merge(post_from_api_tweet_object(tweet))
            kwargs['max_id'] = min(tweet['id'] - 1, kwargs['max_id'])

    else:
        kwargs = None

    db.session.commit()
github edolopez / Marketweet / twitter.py View on Github external
Returns:
      A sequence of twitter.Status instances, one for each message
    '''
    if not user and not self._oauth_consumer:
      raise TwitterError("User must be specified if API is not authenticated.")
    url = '%s/statuses/friends_timeline' % self.base_url
    if user:
      url = '%s/%s.json' % (url, user)
    else:
      url = '%s.json' % url
    parameters = {}
    if count is not None:
      try:
        if int(count) > 100:
          raise TwitterError("'count' may not be greater than 100")
      except ValueError:
        raise TwitterError("'count' must be an integer")
      parameters['count'] = count
    if page is not None:
      try:
        parameters['page'] = int(page)
      except ValueError:
        raise TwitterError("'page' must be an integer")
    if since_id:
      parameters['since_id'] = since_id
    if retweets:
      parameters['include_rts'] = True
    if include_entities:
      parameters['include_entities'] = True
    json = self._FetchUrl(url, parameters=parameters)
    data = self._ParseAndCheckTwitter(json)
github tom-martin / markovator / twitter.py View on Github external
def get_timeline_tweets(count):
    client = oauth.Client(twitter_settings.consumer, twitter_settings.token)
    
    resp, content = client.request('https://api.twitter.com/1.1/statuses/home_timeline.json?count=' + str(count), "GET")
    
    if resp.status != 200:
        raise TwitterError(resp.status, content)
    
    return json.loads(content)
github dhananjaysathe / python-twitter-with-proxy / twitter.py View on Github external
def GetFollowers(self, page=None):
    '''Fetch the sequence of twitter.User instances, one for each follower

    The twitter.Api instance must be authenticated.

    Args:
      page:
        Specifies the page of results to retrieve.
        Note: there are pagination limits. [Optional]

    Returns:
      A sequence of twitter.User instances, one for each follower
    '''
    if not self._oauth_consumer:
      raise TwitterError("twitter.Api instance must be authenticated")
    url = '%s/statuses/followers.json' % self.base_url
    parameters = {}
    if page:
      parameters['page'] = page
    json = self._FetchUrl(url, parameters=parameters)
    data = self._ParseAndCheckTwitter(json)
    return [User.NewFromJsonDict(x) for x in data]
github edolopez / Marketweet / twitter.py View on Github external
def GetFriendIDs(self, user=None, cursor=-1):
      '''Returns a list of twitter user id's for every person
      the specified user is following.

      Args:
        user:
          The id or screen_name of the user to retrieve the id list for
          [Optional]

      Returns:
        A list of integers, one for each user id.
      '''
      if not user and not self._oauth_consumer:
          raise TwitterError("twitter.Api instance must be authenticated")
      if user:
          url = '%s/friends/ids/%s.json' % (self.base_url, user)
      else:
          url = '%s/friends/ids.json' % self.base_url
      parameters = {}
      parameters['cursor'] = cursor
      json = self._FetchUrl(url, parameters=parameters)
      data = self._ParseAndCheckTwitter(json)
      return data
github edolopez / Marketweet / twitter.py View on Github external
"""Try and parse the JSON returned from Twitter and return
    an empty dictionary if there is any error. This is a purely
    defensive check because during some Twitter network outages
    it will return an HTML failwhale page."""
    try:
      data = simplejson.loads(json)
      self._CheckForTwitterError(data)
    except ValueError:
      if "<title>Twitter / Over capacity</title>" in json:
        #raise TwitterError("Capacity Error")
        print TwitterError("Capacity Error")
      if "<title>Twitter / Error</title>" in json:
        #raise TwitterError("Technical Error")
        print TwitterError("Technical Error")
      #raise TwitterError("json decoding")
      print TwitterError("json decoding")
      data = {}     # As the program is never stopped, we need to return something at least

    return data
github Nic0 / tyrs / src / tyrs / tweets.py View on Github external
def PostRetweet(self, id):
        '''This code come from issue #130 on python-twitter tracker'''

        if not self._oauth_consumer:
            raise TwitterError("The twitter.Api instance must be authenticated.")
        try:
            if int(id) &lt;= 0:
                raise TwitterError("'id' must be a positive number")
        except ValueError:
            raise TwitterError("'id' must be an integer")
        url = 'http://api.twitter.com/1/statuses/retweet/%s.json' % id
        json_data = self._FetchUrl(url, post_data={'dummy': None})
        data = json.loads(json_data)
        self._CheckForTwitterError(data)
        return Status.NewFromJsonDict(data)
github dhananjaysathe / python-twitter-with-proxy / twitter.py View on Github external
try:
         if int(count) > 100:
           raise TwitterError("'count' may not be greater than 100")
       except ValueError:
         raise TwitterError("'count' must be an integer")
     if count:
       parameters['count'] = count
     if since_id:
       parameters['since_id'] = since_id
     if include_entities:
       parameters['include_entities'] = True
     if max_id:
       try:
         parameters['max_id'] = long(max_id)
       except:
         raise TwitterError("max_id must be an integer")
     json = self._FetchUrl(url, parameters=parameters)
     data = self._ParseAndCheckTwitter(json)
     return [Status.NewFromJsonDict(x) for x in data]
github edolopez / Marketweet / twitter.py View on Github external
since_id:
        Returns results with an ID greater than (that is, more recent
        than) the specified ID. There are limits to the number of
        Tweets which can be accessed through the API. If the limit of
        Tweets has occured since the since_id, the since_id will be
        forced to the oldest ID available. [Optional]
      page:
        Specifies the page of results to retrieve.
        Note: there are pagination limits. [Optional]

    Returns:
      A sequence of twitter.DirectMessage instances
    '''
    url = '%s/direct_messages.json' % self.base_url
    if not self._oauth_consumer:
      raise TwitterError("The twitter.Api instance must be authenticated.")
    parameters = {}
    if since:
      parameters['since'] = since
    if since_id:
      parameters['since_id'] = since_id
    if page:
      parameters['page'] = page
    json = self._FetchUrl(url, parameters=parameters)
    data = self._ParseAndCheckTwitter(json)
    return [DirectMessage.NewFromJsonDict(x) for x in data]
github kzk / async-python-twitter / twitter.py View on Github external
in_reply_to_status_id:
        The ID of an existing status that the status to be posted is
        in reply to.  This implicitly sets the in_reply_to_user_id
        attribute of the resulting status to the user ID of the
        message being replied to.  Invalid/missing status IDs will be
        ignored. [Optional]
    Returns:
      A twitter.Status instance representing the message posted.
    '''
    if not self._username:
      raise TwitterError("The twitter.Api instance must be authenticated.")

    url = 'http://twitter.com/statuses/update.json'

    if len(status) > CHARACTER_LIMIT:
      raise TwitterError("Text must be less than or equal to %d characters. "
                         "Consider using PostUpdates." % CHARACTER_LIMIT)

    data = {'status': status}
    if in_reply_to_status_id:
      data['in_reply_to_status_id'] = in_reply_to_status_id
    json = self._FetchUrl(url, post_data=data)
    data = simplejson.loads(json)
    self._CheckForTwitterError(data)
    return Status.NewFromJsonDict(data)