本文整理汇总了Python中twython.Twython.html_for_tweet方法的典型用法代码示例。如果您正苦于以下问题:Python Twython.html_for_tweet方法的具体用法?Python Twython.html_for_tweet怎么用?Python Twython.html_for_tweet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twython.Twython
的用法示例。
在下文中一共展示了Twython.html_for_tweet方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: twython_html_for_twit
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import html_for_tweet [as 别名]
def twython_html_for_twit(value):
"""
Transforms the text of the twit into an html version.
Usage::
{{ twit|html_for_tweet }}
:param value: The twit
:return:
"""
return mark_safe(Twython.html_for_tweet(value))
示例2: action
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import html_for_tweet [as 别名]
def action(self, say=True):
tweets = set()
def already_said(id):
if id in tweets:
return True
tweets.add(id)
q = self.bot.session.query(Tweets).filter(Tweets.id == id)
return self.bot.session.query(q.exists()).scalar()
for user in self.users:
last_tweet = self.bot.session.query(LastTweets).filter(LastTweets.user == user).first()
try:
timeline = self.twitter.get_user_timeline(screen_name=user)
except TwythonError as err:
if self.err >= self.max_errors:
raise Pasteque("TWITTER IS DOWN OMG OMG OMG\n%s" % err)
self.err += 1
return
self.err = 0
for tweet in timeline:
if tweet['id'] <= last_tweet.last:
break
if say and not (self.avoid_rt and RT in tweet and already_said(tweet[RT]['id'])):
text = tweet['text']
if RT in tweet:
fmt = u'Tweet de %s retweeté par %s : '
initial = tweet[RT][u'user'][u'screen_name']
fmt_text = fmt % (initial, user)
fmt_html = fmt % (user_url(initial), user_url(user))
text = tweet[RT]['text']
elif REPLY_NAME in tweet and tweet[REPLY_NAME] is not None and tweet[REPLY_TWEET] is not None:
fmt = u'Tweet de %s en réponse à %s : '
url_text = '%s/%s/status/%s' % (URL, tweet[REPLY_NAME], tweet[REPLY_TWEET])
url_html = '<a href="%s">%s</a>' % (url_text, tweet[REPLY_NAME])
fmt_text = fmt % (user, url_text)
fmt_html = fmt % (user_url(user), url_html)
else:
fmt = u'Tweet de %s : '
fmt_text = fmt % user
fmt_html = fmt % user_url(user)
self.bot.say({'text': fmt_text + unescape(text),
'xhtml': fmt_html + Twython.html_for_tweet(tweet)})
tweets.add(tweet['id'])
if timeline:
last_tweet.last = timeline[0]['id']
for tweet in tweets:
self.bot.session.merge(Tweets(id=tweet))
self.bot.session.commit()
示例3: get1000links
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import html_for_tweet [as 别名]
def get1000links():
while True:
uri = ""
## tweet extract method with the last list item as the max_id
user_timeline = twitter.get_user_timeline(
screen_name="timoreilly", count=200, include_retweets=False, max_id=lis[-1]
)
for tweet in user_timeline:
lis.append(tweet["id"])
soup = BeautifulSoup(Twython.html_for_tweet(tweet))
uri = soup.find("a", "twython-url")
if uri:
uri = uri.get("href")
unique = testUniqueURL(uri)
if unique[0]:
unique_URIs.append(unique[1])
print 1000 - len(unique_URIs)
if len(unique_URIs) == 1000:
return
print "Sleep for 5 minutes"
time.sleep(300) ## 5 minute rest between api calls
示例4: Twython
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import html_for_tweet [as 别名]
url = "https://twitter.com/{}".format(User_Name)
#Twitter API Variables needed for authentication
APP_KEY = '8tLZ3KImFbikHPHYohJIDez1C'
APP_SECRET = '1cYBFd7pFPV161hDPLDS7xXCnFmCpPmlPXcxovMKFTly9YWieI'
OAUTH_TOKEN = '4861151506-7ZtKF6s8Ve2mO9bswUIGxNh5OHL4LqvTEkQ3USw'
OAUTH_TOKEN_SECRET = '4EbmpgVJ2C3tgB99wkkVMAL7eMwF9IfKtP2TWNorOeecy'
twitter = Twython(APP_KEY, APP_SECRET)
file = open("tweets.txt", "w+")
#Requests to API to get tweets from a specific user
print("Parsing '{}'...\n".format(url))
user_tweets = twitter.get_user_timeline(screen_name=User_Name,
count = 500, include_rts=False)
#Iteration through dictionary to write it to a file
for tweet in user_tweets:
tweet['text'] = Twython.html_for_tweet(tweet)
out = tweet['text']
outfile = str(remove_html_tags(out).encode('ascii', 'ignore'))
file.write(outfile[2:-1] + "\n")
#Cleanup
file.close()
end = time.time()
print ("Finished processing in {} seconds".format(str(end - start)))
示例5: get_tweet_list_by_html
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import html_for_tweet [as 别名]
def get_tweet_list_by_html(self, data):
tweet_list = []
for result in data['statuses']:
result['text'] = Twython.html_for_tweet(result, use_display_url=True, use_expanded_url=True)
tweet_list.append(result['text'])
return tweet_list
示例6: TwythonAPITestCase
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import html_for_tweet [as 别名]
#.........这里部分代码省略.........
@responses.activate
def test_request_should_handle_401(self):
"""Test that Twython raises an auth error on 401 error"""
endpoint = 'statuses/home_timeline'
url = self.get_url(endpoint)
self.register_response(responses.GET, url, body='{"errors":[{"message":"Error"}]}', status=401)
self.assertRaises(TwythonAuthError, self.api.request, endpoint)
@responses.activate
def test_request_should_handle_400_for_missing_auth_data(self):
"""Test that Twython raises an auth error on 400 error when no oauth data sent"""
endpoint = 'statuses/home_timeline'
url = self.get_url(endpoint)
self.register_response(responses.GET, url,
body='{"errors":[{"message":"Bad Authentication data"}]}', status=400)
self.assertRaises(TwythonAuthError, self.api.request, endpoint)
@responses.activate
def test_request_should_handle_400_that_is_not_auth_related(self):
"""Test that Twython raises a normal error on 400 error when unrelated to authorization"""
endpoint = 'statuses/home_timeline'
url = self.get_url(endpoint)
self.register_response(responses.GET, url,
body='{"errors":[{"message":"Bad request"}]}', status=400)
self.assertRaises(TwythonError, self.api.request, endpoint)
@responses.activate
def test_request_should_handle_rate_limit(self):
"""Test that Twython raises an rate limit error on 429"""
endpoint = 'statuses/home_timeline'
url = self.get_url(endpoint)
self.register_response(responses.GET, url,
body='{"errors":[{"message":"Rate Limit"}]}', status=429)
self.assertRaises(TwythonRateLimitError, self.api.request, endpoint)
@responses.activate
def test_get_lastfunction_header_should_return_header(self):
"""Test getting last specific header of the last API call works"""
endpoint = 'statuses/home_timeline'
url = self.get_url(endpoint)
self.register_response(responses.GET, url, adding_headers={'x-rate-limit-remaining': 37})
self.api.get(endpoint)
value = self.api.get_lastfunction_header('x-rate-limit-remaining')
self.assertEqual(37, value)
value2 = self.api.get_lastfunction_header('does-not-exist')
self.assertIsNone(value2)
value3 = self.api.get_lastfunction_header('not-there-either', 96)
self.assertEqual(96, value3)
def test_get_lastfunction_header_should_raise_error_when_no_previous_call(self):
"""Test attempting to get a header when no API call was made raises a TwythonError"""
self.assertRaises(TwythonError, self.api.get_lastfunction_header, 'no-api-call-was-made')
@responses.activate
def test_sends_correct_accept_encoding_header(self):
"""Test that Twython accepts compressed data."""
endpoint = 'statuses/home_timeline'
url = self.get_url(endpoint)
self.register_response(responses.GET, url)
self.api.get(endpoint)
self.assertEqual(b'gzip, deflate, compress', responses.calls[0].request.headers['Accept-Encoding'])
# Static methods
def test_construct_api_url(self):
"""Test constructing a Twitter API url works as we expect"""
url = 'https://api.twitter.com/1.1/search/tweets.json'
constructed_url = self.api.construct_api_url(url, q='#twitter')
self.assertEqual(constructed_url, 'https://api.twitter.com/1.1/search/tweets.json?q=%23twitter')
def test_encode(self):
"""Test encoding UTF-8 works"""
self.api.encode('Twython is awesome!')
def test_html_for_tweet(self):
"""Test HTML for Tweet returns what we want"""
tweet_text = self.api.html_for_tweet(test_tweet_object)
self.assertEqual(test_tweet_html, tweet_text)
def test_html_for_tweet_expanded_url(self):
"""Test using expanded url in HTML for Tweet displays full urls"""
tweet_text = self.api.html_for_tweet(test_tweet_object,
use_expanded_url=True)
# Make sure full url is in HTML
self.assertTrue('http://google.com' in tweet_text)
def test_html_for_tweet_short_url(self):
"""Test using expanded url in HTML for Tweet displays full urls"""
tweet_text = self.api.html_for_tweet(test_tweet_object, False)
# Make sure HTML doesn't contain the display OR expanded url
self.assertTrue('http://google.com' not in tweet_text)
self.assertTrue('google.com' not in tweet_text)
示例7: htmlify_tweet
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import html_for_tweet [as 别名]
def htmlify_tweet(json_data):
"""Passed the raw JSON data about a Tweet from Twitter's API, it returns
an HTMLified version of the Tweet's text. It:
* Replaces linebreaks with '<br>'s.
* Replaces @mentions with clickable @mentions.
* Replaces #hashtags with clickable #hashtags.
* Replaces $symbols with clickable $symbols.
* Replaces t.co URLs with clickable, full links.
"""
# Temporary, until Twython.html_for_tweet() can handle tweets with
# 'full_text' attributes.
if 'full_text' in json_data:
json_data['text'] = json_data['full_text']
# Some Tweets (eg from a downloaded archive) don't have entities['symbols']
# which Twython.html_for_tweet() currently expects.
# Not needed once github.com/ryanmcgrath/twython/pull/451 is in Twython.
if 'entities' in json_data and 'symbols' not in json_data['entities']:
json_data['entities']['symbols'] = []
# This does most of the work for us:
# https://twython.readthedocs.org/en/latest/usage/special_functions.html#html-for-tweet
html = Twython.html_for_tweet(
json_data, use_display_url=True, use_expanded_url=False)
# Need to do some tidying up:
try:
ents = json_data['entities']
except KeyError:
ents = {}
urls_count = len(ents['urls']) if 'urls' in ents else 0
media_count = len(ents['media']) if 'media' in ents else 0
hashtags_count = len(ents['hashtags']) if 'hashtags' in ents else 0
symbols_count = len(ents['symbols']) if 'symbols' in ents else 0
user_mentions_count = len(ents['user_mentions']) if 'user_mentions' in ents else 0
# Replace the classes Twython adds with rel="external".
html = html.replace('class="twython-hashtag"', 'rel="external"')
html = html.replace('class="twython-mention"', 'rel="external"')
html = html.replace('class="twython-media"', 'rel="external"')
html = html.replace('class="twython-symbol"', 'rel="external"')
# Twython uses the t.co URLs in the anchor tags.
# We want to replace those with the full original URLs.
# And replace the class it adds with rel="external".
if (urls_count + media_count) > 0:
if urls_count > 0:
for url in ents['urls']:
html = html.replace(
'<a href="%s" class="twython-url">' % url['url'],
'<a href="%s" rel="external">' % url['expanded_url']
)
if media_count > 0:
# Remove any media links, as we'll make the photos/movies visible in
# the page. All being well.
for item in ents['media']:
html = html.replace('<a href="%s" rel="external">%s</a>' % \
(item['url'], item['display_url']),
'')
if (urls_count + media_count + hashtags_count + symbols_count + user_mentions_count) == 0:
# Older Tweets might contain links but have no 'urls'/'media' entities.
# So just make their links into clickable links:
# But don't do this for newer Tweets which have an entities element,
# or we'll end up trying to make links from, say user_mentions we
# linked earlier.
html = urlize(html)
# Replace newlines with <br>s
html = re.sub(r'\n', '<br>', html.strip())
return html
示例8: TwythonAPITestCase
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import html_for_tweet [as 别名]
class TwythonAPITestCase(unittest.TestCase):
def setUp(self):
client_args = {
'headers': {
'User-Agent': '__twython__ Test'
},
'allow_redirects': False
}
oauth2_client_args = {
'headers': {} # This is so we can hit coverage that Twython sets User-Agent for us if none is supplied
}
self.api = Twython(app_key, app_secret,
oauth_token, oauth_token_secret,
client_args=client_args)
self.oauth2_api = Twython(app_key, access_token=access_token,
client_args=oauth2_client_args)
def test_construct_api_url(self):
"""Test constructing a Twitter API url works as we expect"""
url = 'https://api.twitter.com/1.1/search/tweets.json'
constructed_url = self.api.construct_api_url(url, q='#twitter')
self.assertEqual(constructed_url, 'https://api.twitter.com/1.1/search/tweets.json?q=%23twitter')
def test_get(self):
"""Test Twython generic GET request works"""
self.api.get('account/verify_credentials')
def test_post(self):
"""Test Twython generic POST request works, with a full url and
with just an endpoint"""
update_url = 'https://api.twitter.com/1.1/statuses/update.json'
status = self.api.post(update_url, params={'status': 'I love Twython!'})
self.api.post('statuses/destroy/%s' % status['id_str'])
def test_get_lastfunction_header(self):
"""Test getting last specific header of the last API call works"""
self.api.get('statuses/home_timeline')
self.api.get_lastfunction_header('x-rate-limit-remaining')
def test_get_lastfunction_header_not_present(self):
"""Test getting specific header that does not exist from the last call returns None"""
self.api.get('statuses/home_timeline')
header = self.api.get_lastfunction_header('does-not-exist')
self.assertEqual(header, None)
def test_get_lastfunction_header_no_last_api_call(self):
"""Test attempting to get a header when no API call was made raises a TwythonError"""
self.assertRaises(TwythonError, self.api.get_lastfunction_header,
'no-api-call-was-made')
def test_search_gen(self):
"""Test looping through the generator results works, at least once that is"""
search = self.api.search_gen('twitter', count=1)
counter = 0
while counter < 2:
counter += 1
result = next(search)
new_id_str = int(result['id_str'])
if counter == 1:
prev_id_str = new_id_str
time.sleep(1) # Give time for another tweet to come into search
if counter == 2:
self.assertTrue(new_id_str > prev_id_str)
def test_encode(self):
"""Test encoding UTF-8 works"""
self.api.encode('Twython is awesome!')
def test_html_for_tweet(self):
"""Test HTML for Tweet returns what we want"""
tweet_text = self.api.html_for_tweet(test_tweet_object)
self.assertEqual(test_tweet_html, tweet_text)
def test_html_for_tweet_expanded_url(self):
"""Test using expanded url in HTML for Tweet displays full urls"""
tweet_text = self.api.html_for_tweet(test_tweet_object,
use_expanded_url=True)
# Make sure full url is in HTML
self.assertTrue('http://google.com' in tweet_text)
def test_html_for_tweet_short_url(self):
"""Test using expanded url in HTML for Tweet displays full urls"""
tweet_text = self.api.html_for_tweet(test_tweet_object, False)
# Make sure HTML doesn't contain the display OR exapanded url
self.assertTrue(not 'http://google.com' in tweet_text)
self.assertTrue(not 'google.com' in tweet_text)
def test_raise_error_on_bad_ssl_cert(self):
"""Test TwythonError is raised by a RequestException when an actual HTTP happens"""
self.assertRaises(TwythonError, self.api.get, 'https://example.com')
# Timelines
def test_get_mentions_timeline(self):
"""Test returning mentions timeline for authenticated user succeeds"""
self.api.get_mentions_timeline()
#.........这里部分代码省略.........
示例9: tweet_to_html
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import html_for_tweet [as 别名]
def tweet_to_html(tweet):
return mark_safe(Twython.html_for_tweet(tweet))
示例10: TestHtmlForTweetTestCase
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import html_for_tweet [as 别名]
class TestHtmlForTweetTestCase(unittest.TestCase):
def setUp(self):
self.api = Twython('', '', '', '')
def load_tweet(self, name):
f = open(os.path.join(
os.path.dirname(__file__),
'tweets',
'%s.json' % name
))
tweet = json.load(f)
f.close()
return tweet
def test_basic(self):
"""Test HTML for Tweet returns what we want"""
tweet_object = self.load_tweet('basic')
tweet_text = self.api.html_for_tweet(tweet_object)
self.assertEqual(tweet_text,
'<a href="http://t.co/FCmXyI6VHd" class="twython-url">google.com</a> is a <a href="https://twitter.com/search?q=%23cool" class="twython-hashtag">#cool</a> site, lol! <a href="https://twitter.com/mikehelmick" class="twython-mention">@mikehelmick</a> shd <a href="https://twitter.com/search?q=%23checkitout" class="twython-hashtag">#checkitout</a>. Love, <a href="https://twitter.com/__twython__" class="twython-mention">@__twython__</a> <a href="https://t.co/67pwRvY6z9" class="twython-url">github.com</a> <a href="http://t.co/N6InAO4B71" class="twython-media">pic.twitter.com/N6InAO4B71</a>')
def test_reply(self):
"""Test HTML for Tweet links the replied-to username."""
tweet_object = self.load_tweet('reply')
tweet_text = self.api.html_for_tweet(tweet_object)
self.assertEqual(tweet_text,
u'<span class="twython-tweet-prefix"><a href="https://twitter.com/philgyford" class="twython-mention">@philgyford</a> </span>Here’s a test tweet that goes on as much as possible and includes an image. Hi to my fans in testland!<span class="twython-tweet-suffix"> https://t.co/tzhyk2QWSr</span>')
def test_expanded_url(self):
"""Test using expanded url in HTML for Tweet displays full urls"""
tweet_object = self.load_tweet('basic')
tweet_text = self.api.html_for_tweet(tweet_object,
use_expanded_url=True)
# Make sure full url is in HTML
self.assertTrue('http://google.com' in tweet_text)
def test_short_url(self):
"""Test using expanded url in HTML for Tweet displays full urls"""
tweet_object = self.load_tweet('basic')
tweet_text = self.api.html_for_tweet(tweet_object, False)
# Make sure HTML doesn't contain the display OR expanded url
self.assertTrue('http://google.com' not in tweet_text)
self.assertTrue('google.com' not in tweet_text)
def test_identical_urls(self):
"""If the 'url's for different url entities are identical, they should link correctly."""
tweet_object = self.load_tweet('identical_urls')
tweet_text = self.api.html_for_tweet(tweet_object)
self.assertEqual(tweet_text,
u'Use Cases, Trials and Making 5G a Reality <a href="https://t.co/W0uArTMk9N" class="twython-url">buff.ly/2sEhrgO</a> #5G #innovation via @5GWorldSeries <a href="https://t.co/W0uArTMk9N" class="twython-url">buff.ly/2sEhrgO</a>')
def test_symbols(self):
tweet_object = self.load_tweet('symbols')
tweet_text = self.api.html_for_tweet(tweet_object)
# Should only link symbols listed in entities:
self.assertTrue('<a href="https://twitter.com/search?q=%24AAPL" class="twython-symbol">$AAPL</a>' in tweet_text)
self.assertTrue('<a href="https://twitter.com/search?q=%24ANOTHER" class="twython-symbol">$ANOTHER</a>' not in tweet_text)
def test_no_symbols(self):
"""Should still work if tweet object has no symbols list"""
tweet = self.load_tweet('symbols')
# Save a copy:
symbols = tweet['entities']['symbols']
del tweet['entities']['symbols']
tweet_text = self.api.html_for_tweet(tweet)
self.assertTrue('symbols: $AAPL and' in tweet_text)
self.assertTrue('and $ANOTHER and $A.' in tweet_text)
def test_compatmode(self):
tweet_object = self.load_tweet('compat')
tweet_text = self.api.html_for_tweet(tweet_object)
# link to compat web status link
self.assertTrue(
u'<a href="https://t.co/SRmsuks2ru" class="twython-url">twitter.com/i/web/status/7…</a>' in tweet_text)
def test_extendedmode(self):
tweet_object = self.load_tweet('extended')
tweet_text = self.api.html_for_tweet(tweet_object)
# full tweet rendered with suffix
self.assertEqual(tweet_text,
'Say more about what\'s happening! Rolling out now: photos, videos, GIFs, polls, and Quote Tweets no longer count toward your 140 characters.<span class="twython-tweet-suffix"> <a href="https://t.co/I9pUC0NdZC" class="twython-media">pic.twitter.com/I9pUC0NdZC</a></span>')
def test_entities_with_prefix(self):
"""
If there is a username mention at the start of a tweet it's in the
"prefix" and so isn't part of the main tweet display text.
But its length is still counted in the indices of any subsequent
mentions, urls, hashtags, etc.
"""
self.maxDiff = 2200
tweet_object = self.load_tweet('entities_with_prefix')
tweet_text = self.api.html_for_tweet(tweet_object)
self.assertEqual(tweet_text,
u'<span class="twython-tweet-prefix"><a href="https://twitter.com/philgyford" class="twython-mention">@philgyford</a> </span>This is a test for <a href="https://twitter.com/visionphil" class="twython-mention">@visionphil</a> that includes a link <a href="https://t.co/sKw4J3A8SZ" class="twython-url">example.org</a> and <a href="https://twitter.com/search?q=%23hashtag" class="twython-hashtag">#hashtag</a> and X for good measure AND that is longer than 140 characters. <a href="https://t.co/jnQdy7Zg7u" class="twython-url">example.com</a>')
def test_media(self):
tweet_object = self.load_tweet('media')
tweet_text = self.api.html_for_tweet(tweet_object)
self.assertEqual(
#.........这里部分代码省略.........
示例11: Twython
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import html_for_tweet [as 别名]
from twython import Twython
APP_KEY = "*****************"
APP_SECRET = "*****************"
ACCESS_TOKEN = "*******************************"
ACCESS_TOKEN_SECRET = "********************************"
twitter = Twython(APP_KEY, APP_SECRET,
ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
user_tweets = twitter.get_user_timeline(screen_name='user_name',include_rts=True)
tweets = ""
for tweet in user_tweets:
tweet = Twython.html_for_tweet(tweet)
tweets += '<li>'+tweet+'</li>'
html = """
<link rel="stylesheet" href="https://abs.twimg.com/a/1387359134/t1/css/t1_core.bundle.css" type="text/css">
<meta charset="UTF-8">
<div class="tweet"><ul>"""+ tweets +"""</ul></div>
"""
create_html = open("fetchTweets.html","w")
create_html.write(html.encode('utf-8').strip())