本文整理汇总了Python中twython.Twython.get_favorites方法的典型用法代码示例。如果您正苦于以下问题:Python Twython.get_favorites方法的具体用法?Python Twython.get_favorites怎么用?Python Twython.get_favorites使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twython.Twython
的用法示例。
在下文中一共展示了Twython.get_favorites方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: clear_favourites
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_favorites [as 别名]
def clear_favourites():
t = Twython(consumer_key, consumer_secret, access_token_key, access_token_secret)
# Let's start at page 1 of your favourites because you know, it's a very good place to start
count = 1
# Now, let's start an infinite loop and I don't mean the one with Apple's HQ
while True:
# Get your favourites from Twitter
faves = t.get_favorites(page=count)
# If there's no favourites left from this page, do the Di Caprio and jump out
if len(faves) == 0:
break
# Programmers have been doing inception for ages before Nolan did. Let's go deeper and get
# into another loop now
for fav in faves:
t.destroy_favorite(id=fav['id'])
count += 1
# Now comes the all important part of the code. As my grandmother once told me, it is
# important to get good sleep, and we shall precisely do that for 15 minutes because you know
# Twitter mama hates rate-limit violators
sleep(900)
示例2: open
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_favorites [as 别名]
fo = open(user + ".txt", "wb")
fo.write("File of favorites from " + user + ":\n\n");
fo.close()
#file of tweet links for stuff
fl = open(user+ "_links" + ".txt", "wb")
fl.write("File of links from " + user + ":\n\n");
fl.close()
howlong = 0
for i in range(0, endrange): ## iterate through all tweets at 200 per round
## tweet extract method with the last list item as the max_id
if starting_tweet_id == 0:
try:
user_faves = twitter.get_favorites(screen_name=user, count=count)
except:
print "You are not authorized to view this user, they probably have a private profile. The jerks..."
break
else:
try:
if howlong == endrange:
user_faves = twitter.get_favorites(screen_name=user, count=count, max_id = starting_tweet_id)
else:
user_faves = twitter.get_favorites(screen_name=user, count=lastround, max_id = starting_tweet_id)
except:
print "You are not authorized to view this user, they probably have a private profile. The jerks..."
break
## pretty printing used for troublshooting and finding further parts of a tweet to use
#pprint.pprint(user_faves)
示例3: TwythonAPITestCase
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_favorites [as 别名]
#.........这里部分代码省略.........
'''Test returning list of accounts the specified user can
contribute to succeeds'''
self.api.get_contributees(screen_name='TechCrunch')
def test_get_contributors(self):
'''Test returning list of accounts that contribute to the
authenticated user fails because we are not a Contributor account'''
self.assertRaises(TwythonError, self.api.get_contributors,
screen_name=screen_name)
def test_remove_profile_banner(self):
'''Test removing profile banner succeeds'''
self.api.remove_profile_banner()
def test_get_profile_banner_sizes(self):
'''Test getting list of profile banner sizes fails because
we have not uploaded a profile banner'''
self.assertRaises(TwythonError, self.api.get_profile_banner_sizes)
# Suggested Users
def test_get_user_suggestions_by_slug(self):
'''Test getting user suggestions by slug succeeds'''
self.api.get_user_suggestions_by_slug(slug='twitter')
def test_get_user_suggestions(self):
'''Test getting user suggestions succeeds'''
self.api.get_user_suggestions()
def test_get_user_suggestions_statuses_by_slug(self):
'''Test getting status of suggested users succeeds'''
self.api.get_user_suggestions_statuses_by_slug(slug='funny')
# Favorites
def test_get_favorites(self):
'''Test getting list of favorites for the authenticated
user succeeds'''
self.api.get_favorites()
def test_create_and_destroy_favorite(self):
'''Test creating and destroying a favorite on a tweet succeeds'''
self.api.create_favorite(id=test_tweet_id)
self.api.destroy_favorite(id=test_tweet_id)
# Lists
def test_show_lists(self):
'''Test show lists for specified user'''
self.api.show_lists(screen_name='twitter')
def test_get_list_statuses(self):
'''Test timeline of tweets authored by members of the
specified list succeeds'''
self.api.get_list_statuses(list_id=test_list_id)
def test_create_update_destroy_list_add_remove_list_members(self):
'''Test create a list, adding and removing members then
deleting the list succeeds'''
the_list = self.api.create_list(name='Stuff')
list_id = the_list['id_str']
self.api.update_list(list_id=list_id, name='Stuff Renamed')
# Multi add/delete members
self.api.create_list_members(list_id=list_id,
screen_name='johncena,xbox')
self.api.delete_list_members(list_id=list_id,
screen_name='johncena,xbox')
示例4: ServiceTwitter
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_favorites [as 别名]
class ServiceTwitter(ServicesMgr):
"""
Service Twitter
"""
def __init__(self, token=None, **kwargs):
"""
:param token:
:param kwargs:
"""
super(ServiceTwitter, self).__init__(token, **kwargs)
self.consumer_key = settings.TH_TWITTER_KEY['consumer_key']
self.consumer_secret = settings.TH_TWITTER_KEY['consumer_secret']
self.token = token
self.oauth = 'oauth1'
self.service = 'ServiceTwitter'
if self.token is not None:
token_key, token_secret = self.token.split('#TH#')
try:
self.twitter_api = Twython(self.consumer_key,
self.consumer_secret,
token_key, token_secret)
except (TwythonAuthError, TwythonRateLimitError) as e:
us = UserService.objects.get(token=token)
logger.error(e.msg, e.error_code)
update_result(us.trigger_id, msg=e.msg, status=False)
def read_data(self, **kwargs):
"""
get the data from the service
:param kwargs: contain keyword args : trigger_id at least
:type kwargs: dict
:rtype: list
"""
twitter_status_url = 'https://www.twitter.com/{}/status/{}'
twitter_fav_url = 'https://www.twitter.com/{}/status/{}'
now = arrow.utcnow().to(settings.TIME_ZONE)
my_tweets = []
search = {}
since_id = None
trigger_id = kwargs['trigger_id']
date_triggered = arrow.get(kwargs['date_triggered'])
def _get_tweets(twitter_obj, search):
"""
get the tweets from twitter and return the filters to use :
search and count
:param twitter_obj: from Twitter model
:param search: filter used for twython.search() or
twython.get_user_timeline())
:type twitter_obj: Object
:type search: dict
:return: count that limit the quantity of tweet to retrieve,
the filter named search, the tweets
:rtype: list
"""
"""
explanations about statuses :
when we want to track the tweet of a screen 'statuses' contain all of them
when we want to track all the tweet matching a tag 'statuses' contain statuses + metadata array
this is why we need to do statuses = statuses['statuses']
to be able to handle the result as for screen_name
"""
# get the tweets for a given tag
# https://dev.twitter.com/docs/api/1.1/get/search/tweets
statuses = ''
count = 100
if twitter_obj.tag:
count = 100
search['count'] = count
search['q'] = twitter_obj.tag
search['result_type'] = 'recent'
# do a search
statuses = self.twitter_api.search(**search)
# just return the content of te statuses array
statuses = statuses['statuses']
# get the tweets from a given user
# https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
elif twitter_obj.screen:
count = 200
search['count'] = count
search['screen_name'] = twitter_obj.screen
# call the user timeline and get his tweet
try:
if twitter_obj.fav:
count = 20
search['count'] = 20
# get the favorites https://dev.twitter.com/rest/
# reference/get/favorites/list
statuses = self.twitter_api.get_favorites(**search)
else:
statuses = self.twitter_api.get_user_timeline(**search)
except TwythonAuthError as e:
logger.error(e.msg, e.error_code)
#.........这里部分代码省略.........
示例5: TwythonAPITestCase
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_favorites [as 别名]
#.........这里部分代码省略.........
"""Test returning list of accounts the specified user can
contribute to succeeds"""
self.api.get_contributees(screen_name='TechCrunch')
def test_get_contributors(self):
"""Test returning list of accounts that contribute to the
authenticated user fails because we are not a Contributor account"""
self.assertRaises(TwythonError, self.api.get_contributors,
screen_name=screen_name)
def test_remove_profile_banner(self):
"""Test removing profile banner succeeds"""
self.api.remove_profile_banner()
def test_get_profile_banner_sizes(self):
"""Test getting list of profile banner sizes fails because
we have not uploaded a profile banner"""
self.assertRaises(TwythonError, self.api.get_profile_banner_sizes)
# Suggested Users
def test_get_user_suggestions_by_slug(self):
"""Test getting user suggestions by slug succeeds"""
self.api.get_user_suggestions_by_slug(slug='twitter')
def test_get_user_suggestions(self):
"""Test getting user suggestions succeeds"""
self.api.get_user_suggestions()
def test_get_user_suggestions_statuses_by_slug(self):
"""Test getting status of suggested users succeeds"""
self.api.get_user_suggestions_statuses_by_slug(slug='funny')
# Favorites
def test_get_favorites(self):
"""Test getting list of favorites for the authenticated
user succeeds"""
self.api.get_favorites()
def test_create_and_destroy_favorite(self):
"""Test creating and destroying a favorite on a tweet succeeds"""
self.api.create_favorite(id=test_tweet_id)
self.api.destroy_favorite(id=test_tweet_id)
# Lists
def test_show_lists(self):
"""Test show lists for specified user"""
self.api.show_lists(screen_name='twitter')
def test_get_list_statuses(self):
"""Test timeline of tweets authored by members of the
specified list succeeds"""
self.api.get_list_statuses(list_id=test_list_id)
def test_create_update_destroy_list_add_remove_list_members(self):
"""Test create a list, adding and removing members then
deleting the list succeeds"""
the_list = self.api.create_list(name='Stuff')
list_id = the_list['id_str']
self.api.update_list(list_id=list_id, name='Stuff Renamed')
screen_names = ['johncena', 'xbox']
# Multi add/delete members
self.api.create_list_members(list_id=list_id,
screen_name=screen_names)
self.api.delete_list_members(list_id=list_id,
示例6: TwythonEndpointsTestCase
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_favorites [as 别名]
#.........这里部分代码省略.........
def test_list_mute_ids(self):
"""Test listing user ids who are muted by the authenticated user
succeeds"""
self.api.list_mute_ids()
@unittest.skip('skipping non-updated test')
def test_create_mute(self):
"""Test muting a user succeeds"""
self.api.create_mute(screen_name='justinbieber')
@unittest.skip('skipping non-updated test')
def test_destroy_mute(self):
"""Test muting a user succeeds"""
self.api.destroy_mute(screen_name='justinbieber')
# Suggested Users
@unittest.skip('skipping non-updated test')
def test_get_user_suggestions_by_slug(self):
"""Test getting user suggestions by slug succeeds"""
self.api.get_user_suggestions_by_slug(slug='twitter')
@unittest.skip('skipping non-updated test')
def test_get_user_suggestions(self):
"""Test getting user suggestions succeeds"""
self.api.get_user_suggestions()
@unittest.skip('skipping non-updated test')
def test_get_user_suggestions_statuses_by_slug(self):
"""Test getting status of suggested users succeeds"""
self.api.get_user_suggestions_statuses_by_slug(slug='funny')
# Favorites
@unittest.skip('skipping non-updated test')
def test_get_favorites(self):
"""Test getting list of favorites for the authenticated
user succeeds"""
self.api.get_favorites()
@unittest.skip('skipping non-updated test')
def test_create_and_destroy_favorite(self):
"""Test creating and destroying a favorite on a tweet succeeds"""
self.api.create_favorite(id=test_tweet_id)
self.api.destroy_favorite(id=test_tweet_id)
# Lists
@unittest.skip('skipping non-updated test')
def test_show_lists(self):
"""Test show lists for specified user"""
self.api.show_lists(screen_name='twitter')
@unittest.skip('skipping non-updated test')
def test_get_list_statuses(self):
"""Test timeline of tweets authored by members of the
specified list succeeds"""
self.api.get_list_statuses(slug=test_list_slug,
owner_screen_name=test_list_owner_screen_name)
@unittest.skip('skipping non-updated test')
def test_create_update_destroy_list_add_remove_list_members(self):
"""Test create a list, adding and removing members then
deleting the list succeeds"""
the_list = self.api.create_list(name='Stuff %s' % int(time.time()))
list_id = the_list['id_str']
self.api.update_list(list_id=list_id, name='Stuff Renamed \
%s' % int(time.time()))
示例7: raw_input
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_favorites [as 别名]
# Post
post = raw_input("Write your tweet: ")
twitter.update_status(status=post)
# Get timelines
print twitter.get_user_timeline(screen_name = "name")
print twitter.get_home_timeline(count = 5)
# Search
print twitter.search(q="linux", result_type="popular")
# Follow
twitter.create_friendship(screen_name = "LinuxUserMag")
# Retweet
twitter.retweet(id = "12345")
# Favouriting
twitter.create_favorite(id = "12345")
print twitter.get_favorites()
# Mentions
print twitter.get_mentions_timeline(count="5")
# Trending
print twitter.get_place_trends(id="1")
# Retrieve lists
print twitter.get_list_statuses(id = "12345")
示例8: Twython
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_favorites [as 别名]
import sys
from twython import Twython
# Twitter Login
creds = json.load(open('creds.json'))
twitter = Twython(creds['APP_KEY'], creds['APP_SECRET'], oauth_version=2)
ACCESS_TOKEN = twitter.obtain_access_token()
twitter = Twython(creds['APP_KEY'], access_token=ACCESS_TOKEN)
# Lets grabe favs from this year
user = 'lhl'
keep_going = 1
min = 0
while keep_going:
# https://dev.twitter.com/rest/reference/get/favorites/list
if min:
favs = twitter.get_favorites(screen_name=user, count=200, max_id=min)
else:
favs = twitter.get_favorites(screen_name=user, count=200)
# Write out file
max = favs[0]['id']
min = favs[-1]['id']
with open('favs.%s.%s.json' % (max, min), 'w') as outfile:
json.dump(favs, outfile)
# Stop after 2014
if favs[-1]['created_at'][-4:] != '2014':
keep_going = 0
示例9: favourites_xls
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_favorites [as 别名]
def favourites_xls(opt):
t = Twython(consumer_key, consumer_secret, access_token_key, access_token_secret)
# Let's create an empty xls Workbook and define formatting
wb = Workbook()
ws = wb.add_sheet('0')
# Column widths
ws.col(0).width = 256 * 30
ws.col(1).width = 256 * 30
ws.col(2).width = 256 * 60
ws.col(3).width = 256 * 30
ws.col(4).width = 256 * 30
# Stylez
style_link = easyxf('font: underline single, name Arial, height 280, colour_index blue')
style_heading = easyxf('font: bold 1, name Arial, height 280; pattern: pattern solid, pattern_fore_colour yellow, pattern_back_colour yellow')
style_wrap = easyxf('align: wrap 1; font: height 280')
# Headings in proper MBA spreadsheet style - Bold with yellow background
ws.write(0,0,'Author',style_heading)
ws.write(0,1,'Twitter Handle',style_heading)
ws.write(0,2,'Text',style_heading)
ws.write(0,3,'Embedded Links',style_heading)
# Let's start at page 1 of your favourites because you know, it's a very good place to start
count = 1
pagenum = 1
# Now, let's start an infinite loop and I don't mean the one with Apple's HQ
while True:
# Get your favourites from Twitter
faves = t.get_favorites(page=pagenum)
# If there's no favourites left from this page OR we've reached the page number specified by our user, do the Di Caprio and jump out
if len(faves) == 0 or (opt != 'all' and pagenum > int(opt)):
break
# Programmers have been doing inception for ages before Nolan did. Let's go deeper and get
# into another loop now
for fav in faves:
ws.write(count, 0, fav['user']['name'],style_wrap)
ws.write(count, 1, fav['user']['screen_name'],style_wrap)
ws.write(count, 2, fav['text'],style_wrap)
links = fav['entities']['urls']
i = 0
for link in links:
formatted_link = 'HYPERLINK("%s";"%s")' % (link['url'],"link")
ws.write(count, 3+i, Formula(formatted_link), style_link)
i += 1
count += 1
pagenum += 1
# Now comes the all important part of the code. As my grandmother once told me, it is
# important to get good sleep, and we shall precisely do that for 15 minutes because you know
# Twitter mama hates rate-limit violators. We will use 12 requests for every 15 minutes just to be
# on the safe side
if (pagenum % 12 == 0):
time.sleep(900)
# Now for the step that has caused untold misery and suffering to people who forget to do it at work
wb.save('twitter_favourites.xls')
示例10: Tweets
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_favorites [as 别名]
class Tweets():
"""
Manages Tweet class instances.
"""
def __init__(self):
# arrays of tweets classes
self.tweets = []
# Boolean that tracks if the calls
# to the server were successful.
self.successful_connection = False
# arrays of tids represented
# in this object
self.tids = []
# twitter connection
self.api = Twython(
settings.NEWS_TWITTER_CONSUMER_KEY,
settings.NEWS_TWITTER_CONSUMER_SECRET,
settings.NEWS_TWITTER_ACCESS_TOKEN,
settings.NEWS_TWITTER_ACCESS_TOKEN_SECRET)
# raw twitter response
self.raw = self.get_tweets()
# add all posts to this manager
for tweet in self.raw:
self.add(Tweet(tweet))
def get_tweets(self):
"""
will get all of the toots to deal with
"""
tweets = []
timeline, favorites = None, None
try:
timeline = self.api.get_user_timeline(
screen_name=settings.NEWS_TWITTER_ACCOUNT,
count=200,
include_rts=True,
exclude_replies=True)
# sleep for 10 seconds
# between calls.
time.sleep(10)
favorites = self.api.get_favorites(
screen_name=settings.NEWS_TWITTER_ACCOUNT,
count=200)
if len(timeline) > 0 and len(favorites) > 0:
tweets = timeline + favorites
self.successful_connection = True
else:
self.successful_connection = False
except:
self.successful_connection = False
logging.info("Problems getting tweets from Twitter API.")
logging.info("Favorites:")
logging.info(favorites)
logging.info("Timeline:")
logging.info(timeline)
return tweets
def add(self, tweet):
"""
add a Tweet instance to be tracked
for updates, to to be created
"""
self.tids.append(tweet.tid)
self.tweets.append(tweet)
return self
def compare(self, steam_model):
"""
steam_model is a Tweet model instance
if update needs to occur, do so
from the steam_model instance
return self
"""
tweet = None
# set `tweet` to the Tweet class
# of data that twitter sent us
# to compare against steam_model
for t in self.tweets:
if steam_model.tid == t.tid:
tweet = t
break
# compare the twitter data to steam data
if tweet:
# we now have evidence that this
# thing exists in some form in the
#.........这里部分代码省略.........