當前位置: 首頁>>代碼示例>>Python>>正文


Python twython.TwythonError方法代碼示例

本文整理匯總了Python中twython.TwythonError方法的典型用法代碼示例。如果您正苦於以下問題:Python twython.TwythonError方法的具體用法?Python twython.TwythonError怎麽用?Python twython.TwythonError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在twython的用法示例。


在下文中一共展示了twython.TwythonError方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: post_tweet

# 需要導入模塊: import twython [as 別名]
# 或者: from twython import TwythonError [as 別名]
def post_tweet(message: str):
    """Post tweet message to account.

    Parameters
    ----------
    message: str
        Message to post on Twitter.
    """
    try:
        twitter = Twython(TwitterAuth.consumer_key,
                          TwitterAuth.consumer_secret,
                          TwitterAuth.access_token,
                          TwitterAuth.access_token_secret)
        twitter.update_status(status=message)
    except TwythonError as e:
        print(e) 
開發者ID:peterdalle,項目名稱:twitterbot,代碼行數:18,代碼來源:twitterbot.py

示例2: search_and_retweet

# 需要導入模塊: import twython [as 別名]
# 或者: from twython import TwythonError [as 別名]
def search_and_retweet(query: str, count=10):
    """Search for a query in tweets, and retweet those tweets.

    Parameters
    ----------
    query: str
        A query to search for on Twitter.
    count: int
        Number of tweets to search for. You should probably keep this low
        when you use search_and_retweet() on a schedule (e.g. cronjob).
    """
    try:
        twitter = Twython(TwitterAuth.consumer_key,
                          TwitterAuth.consumer_secret,
                          TwitterAuth.access_token,
                          TwitterAuth.access_token_secret)
        search_results = twitter.search(q=query, count=count)
    except TwythonError as e:
        print(e)
        return
    for tweet in search_results["statuses"]:
        # Make sure we don't retweet any dubplicates.
        if not is_in_logfile(
                    tweet["id_str"], Settings.posted_retweets_output_file):
            try:
                twitter.retweet(id=tweet["id_str"])
                write_to_logfile(
                    tweet["id_str"], Settings.posted_retweets_output_file)
                print("Retweeted {} (id {})".format(shorten_text(
                    tweet["text"], maxlength=40), tweet["id_str"]))
            except TwythonError as e:
                print(e)
        else:
            print("Already retweeted {} (id {})".format(
                shorten_text(tweet["text"], maxlength=40), tweet["id_str"])) 
開發者ID:peterdalle,項目名稱:twitterbot,代碼行數:37,代碼來源:twitterbot.py

示例3: tweet

# 需要導入模塊: import twython [as 別名]
# 或者: from twython import TwythonError [as 別名]
def tweet(self):
        """Send images to be rendered and tweet them with a text status."""
        square = False if len(self.matching_grafs) == 1 else True
        for graf in self.matching_grafs[:4]:
            self.imgs.append(render_img(graf, square=square))

        twitter = get_twitter_instance()

        media_ids = []

        for img in self.imgs:
            try:
                img_io = BytesIO()
                img.save(img_io, format='jpeg', quality=95)
                img_io.seek(0)
                res = twitter.upload_media(media=img_io)

                media_ids.append(res['media_id'])
            except TwythonError:
                pass

        source = self.outlet + ": " if self.outlet else ''

        # Truncate title to fit in remaining characters.
        # As of 2019-03-03:
        # - Tweets can be 280 characters.
        # - Minus the length of the `source` string.
        # - Minus three more characters for the ellipsis (a two-byte character) and space between the title and the URL.
        # - Links count for 23 characters once they've been wrapped in a t.co URL.
        remaining_chars = 280 - len(source) - 3 - 23
        title = (self.title[:remaining_chars] + '…') if len(self.title) > remaining_chars else self.title

        status = "{}{} {}".format(source, title, self.url)

        twitter.update_status(status=status, media_ids=media_ids)
        print(status)

        self.tweeted = True 
