Skip to content

Commit

Permalink
Merge branch 'dylancaponi-twitter-v2'
Browse files Browse the repository at this point in the history
  • Loading branch information
geduldig committed Oct 4, 2020
2 parents 5ede20d + 16f5360 commit d870455
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
13 changes: 12 additions & 1 deletion TwitterAPI/TwitterAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def __init__(
raise Exception('Unknown oAuth version')

def _prepare_url(self, subdomain, path):
if self.version:
version = self.version

if subdomain == 'curator':
return '%s://%s.%s/%s/%s.json' % (PROTOCOL,
subdomain,
Expand All @@ -90,6 +93,13 @@ def _prepare_url(self, subdomain, path):
DOMAIN,
ADS_VERSION,
path)
elif version == '2':
return '%s://%s.%s/%s/%s' % (PROTOCOL,
subdomain,
DOMAIN,
version,
path)

elif subdomain == 'api' and 'labs/' in path:
return '%s://%s.%s/%s' % (PROTOCOL,
subdomain,
Expand All @@ -114,7 +124,7 @@ def _get_endpoint(self, resource):
else:
return (resource, resource)

def request(self, resource, params=None, files=None, method_override=None):
def request(self, resource, params=None, files=None, method_override=None, version=None):
"""Request a Twitter REST API or Streaming API resource.
:param resource: A valid Twitter endpoint (ex. "search/tweets")
Expand All @@ -125,6 +135,7 @@ def request(self, resource, params=None, files=None, method_override=None):
:returns: TwitterResponse
:raises: TwitterConnectionError
"""
self.version = version
resource, endpoint = self._get_endpoint(resource)
if endpoint not in ENDPOINTS:
raise Exception('Endpoint "%s" unsupported' % endpoint)
Expand Down
16 changes: 16 additions & 0 deletions TwitterAPI/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,21 @@
'account_activity/webhooks/:PARAM/subscriptions': ('POST', 'api'), # ENVIRONMENT NAME
'account_activity/webhooks/:PARAM/subscriptions/list': ('GET', 'api'), # ENVIRONMENT NAME

# API V2 EARLY ACCESS

'tweets/:PARAM': ('GET', 'api'), # ID
'tweets?ids=:PARAM': ('GET', 'api'), # IDS
'users/:PARAM': ('GET', 'api'), # ID
'users?ids=:PARAM': ('GET', 'api'), # IDS
'users/by/username/:PARAM': ('GET', 'api'), # USERNAME
'users/by?usernames=:PARAM': ('GET', 'api'), # USERNAMES
'tweets/search/recent?query=:PARAM': ('GET', 'api'), # QUERY
'tweets/search/stream/rules': ('POST', 'api'),
'tweets/search/stream/rules': ('GET', 'api'),
'tweets/sample/stream': ('GET', 'api'),
'tweets/:id/hidden': ('PUT', 'api'),


# PREMIUM SEARCH API

'tweets/search/30day/:PARAM': ('GET', 'api'), # LABEL
Expand All @@ -223,6 +238,7 @@
'tweets/search/fullarchive/:PARAM/counts': ('GET', 'api'), # LABEL

#LABS API (BETAS) WILL NEED APPLICATION APPROVAL

'labs/2/tweets/:PARAM': ('GET', 'api'), # TWEET ID
'labs/2/tweets': ('GET', 'api'),
'labs/2/tweets/search': ('GET', 'api'),
Expand Down
15 changes: 15 additions & 0 deletions examples/lookup_tweet_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# You must opt-in to v2 here:
# https://developer.twitter.com/en/portal/opt-in

from TwitterAPI import TwitterAPI

TWEET_ID = '964575983633252353'

api = TwitterAPI(<consumer_key>,
<consumer_secret>,
auth_type='oAuth2')

r = api.request(f'tweets/:{TWEET_ID}' , version='2')
tweet = r.json()
print(f"{tweet['data']['id']}: {tweet['data']['text']}" if r.status_code == 200
else f"PROBLEM: {r.text}")
16 changes: 16 additions & 0 deletions examples/lookup_user_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Documentation
# https://documenter.getpostman.com/view/9956214/T1LMiT5U#auth-info-784efcda-ed4c-4491-a4c0-a26470a67400

# You must opt-in to v2 here:
# https://developer.twitter.com/en/portal/opt-in

from TwitterAPI import TwitterAPI, TwitterPager

SCREEN_NAME = 'TheTweetOfGod'

api = TwitterAPI(<consumer_key>,
<consumer_secret>,
auth_type='oAuth2')

r = api.request(f'users/by/username/:{SCREEN_NAME}', version='2')
print(r.json()['data']['id'] if r.status_code == 200 else 'PROBLEM: ' + r.text)

0 comments on commit d870455

Please sign in to comment.