本文整理汇总了Python中tweepy.API.trends_place方法的典型用法代码示例。如果您正苦于以下问题:Python API.trends_place方法的具体用法?Python API.trends_place怎么用?Python API.trends_place使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tweepy.API
的用法示例。
在下文中一共展示了API.trends_place方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_location_trends
# 需要导入模块: from tweepy import API [as 别名]
# 或者: from tweepy.API import trends_place [as 别名]
def get_location_trends(locations, auth):
api = API(auth)
trends = api.trends_place(locations)
data = trends[0]
trends_data = data['trends']
names = [trend['name'] for trend in trends_data]
print names
示例2: __init__
# 需要导入模块: from tweepy import API [as 别名]
# 或者: from tweepy.API import trends_place [as 别名]
def __init__(self):
auth = OAuthHandler(app.config['CONSUMER_KEY'], app.config['CONSUMER_SECRET'])
auth.set_access_token(app.config['ACCESS_TOKEN'], app.config['ACCESS_TOKEN_SECRET'])
api = API(auth,wait_on_rate_limit=True)
self.db = 'tweets.sqlite'
self.tweets = []
#Get worldwide trend topics
k = api.trends_place(1)
trends = [i['name'] for i in k[0]['trends']]
#random_numbers = sample(xrange(len(trends)-1),10) # create 10 random numbers for random trends search
""" This is the my home_timeline and I'll categorize from my twitter account to more true result """
for tweet in api.home_timeline(count=200, include_rts=0):
try:
url = tweet.entities['urls'][0]['expanded_url']
except IndexError:
url = False
if (url is not False):
self.tweets.append((
tweet.id_str,
self.extract_urls(tweet.text),
url,
str(tweet.created_at).replace(' ', 'T'),
tweet.retweet_count,
tweet.user.screen_name,
tweet.user.profile_image_url.replace("_normal",""),
tweet.user.followers_count
))
for i in range(30):
for tweet in Cursor(api.search,q=trends[i],result_type="popular").items(19):
try:
url = tweet.entities['urls'][0]['expanded_url']
except:
url = False
if (url is not False):
self.tweets.append((
tweet.id_str,
self.extract_urls(tweet.text),
url,
str(tweet.created_at).replace(' ', 'T'),
tweet.retweet_count,
tweet.user.screen_name,
tweet.user.profile_image_url.replace("_normal",""),
tweet.user.followers_count
))
示例3: Twitter
# 需要导入模块: from tweepy import API [as 别名]
# 或者: from tweepy.API import trends_place [as 别名]
#.........这里部分代码省略.........
"""Returns the requested user."""
try:
user = self.api.get_user(user, include_entities=True)
except TweepError as error:
raise TwitterError("Failed to get user: %s" % error)
return user
def get_followed(self):
"""Returns an array of screen_names that you follow."""
try:
followed = []
for user in Cursor(self.api.friends).items(200):
followed.append(user.screen_name)
except TweepError as error:
raise TwitterError("Faled to get followed: %s" % error)
return followed
def get_trend_places(self):
"""
Returns a dict of dicts, first by country then by place.
Every country has a special 'woeid' key that holds the woeid of the
country itself and all the places of the country pointing to their
respective woeids.
A special exception is the 'Worldwide' "country" which only has a
woeid entry.
"""
try:
trend_places = self.api.trends_available()
except TweepError as error:
raise TwitterError("Falied to get available places: %s." % error)
places = defaultdict(dict)
for place in trend_places:
if not place['country']:
places['Worldwide'] = {'woeid': place['woeid']}
else:
if place['country'] == place['name']:
places[place['country']]['woeid'] = place['woeid']
else:
places[place['country']][place['name']] = place['woeid']
return places
def get_trends(self, woeid):
"""
Gets the current trends for the location represented by woeid.
Returns a list of trends with element 0 representing the location name.
"""
try:
trend_response = self.api.trends_place(woeid)
except TweepError as error:
raise TwitterError("Failed to get trends: %s" % error)
trends = []
trends.append(trend_response[0]['locations'][0]['name'])
for trend in trend_response[0]['trends']:
trends.append(trend['name'])
return trends
def get_favorites(self):
"""Get your favourite tweets."""
try:
favorites = self.api.favorites(include_entities=True)
except TweepError as error:
raise TwitterError("Failed to get favourites: %s" % error)
return favorites
def status_count(self, message):
"""
Replaces the URLs and returns the length how twitter would count it.
"""
message = self.__replace_urls(message)
return(len(message))
def __is_sane(self, message):
"""Does sanity checks to see if the status is valid."""
if self.status_count(message) > 140:
return False
return True
def __replace_urls(self, message):
"""Replace URLs with placeholders, 20 for http URLs, 21 for https."""
# regexes to match URLs
octet = r'(?:2(?:[0-4]\d|5[0-5])|1\d\d|\d{1,2})'
ip_addr = r'%s(?:\.%s){3}' % (octet, octet)
# Base domain regex off RFC 1034 and 1738
label = r'[0-9a-z][-0-9a-z]*[0-9a-z]?'
domain = r'%s(?:\.%s)*\.[a-z][-0-9a-z]*[a-z]?' % (label, label)
url_re = re.compile(r'(\w+://(?:%s|%s)(?::\d+)?(?:/[^\])>\s]*)?)' %
(domain, ip_addr), re.I)
new_message = message
for url in url_re.findall(message):
short_url = 'x' * 20
if url.startswith('https'):
short_url = 'x' * 21
new_message = new_message.replace(url, short_url)
return new_message