開發者ID:freedomofpress,項目名稱:trackthenews,代碼行數:40,代碼來源:core.py

示例4: tweet

# 需要導入模塊: import twython [as 別名]
# 或者: from twython import TwythonError [as 別名]
def tweet(twitter, message):
    print('Tweeting: "{}"'.format(message))
    try:
        twitter.update_status(status=message)
        print('Done')
    except TwythonError as e:
        print('Impossible to tweet, reason: {}'.format(e)) 
開發者ID:PacktPublishing,項目名稱:Python-for-Everyday-Life,代碼行數:9,代碼來源:tweet.py

示例5: search_tweets

# 需要導入模塊: import twython [as 別名]
# 或者: from twython import TwythonError [as 別名]
def search_tweets(twitter, query, how_many=10, kind='recent'):
    try:
        result_set = twitter.search(q=query, count=how_many, result_type=kind)
        return result_set['statuses']
    except TwythonError as e:
        print('Something bad happened, reason: {}'.format(e))
        return [] 
開發者ID:PacktPublishing,項目名稱:Python-for-Everyday-Life,代碼行數:9,代碼來源:tweet.py

示例6: retweet

# 需要導入模塊: import twython [as 別名]
# 或者: from twython import TwythonError [as 別名]
def retweet(twitter, tweet_id):
    print('Retweeting tweet with ID={}'.format(tweet_id))
    try:
        twitter.retweet(id=tweet_id)
        print('Done')
    except TwythonError as e:
        print('Impossible to retweet, reason: {}'.format(e)) 
開發者ID:PacktPublishing,項目名稱:Python-for-Everyday-Life,代碼行數:9,代碼來源:tweet.py

示例7: test_send

# 需要導入模塊: import twython [as 別名]
# 或者: from twython import TwythonError [as 別名]
def test_send(self):
        try:
            self.channel.send(self.randText)
        except TwythonError as e:
            # something out of our control
            raise SkipTest("Twython error occurred: {}".format(e))

        resp = self.client.get_user_timeline(screen_name=self.testParams['name'])
        if 'text' in resp[0]:
            self.assertEqual(resp[0]['text'], self.randText) 
開發者ID:DakotaNelson,項目名稱:sneaky-creeper,代碼行數:12,代碼來源:test_twitter.py

示例8: test_receive

# 需要導入模塊: import twython [as 別名]
# 或者: from twython import TwythonError [as 別名]
def test_receive(self):
        try:
            self.client.update_status(status=self.randText)
        except TwythonError as e:
            # something out of our control
            raise SkipTest("Twython error occurred: {}".format(e))

        tweets = self.channel.receive()
        self.assertEqual(tweets[0], self.randText) 
開發者ID:DakotaNelson,項目名稱:sneaky-creeper,代碼行數:11,代碼來源:test_twitter.py

示例9: unit_channel

# 需要導入模塊: import twython [as 別名]
# 或者: from twython import TwythonError [as 別名]
def unit_channel(channel, data):
    """ Test a channel. """

    t = sneakers.Exfil(channel, [])

    # get parameters from config folder
    configPath = os.path.join(basePath, 'config', '{}-config.json'.format(channel))

    try:
        with open(configPath) as f:
            params = json.loads(f.read())
    except:
        raise SkipTest('could not load configuration file for {}'.format(channel))

    t.set_channel_params({'sending': params[channel],
                          'receiving': params[channel]})

    try:
        t.send(data)
    except TwythonError as e:
        # something out of our control
        raise SkipTest("Twython error occurred: {}".format(e))

    got = t.receive()
    if len(data) > 300:
        assert_in(data, got,
          'Failed in assertion for the \'{}\' channel with a very large payload.'.format(channel))
    else:
        assert_in(data, got)

######################################################
#################### Actual Tests ####################
###################################################### 
開發者ID:DakotaNelson,項目名稱:sneaky-creeper,代碼行數:35,代碼來源:test_all.py


注:本文中的twython.TwythonError方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。