本文整理汇总了Python中tweepy.api.API类的典型用法代码示例。如果您正苦于以下问题:Python API类的具体用法?Python API怎么用?Python API使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了API类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: run_user_timeline_download
def run_user_timeline_download():
print('downloading user-timelines...')
api = API(auth, parser=JSONParser())
user_str_ids = []
with open('data/top_users_to_PrEP.txt') as f_in:
for line_no, line in enumerate(f_in):
if line_no == 1000:
break
user_str_ids.append(line)
users = []
pages = list(range(0, 150))
with open('data/user_timeline_tweets.json', 'w') as f_out:
for user_id in user_str_ids:
try:
time.sleep(60 * 16)
for page in pages:
for twt in api.user_timeline(user_id, count=20, page=page):
f_out.write(json.dumps(twt) + '\n')
users.append(user_id)
except:
pass
print('done with user-timelines...')
print(users)
print(len(user_str_ids))
开发者ID:quinngroup,项目名称:sm_w2v,代码行数:26,代码来源:__main__.py
示例2: _get_rate_limit_status
def _get_rate_limit_status(self, key, secret):
"""
Get rate limit status for specified access token key.
"""
auth = OAuthHandler(self.consumer_key, self.consumer_secret)
auth.set_access_token(key, secret)
api = API(auth)
return api.rate_limit_status()
开发者ID:svven,项目名称:tweepy,代码行数:8,代码来源:limit.py
示例3: get_username
def get_username(self):
if self.username is None:
api = API(self)
user = api.verify_credentials()
if user:
self.username = user.screen_name
else:
raise TweepError("Unable to get username, invalid oauth token!")
return self.username
开发者ID:archerhu,项目名称:tweepy,代码行数:9,代码来源:auth.py
示例4: tweet
def tweet(answer):
CONSUMER_KEY = config.get("auth", "CONSUMER_KEY")
CONSUMER_SECRET = config.get("auth", "CONSUMER_SECRET")
ACCESS_TOKEN = config.get("auth", "ACCESS_TOKEN")
ACCESS_TOKEN_SECRET = config.get("auth", "ACCESS_TOKEN_SECRET")
auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = API(auth)
result = api.update_status(status=answer)
开发者ID:ericoporto,项目名称:Chove-Agora,代码行数:10,代码来源:weathertwitter.py
示例5: get_user_id
def get_user_id(self):
if self.user_id is None:
api = API(self)
user = api.verify_credentials()
if user:
self.username = user.screen_name
self.user_id = user.id
else:
raise TweepError('Unable to get user_id,'
' invalid oauth token!')
return self.user_id
开发者ID:parantapa,项目名称:tweepy,代码行数:11,代码来源:auth.py
示例6: _testoauth
def _testoauth(self):
auth = OAuthHandler(oauth_consumer_key, oauth_consumer_secret)
# test getting access token
auth_url = auth.get_authorization_url()
print 'Please authorize: ' + auth_url
verifier = raw_input('PIN: ').strip()
self.assert_(len(verifier) > 0)
access_token = auth.get_access_token(verifier)
self.assert_(access_token is not None)
# build api object test using oauth
api = API(auth)
s = api.update_status('test %i' % random.randint(0, 1000))
api.destroy_status(s.id)
开发者ID:007Hamza,项目名称:BTP,代码行数:15,代码来源:test_api1.py
示例7: update_twitter_profile
def update_twitter_profile(user):
a = API()
try:
profile = user.get_profile()
twitter_user = a.get_user(user_id=profile.twitter_profile.twitter_id)
except:
twitter_user = None
if twitter_user:
profile.user.first_name = twitter_user.name.split(" ")[0]
profile.user.last_name = " ".join(twitter_user.name.split(" ")[1:])
profile.user.save()
profile.website = twitter_user.url
profile.profile_image_url = twitter_user.profile_image_url
profile.description = twitter_user.description
profile.twitter_name = twitter_user.screen_name
profile.location=twitter_user.location
profile.save()
开发者ID:fxdgear,项目名称:beersocial,代码行数:19,代码来源:tasks.py
示例8: scrapeThread
def scrapeThread(index):
auth = OAuthHandler(consumerKeys[index], consumerSecrets[index])
auth.set_access_token(accessTokens[index], accessSecrets[index])
api = API(auth)
try:
api.verify_credentials()
except TweepError:
print "Failed to authenticate - most likely reached rate limit/incorrect credentials!"
return
else:
print "You have successfully logged on as: " + api.me().screen_name
for i in range(0, numDays):
for query in queries[index]:
count = 0
cursor = Cursor(api.search,
q=quote(query.encode('utf-8')),
lang=langs[index],
since=sinces[i],
until=untils[i],
include_entities=True).items()
while True:
try:
tweet = cursor.next()
utc = datetime.now().strftime('%Y%m%dT%H%M%S%f')
outPath = path.join(outDir, sinces[i], langs[index], utc + '.json')
with open(outPath, 'w') as output:
output.write(dumps(tweet._json, ensure_ascii=False).encode('utf8'))
count += 1
if count == int(perDay / len(queries[index])):
break
except TweepError:
print langs[index] + " - rate limit reached! Pausing thread for 15 minutes."
sleep(60 * 15)
continue
except StopIteration:
break
print str(count) + " tweets stored in " + outPath
开发者ID:ramanpreetSinghKhinda,项目名称:CSE_535_Multilingual_Search_System,代码行数:39,代码来源:scrape.py
示例9: Tweet
class Tweet():
def __init__(self, auth):
self.auth = auth
self.api = API(auth)
def tweet_with_media(self, fn, status):
self.api.update_with_media(fn, status=status)
def update_with_media(self, fn, status, tweet_id):
# self.api.update_with_media(filename=fn, status=status, in_reply_to_status_id=tweet_id)
media = self.api.media_upload(fn)
self.api.update_status(status=status, reply_to_status_id=tweet_id, media_ids=[media.media_id])
def update(self, status, tweet_id):
self.api.update_status(status=status, reply_to_status_id=tweet_id)
开发者ID:aratakokubun,项目名称:RaspiBot,代码行数:16,代码来源:TwitterIO.py
示例10: update_tweet
def update_tweet(text):
auth = get_oauth()
api = API(auth)
api.update_status(status=text)
开发者ID:KyosukeHagiwara,项目名称:Komiyama,代码行数:4,代码来源:Komiyama.py
示例11: API
import sys,random,math,time
sys.path.append("../lib/")
from tweepy import api, error
from tweepy.cache import FileCache,DBCache, DBFileCache
from tweepy.api import API
import tweepy
from tweepy.models import *
from tweeapi import APISingleton
if __name__ == "__main__":
while 1:
try:
auth = tweepy.OAuthHandler("xg2hLKvf1nxw1TUALvx5xA", "MkX0lDUik0mJuc6nxserddbQDWd7ZTErQN6Tf0OhOM")
auth.set_access_token("174566652-MOGbxytlmUHIN5tEMgl5rgqWdWaIQXYZ6XPyYKl1", "yem38OfoUbsoPZvOVr3k0n3X7JSUDYD8oxAKXvrJw6k")
twitterApi = API(auth_handler=auth,
host='api.twitter.com', search_host='search.twitter.com',
cache=None, secure=False, api_root='/1', search_root='',
retry_count=0, retry_delay=0, retry_errors=None,
parser=None)
ret = twitterApi.rate_limit_status()
print ret
sys.exit(0)
except Exception,e:
print e
pass
开发者ID:bakhshandeh,项目名称:twitter,代码行数:27,代码来源:limit_count.py
示例12: None
{'$pull': {'termsList': {'handle': user['handle']}}})
mongo_config.update({'module': 'collecting-follow'},
{'$set': {'termsList': {'handle': user['handle'], 'id': user_id, 'collect': 0 }}})
# Loops thru current stored handles and adds list if both:
# A) Value isn't set to None (not valid OR no longer in use)
all_stored_handles = [user['handle'] for user in stored_terms]
stored_handles = [user['handle'] for user in stored_terms if user['id'] and user['collect']]
print 'MAIN: %d user ids for collection found in Mongo!' % len(stored_handles)
# Loop thru & query (except handles that have been stored)
print 'MAIN: Querying Twitter API for new handle:id pairs...'
logger.info('MAIN: Querying Twitter API for new handle:id pairs...')
# Initiates REST API connection
twitter_api = API(auth_handler=auth)
failed_handles = []
success_handles = []
# Loops thru user-given terms list
for handle in termsList:
# If handle already stored, no need to query for ID
if handle in stored_handles:
pass
# Queries the Twitter API for the ID value of the handle
else:
try:
user = twitter_api.get_user(screen_name=handle)
except TweepError as tweepy_exception:
error_message = tweepy_exception.args[0][0]['message']
code = tweepy_exception.args[0][0]['code']
# Rate limited for 15 minutes w/ code 88
开发者ID:CS-IASTATE-SocialMediaLab,项目名称:Syr-SM-Collection-Toolkit,代码行数:31,代码来源:ThreadedCollector.py
示例13: API
forward = []
backward = []
if __name__ == "__main__":
while 1:
try:
forward = []
backward = []
#outfile = sys.argv[1]
#auth = tweepy.BasicAuthHandler('reza_shz', 'mehdireza')
auth = tweepy.OAuthHandler("xg2hLKvf1nxw1TUALvx5xA", "MkX0lDUik0mJuc6nxserddbQDWd7ZTErQN6Tf0OhOM")
auth.set_access_token("174566652-MOGbxytlmUHIN5tEMgl5rgqWdWaIQXYZ6XPyYKl1", "yem38OfoUbsoPZvOVr3k0n3X7JSUDYD8oxAKXvrJw6k")
twitterApi = API(auth_handler=auth,
host='api.twitter.com', search_host='search.twitter.com',
cache=FileCache("cache", timeout = -1), secure=False, api_root='/1', search_root='',
retry_count=0, retry_delay=0, retry_errors=None,
parser=None)
#username1, username2,listUsernames = readFile(outfile)
user1 = twitterApi.get_user(sys.argv[1]) #@UndefinedVariable
user2 = twitterApi.get_user(sys.argv[2]) #@UndefinedVariable
forward.append({"obj":user1, "cursor":-1, "friends":[], "cursor_obj":-1, "path":[]})
backward.append({"obj":user2, "cursor":-1, "cursor_obj":-1,"path":[], "followers":[] })
reqs = 0
while 1:
fin, path = go_backward()
reqs +=1;print reqs
if fin: print path;reqs=-2;break
while has_node(backward):
开发者ID:bakhshandeh,项目名称:twitter,代码行数:29,代码来源:bidirectional_request_opt_final.py
示例14: API
if __name__ == "__main__":
while 1:
try:
auth = tweepy.OAuthHandler("xg2hLKvf1nxw1TUALvx5xA", "MkX0lDUik0mJuc6nxserddbQDWd7ZTErQN6Tf0OhOM")
auth.set_access_token(
"174566652-MOGbxytlmUHIN5tEMgl5rgqWdWaIQXYZ6XPyYKl1", "yem38OfoUbsoPZvOVr3k0n3X7JSUDYD8oxAKXvrJw6k"
)
twitterApi = API(
auth_handler=auth,
host="api.twitter.com",
search_host="search.twitter.com",
cache=DBFileCache(DBCache(timeout=-1), FileCache("cache", timeout=-1), timeout=-1),
secure=False,
api_root="/1",
search_root="",
retry_count=0,
retry_delay=0,
retry_errors=None,
parser=None,
)
i = random.randint(1, 1000000000)
u = handle_func(twitterApi.get_user, user_id=i)
tweets = u.timeline(count=100, include_rts=1)
friends = twitterApi.friends_ids(user_id=u.id)
followers = twitterApi.followers_ids(user_id=u.id)
except Exception, e:
print e
开发者ID:bakhshandeh,项目名称:twitter,代码行数:28,代码来源:randomRequest.py
示例15: get
def get(self):
verifier = self.request.get("oauth_verifier")
if verifier:
# Get access token
handler = auth.OAuthHandler(config.CONSUMER_KEY, config.CONSUMER_SECRET)
handler.set_request_token(self.session.get("request_token_key"), self.session.get("request_token_secret"))
access_token = handler.get_access_token(verifier)
if access_token:
# Get user
logging.info("Access token: %s" %(access_token))
user = User.all().filter("twitter_access_token_key", access_token.key).get()
if((not user) or (user and user.updated < datetime.now() - timedelta(0,86400))):
logging.info("Connecting to the Twitter API")
api = API(handler)
temp_user = api.verify_credentials()
temp_image = urlfetch.Fetch(str(temp_user.profile_image_url).replace("_normal", "")).content
# Transform image into .PNG
image_manager = images.Image(image_data=temp_image)
image_manager.rotate(360)
temp_png = image_manager.execute_transforms()
logging.info("Encoded into .PNG")
# Save or update image in Cloud storage
filename = config.FOLDER + "/" + str(temp_user.id)
gcs_file = gcs.open(filename,'w',content_type="image/png",options={"x-goog-acl":"public-read"})
gcs_file.write(temp_png)
gcs_file.close()
logging.info("Image saved to Google Cloud Storage")
# Get avatar
blob_filename = "/gs" + filename
blobkey = blobstore.create_gs_key(blob_filename)
temp_avatar = str(images.get_serving_url(blobkey))
if not user:
logging.info("User did not exist")
user = User(
twitter_id = str(temp_user.id),
twitter_access_token_key = str(access_token.key),
twitter_access_token_secret = str(access_token.secret),
username = str(temp_user.screen_name).lower(),
name = temp_user.name,
bio = temp_user.description,
avatar = temp_avatar,
)
else:
logging.info("User had to be updated")
user.twitter_id = str(temp_user.id)
user.twitter_access_token_key = str(access_token.key)
user.twitter_access_token_secret = str(access_token.secret)
user.username = str(temp_user.screen_name).lower()
user.name = temp_user.name
user.bio = temp_user.description
user.avatar = temp_avatar
user.put()
logging.info("User @%s saved in datastore"%(user.username))
# Save user in session
self.session["id"] = user.key().id()
else:
logging.error("No access token from Twitter")
print "Error"
else:
logging.error("No verifier")
print "Error"
# Redirect users to the page they came from or the page they're supposed to head to
next = self.session.get("next")
redirect = self.session.get("referer")
if next:
redirect = next
self.redirect(str(redirect))
开发者ID:detcherry,项目名称:gopitch,代码行数:81,代码来源:auth.py
示例16: update_reply
def update_reply(text, reply_id, screen_name):
auth = get_oauth()
api = API(auth)
st = "@" + str(screen_name) + " " + str(text)
api.update_status(status=st, in_reply_to_status_id=reply_id)
开发者ID:KyosukeHagiwara,项目名称:Komiyama,代码行数:5,代码来源:Komiyama.py
示例17: TweepyAPITests
class TweepyAPITests(unittest.TestCase):
def setUp(self):
auth = OAuthHandler(oauth_consumer_key, oauth_consumer_secret)
auth.set_access_token(oauth_token, oauth_token_secret)
self.api = API(auth)
self.api.retry_count = 2
self.api.retry_delay = 5
def testhometimeline(self):
self.api.home_timeline()
def testfriendstimeline(self):
self.api.friends_timeline()
def testusertimeline(self):
self.api.user_timeline()
self.api.user_timeline('twitter')
def testmentions(self):
self.api.mentions()
def testretweetedbyme(self):
self.api.retweeted_by_me()
def testretweetedbyuser(self):
self.api.retweeted_by_user('twitter')
def testretweetedtome(self):
self.api.retweeted_to_me()
def testretweetsofme(self):
self.api.retweets_of_me()
def testretweet(self):
s = self.api.retweet(123)
s.destroy()
def testretweets(self):
self.api.retweets(123)
def testgetstatus(self):
self.api.get_status(id=123)
def testupdateanddestroystatus(self):
# test update
text = 'testing %i' % random.randint(0, 1000)
update = self.api.update_status(status=text)
self.assertEqual(update.text, text)
# test destroy
deleted = self.api.destroy_status(id=update.id)
self.assertEqual(deleted.id, update.id)
def testgetuser(self):
u = self.api.get_user('twitter')
self.assertEqual(u.screen_name, 'twitter')
u = self.api.get_user(783214)
self.assertEqual(u.screen_name, 'twitter')
def testsearchusers(self):
self.api.search_users('twitter')
def testme(self):
me = self.api.me()
self.assertEqual(me.screen_name, username)
def testfriends(self):
self.api.friends()
def testfollowers(self):
self.api.followers()
def testdirectmessages(self):
self.api.direct_messages()
def testsentdirectmessages(self):
self.api.sent_direct_messages()
def testsendanddestroydirectmessage(self):
# send
sent_dm = self.api.send_direct_message(username, text='test message')
self.assertEqual(sent_dm.text, 'test message')
self.assertEqual(sent_dm.sender.screen_name, username)
self.assertEqual(sent_dm.recipient.screen_name, username)
# destroy
destroyed_dm = self.api.destroy_direct_message(sent_dm.id)
self.assertEqual(destroyed_dm.text, sent_dm.text)
self.assertEqual(destroyed_dm.id, sent_dm.id)
self.assertEqual(destroyed_dm.sender.screen_name, username)
self.assertEqual(destroyed_dm.recipient.screen_name, username)
def testcreatedestroyfriendship(self):
enemy = self.api.destroy_friendship('twitter')
self.assertEqual(enemy.screen_name, 'twitter')
self.assertFalse(self.api.exists_friendship(username, 'twitter'))
friend = self.api.create_friendship('twitter')
#.........这里部分代码省略.........
开发者ID:007Hamza,项目名称:BTP,代码行数:101,代码来源:test_api1.py
示例18: __init__
def __init__(self, auth):
self.auth = auth
self.api = API(auth)
开发者ID:aratakokubun,项目名称:RaspiBot,代码行数:3,代码来源:TwitterIO.py
示例19: setUp
def setUp(self):
auth = OAuthHandler(oauth_consumer_key, oauth_consumer_secret)
auth.set_access_token(oauth_token, oauth_token_secret)
self.api = API(auth)
self.api.retry_count = 2
self.api.retry_delay = 5
开发者ID:007Hamza,项目名称:BTP,代码行数:6,代码来源:test_api1.py
示例20: go
#.........这里部分代码省略.........
# Only call for new pairs
if collectSignal and (threading.activeCount() == 1):
# Names collection thread & adds to counter
myThreadCounter += 1
myThreadName = 'collector-' + collection_type + '%s' % myThreadCounter
termsList = collector['terms_list']
if termsList:
print 'Terms list length: ' + str(len(termsList))
# Grab IDs for follow stream
if collection_type == 'follow':
"""
TODO - Update Mongo terms w/ set for collect status 0 or 1
# Updates current stored handles to collect 0 if no longer listed in terms file
stored_terms = doc['termsList']
for user in stored_terms:
if user['handle'] not in termsList:
user_id = user['id']
mongo_config.update({'module': 'collector-follow'},
{'$pull': {'termsList': {'handle': user['handle']}}})
mongo_config.update({'module': 'collecting-follow'},
{'$set': {'termsList': {'handle': user['handle'], 'id': user_id, 'collect': 0 }}})
# Loops thru current stored handles and adds list if both:
# A) Value isn't set to None (not valid OR no longer in use)
all_stored_handles = [user['handle'] for user in stored_terms]
stored_handles = [user['handle'] for user in stored_terms if user['id'] and user['collect']]
print 'MAIN: %d user ids for collection found in Mongo!' % len(stored_handles)
"""
# Loop thru & query (except handles that have been stored)
print 'MAIN: Querying Twitter API for handle:id pairs...'
logger.info('MAIN: Querying Twitter API for handle:id pairs...')
# Initiates REST API connection
twitter_api = API(auth_handler=auth)
failed_handles = []
success_handles = []
# Loops thru user-given terms list
for item in termsList:
term = item['term']
# If term already has a valid ID, pass
if item['id'] is not None:
pass
# Queries the Twitter API for the ID value of the handle
else:
try:
user = twitter_api.get_user(screen_name=term)
except TweepError as tweepy_exception:
error_message = tweepy_exception.args[0][0]['message']
code = tweepy_exception.args[0][0]['code']
# Rate limited for 15 minutes w/ code 88
if code == 88:
print 'MAIN: User ID grab rate limited. Sleeping for 15 minutes.'
logger.exception('MAIN: User ID grab rate limited. Sleeping for 15 minutes.')
time.sleep(900)
# Handle doesn't exist, added to Mongo as None
elif code == 34:
print 'MAIN: User w/ handle %s does not exist.' % term
logger.exception('MAIN: User w/ handle %s does not exist.' % term)
item['collect'] = 0
item['id'] = None
failed_handles.append(term)
# Success - handle:ID pair stored in Mongo
else:
开发者ID:bitslabsyr,项目名称:stack,代码行数:67,代码来源:ThreadedCollector.py
注:本文中的tweepy.api.API类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论