-
-
Save sparack/af415adcb9b1c959eda502faf9c5abe3 to your computer and use it in GitHub Desktop.
Sample script to demo use of Tweet annotations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
def create_url(): | |
# Replace the terms in the query below to see more annotations and named entities | |
query = "lamar jackson" | |
tweet_fields = "tweet.fields=context_annotations" | |
url = "https://api.twitter.com/2/tweets/search/recent?query={}&{}".format(query, tweet_fields) | |
return url | |
def create_headers(): | |
# Replace the XXXXX with your own bearer token from you developer portal | |
headers = {"Authorization": "Bearer XXXXX"} | |
return headers | |
def connect_to_endpoint(url, headers): | |
response = requests.request("GET", url, headers=headers) | |
print(response.status_code) | |
if response.status_code != 200: | |
raise Exception(response.status_code, response.text) | |
return response.json() | |
def main(): | |
url = create_url() | |
headers = create_headers() | |
json_response = connect_to_endpoint(url, headers) | |
print(json.dumps(json_response, indent=4, sort_keys=True)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment