-
-
Save sparack/6fba846e0cfdbd6d7dccd2975718ac0b to your computer and use it in GitHub Desktop.
Sample code to demo how to search using entity in queries
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_headers(bearer_token): | |
headers = {"Authorization": "Bearer {}".format(bearer_token)} | |
return headers | |
def connect_to_endpoint(url, headers, params): | |
response = requests.request("GET", url, headers=headers, params=params) | |
print(response.status_code) | |
if response.status_code != 200: | |
raise Exception(response.status_code, response.text) | |
return response.json() | |
def main(): | |
# Replace with your own bearer token from your academic project in developer portal | |
bearer_token = "REPLACE_ME" | |
url = "https://api.twitter.com/2/tweets/search/all" | |
headers = create_headers(bearer_token) | |
# Replace query and additional fields below with your own query and requested fields | |
# Note the use of entity in the query | |
params = {'query': 'entity:Baltimore Ravens', 'tweet.fields': 'created_at,context_annotations'} | |
json_response = connect_to_endpoint(url, headers, params) | |
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