-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
74 lines (50 loc) · 2.12 KB
/
example.py
File metadata and controls
74 lines (50 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from typing import Text
from requests.models import Response
import tweepy
import requests
import json
#uncomment any code to test.
# Create these keys and tokens in your Twitter Developer account check #readme
consumer_key = "#"
consumer_secret = "#"
bearer_token = "#"
Access_token = "#"
Access_token_secret = "#"
# Creating the authentication object
auth = tweepy.OAuthHandler(consumer_secret, consumer_key)
# Setting your access token and secret
auth.set_access_token(Access_token,Access_token_secret)
# Creating the API object while passing in auth information
api = tweepy.API(auth)
#public_tweets = api.home_timeline()
# foreach through all tweets pulled
#for tweet in public_tweets:
# printing the text stored inside the tweet object
#print (tweet.text)
#status = "Testing!"
#api.update_status(status=status)
search_url = "https://api.twitter.com/2/tweets/search/recent?"
#query_params = { 'query': '#Alshabaab (Alshabaab OR shabaab OR Jihad OR Mujahidin OR mandera OR wajir -is:retweet) OR #Garrissa ',
#'tweet.fields': 'author_id', }
#Use your on topic of interest or Keywords or Lexicons at #HERE
query_params = { 'query': '#HERE ( -is:retweet)',
'tweet.fields': 'author_id', 'tweet.fields': 'created_at', 'max_results':'100', }
def create_headers(bearer_token):
headers = {"Authorization": "Bearer {}".format(bearer_token)}
return headers
def connect_to_endpoint(url, headers, params):
response = requests.request("GET", search_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():
headers = create_headers(bearer_token)
json_response = connect_to_endpoint(search_url, headers, query_params)
# print(json.dumps(json_response, indent=4, sort_keys=True))
#output_file = json.dumps
with open("example.json", "w") as f:
json.dump(json_response, f, indent=4, sort_keys=True)
if __name__ == "__main__":
main()
print("Finished crawling, saving json-file.")