本文整理汇总了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)
示例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"]))
示例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
示例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))
示例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 []
示例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))
示例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)
示例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)
示例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 ####################
######################################################