本文整理汇总了Python中twython.Twython.request方法的典型用法代码示例。如果您正苦于以下问题:Python Twython.request方法的具体用法?Python Twython.request怎么用?Python Twython.request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twython.Twython
的用法示例。
在下文中一共展示了Twython.request方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_latest_tweet
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import request [as 别名]
def get_latest_tweet(app):
"""
Get the latest tweet in HTML format adding the user profile image.
:param Flask app:
:rtype: unicode
"""
twitter = Twython(
app.config.get('TWITTER_CONSUMER_KEY'),
app.config.get('TWITTER_CONSUMER_SECRET'),
app.config.get('TWITTER_ACCESS_TOKEN'),
app.config.get('TWITTER_ACCESS_TOKEN_SECRET')
)
# Latest tweet
try:
user_timeline = twitter.get_user_timeline()
latest_id = user_timeline[0]['id']
tweet_response = twitter.request(
'statuses/show', params={
'id': latest_id
}
)
oembed_response = twitter.request(
'statuses/oembed', params={
'id': latest_id,
'hide_media': False,
'hide_thread': True,
'omit_script': True
}
)
except TwythonRateLimitError:
return u''
return render_template(
'latest_tweet.html',
tweet=tweet_response,
oembed=oembed_response
)
示例2: Twitter
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import request [as 别名]
class Twitter():
def __init__(self, oauth_token=None, oauth_token_secret=None, callback_url=None):
self.twython = Twython(app_key=settings.CONSUMER_KEY,
app_secret=settings.CONSUMER_SECRET, oauth_token=oauth_token,
oauth_token_secret=oauth_token_secret, callback_url=callback_url)
def get_authentication_tokens(self):
return self.twython.get_authentication_tokens()
def get_authorized_tokens(self, oauth_verifier):
return self.twython.get_authorized_tokens(oauth_verifier)
def get_home_timeline(self):
tweets = self.twython.request('statuses/home_timeline', params={'include_entities': '1'})
logger.debug('tweets: ' + str(tweets))
processed_tweets = [Tweet(tweet) for tweet in tweets]
return processed_tweets
示例3: __init__
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import request [as 别名]
class Twitter:
def __init__(self, oauth_token=None, oauth_token_secret=None, callback_url=None):
self.twython = Twython(
twitter_token=settings.CONSUMER_KEY,
twitter_secret=settings.CONSUMER_SECRET,
oauth_token=oauth_token,
oauth_token_secret=oauth_token_secret,
callback_url=callback_url,
)
def get_authentication_tokens(self):
return self.twython.get_authentication_tokens()
def get_authorized_tokens(self):
return self.twython.get_authorized_tokens()
def get_home_timeline(self):
return self.twython.request("statuses/home_timeline", params={"include_entities": "1"})
示例4: get_twav
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import request [as 别名]
def get_twav(account_name):
""" get twitter icon """
if account_name is None:
return None
if account_name.startswith('@'):
account_name = account_name.lstrip('@')
api = 'https://api.twitter.com/1.1/users/show.json'
args = {'screen_name': account_name}
t = Twython(app_key=KEY,
app_secret=SECRET,
oauth_token=OTOKEN,
oauth_token_secret=OSECRET)
try:
resp = t.request(api, params=args)
image = resp['profile_image_url']
first, last = image.split('_normal') # to get the regular size
except (TwythonError, ValueError):
return None
return first + last
示例5: __init__
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import request [as 别名]
class TwitterAds:
twitter_consumer_key = None
twitter_consumer_secret = None
access_token = None
access_token_secret = None
base_twitter_ads_url = 'https://ads-api.twitter.com/0/'
base_twitter_ton_url = 'https:/ton.twitter.com/1.1/ton/bucket/'
twitter_ads = None
ads_account_id = None
def __init__(self, twitter_consumer_key, twitter_consumer_secret,
access_token, access_token_secret):
assert twitter_consumer_key is not None, "Provide twitter_consumer_key"
assert twitter_consumer_secret is not None, "Provide twitter_consumer_secret"
assert access_token is not None, "Provide access_token"
assert access_token_secret is not None, "Provide, access_token_secret"
self.twitter_consumer_key = twitter_consumer_key
self.twitter_consumer_secret = twitter_consumer_secret
self.access_token = access_token
self.access_token_secret = access_token_secret
self.twitter_ads = Twython(self.twitter_consumer_key, self.twitter_consumer_secret,
self.access_token, self.access_token_secret)
def get_adds_account_id_from_name(self, account_name):
ads_account_details = self.get_ads_accounts(account_name=account_name)
if not ads_account_details:
print("Account permissions for {" + account_name + "} Not granted")
return None
return ads_account_details['id']
def verify_ads_account_id(self, ads_account_id):
ads_account_details = self.get_ads_accounts(ads_account_id=ads_account_id)
if not ads_account_details:
print("Account permissions for {" + ads_account_id + "} Not granted")
return False
return True
def get_ads_accounts(self, account_name=None, ads_account_id=None):
assert account_name is not None or ads_account_id is not None, "Provide either account_name or ads_account_id"
all_ads_accounts = self.twitter_ads.request( self.base_twitter_ads_url + 'accounts')
if 'data' in all_ads_accounts:
for data in all_ads_accounts['data']:
if account_name and data['name'].lower() == account_name.lower() or\
ads_account_id and str(data['id']) == ads_account_id:
return data
return None
def get_all_ads_accounts(self):
all_ads_accounts = self.twitter_ads.request( self.base_twitter_ads_url + 'accounts')
if 'data' in all_ads_accounts:
return all_ads_accounts['data']
return None
'''
Ref - https://dev.twitter.com/ads/campaigns/funding-instruments
'''
def get_funding_instrument(self, account_id):
ads_funding_data = self.twitter_ads.request( self.base_twitter_ads_url +\
'accounts/' + account_id + "/funding_instruments")
return ads_funding_data
def get_campaigns(self, account_id):
get_campaigns_data = self.twitter_ads.request( self.base_twitter_ads_url +\
'accounts/' + account_id + "/campaigns")
return get_campaigns_data
'''
Ref - https://dev.twitter.com/ads/reference/put/accounts/%3Aaccount_id/campaigns/%3Acampaign_id
'''
def modify_campaign(self, account_id, campaign_id, start_time=None,
name=None, end_time=None, total_budget_amount_local_micro=None,
daily_budget_amount_local_micro=None,
paused=None, standard_delivery=None):
assert account_id is not None, 'account_id is required'
assert campaign_id is not None, 'campaign_id is required'
tmp_params = locals()
del tmp_params['self']
del tmp_params['account_id']
params = dict((k, v) for k, v in tmp_params.iteritems() if v)
modify_campaign_response = self.twitter_ads.request(self.base_twitter_ads_url +\
'accounts/' + account_id + '/campaigns/' + campaign_id,
params=params,
method='PUT')
#.........这里部分代码省略.........
示例6: TwythonAPITestCase
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import request [as 别名]
class TwythonAPITestCase(unittest.TestCase):
def setUp(self):
self.api = Twython('', '', '', '')
def get_url(self, endpoint):
"""Convenience function for mapping from endpoint to URL"""
return '%s/%s.json' % (self.api.api_url % self.api.api_version, endpoint)
def register_response(self, method, url, body='{}', match_querystring=False,
status=200, adding_headers=None, stream=False,
content_type='application/json; charset=utf-8'):
"""Wrapper function for responses for simpler unit tests"""
# responses uses BytesIO to hold the body so it needs to be in bytes
if not is_py2:
body = bytes(body, 'UTF-8')
responses.add(method, url, body, match_querystring,
status, adding_headers, stream, content_type)
@responses.activate
def test_request_should_handle_full_endpoint(self):
"""Test that request() accepts a full URL for the endpoint argument"""
url = 'https://api.twitter.com/1.1/search/tweets.json'
self.register_response(responses.GET, url)
self.api.request(url)
self.assertEqual(1, len(responses.calls))
self.assertEqual(url, responses.calls[0].request.url)
@responses.activate
def test_request_should_handle_relative_endpoint(self):
"""Test that request() accepts a twitter endpoint name for the endpoint argument"""
url = 'https://api.twitter.com/1.1/search/tweets.json'
self.register_response(responses.GET, url)
self.api.request('search/tweets', version='1.1')
self.assertEqual(1, len(responses.calls))
self.assertEqual(url, responses.calls[0].request.url)
@responses.activate
def test_request_should_post_request_regardless_of_case(self):
"""Test that request() accepts the HTTP method name regardless of case"""
url = 'https://api.twitter.com/1.1/statuses/update.json'
self.register_response(responses.POST, url)
self.api.request(url, method='POST')
self.api.request(url, method='post')
self.assertEqual(2, len(responses.calls))
self.assertEqual('POST', responses.calls[0].request.method)
self.assertEqual('POST', responses.calls[1].request.method)
@responses.activate
def test_request_should_throw_exception_with_invalid_http_method(self):
"""Test that request() throws an exception when an invalid HTTP method is passed"""
# TODO(cash): should Twython catch the AttributeError and throw a TwythonError
self.assertRaises(AttributeError, self.api.request, endpoint='search/tweets', method='INVALID')
@responses.activate
def test_request_should_encode_boolean_as_lowercase_string(self):
"""Test that request() encodes a boolean parameter as a lowercase string"""
endpoint = 'search/tweets'
url = self.get_url(endpoint)
self.register_response(responses.GET, url)
self.api.request(endpoint, params={'include_entities': True})
self.api.request(endpoint, params={'include_entities': False})
self.assertEqual(url + '?include_entities=true', responses.calls[0].request.url)
self.assertEqual(url + '?include_entities=false', responses.calls[1].request.url)
@responses.activate
def test_request_should_handle_string_or_number_parameter(self):
"""Test that request() encodes a numeric or string parameter correctly"""
endpoint = 'search/tweets'
url = self.get_url(endpoint)
self.register_response(responses.GET, url)
self.api.request(endpoint, params={'lang': 'es'})
self.api.request(endpoint, params={'count': 50})
self.assertEqual(url + '?lang=es', responses.calls[0].request.url)
self.assertEqual(url + '?count=50', responses.calls[1].request.url)
@responses.activate
def test_request_should_encode_list_of_strings_as_string(self):
"""Test that request() encodes a list of strings as a comma-separated string"""
endpoint = 'search/tweets'
url = self.get_url(endpoint)
location = ['37.781157', '-122.39872', '1mi']
self.register_response(responses.GET, url)
self.api.request(endpoint, params={'geocode': location})
# requests url encodes the parameters so , is %2C
self.assertEqual(url + '?geocode=37.781157%2C-122.39872%2C1mi', responses.calls[0].request.url)
#.........这里部分代码省略.........