-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindividual (2).py
More file actions
83 lines (62 loc) · 2.3 KB
/
individual (2).py
File metadata and controls
83 lines (62 loc) · 2.3 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
75
76
77
78
79
80
81
82
83
from typing import Text
from requests.models import Response
import tweepy
import matplotlib.pyplot as plt
import requests
import os
import json
import numpy as np
import sys
import csv
from twarc_csv import CSVConverter
import sys
#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)
def create_url():
# Replace with user ID below
user_id = ####
return "https://api.twitter.com/2/users/{}/tweets".format(user_id)
def get_params():
# Tweet fields are adjustable.
# Options include:
# attachments, author_id, context_annotations,
# conversation_id, created_at, entities, geo, id,
# in_reply_to_user_id, lang, non_public_metrics, organic_metrics,
# possibly_sensitive, promoted_metrics, public_metrics, referenced_tweets,
# source, text, and withheld
return {"tweet.fields": "created_at", "tweet.fields": "author_id","user.fields": "username",'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", url, headers=headers, params=params)
print(response.status_code)
if response.status_code != 200:
raise Exception(
"Request returned an error: {} {}".format(
response.status_code, response.text
)
)
return response.json()
def main():
url = create_url()
params = get_params()
headers = create_headers (bearer_token)
json_response = connect_to_endpoint(url, headers, params)
# print(json.dumps(json_response, indent=4, sort_keys=True))
with open("#individual.json", "w") as f:
json.dump(json_response, f, indent=4, sort_keys=True)
print("Finished crawling, saving json-file.")
if __name__ == "__main__":
main()