-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_data.py
161 lines (116 loc) · 5.05 KB
/
read_data.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import requests
import json
import media_objects as mo
from Key1 import USER_TOKEN
# fetches user data
def getUser():
url = f"https://graph.facebook.com/v19.0/me?fields=id,name&access_token={USER_TOKEN}"
response = requests.request("GET", url)
data = json.loads(response.text)
return data
# fetches pages from user
def getPages(user):
url = f"https://graph.facebook.com/v19.0/{user.id}/accounts?access_token={USER_TOKEN}"
response = requests.request("GET", url)
data = json.loads(response.text)
pages = data["data"]
return pages
# fetches posts from page
def getPostsFromPage(page):
url = f"https://graph.facebook.com/v19.0/{page.id}/feed?access_token={page.token}"
response = requests.request("GET", url)
data = json.loads(response.text)
posts = data['data']
return posts
# fetches comments from post
def getCommentsFromPost(page, post):
url = f"https://graph.facebook.com/v19.0/{post.id}/comments?access_token={page.token}"
response = requests.request("GET", url);
data = json.loads(response.text)
comment_data = data['data']
return comment_data
#gets reactions from a Post or Comment object
def getReactions(page, object):
url_love = f"https://graph.facebook.com/{object.id}?fields=reactions.type(LOVE).limit(0).summary(total_count)&access_token={page.token}"
url_like = f"https://graph.facebook.com/{object.id}?fields=reactions.type(LIKE).limit(0).summary(total_count)&access_token={page.token}"
url_wow = f"https://graph.facebook.com/{object.id}?fields=reactions.type(WOW).limit(0).summary(total_count)&access_token={page.token}"
url_haha = f"https://graph.facebook.com/{object.id}?fields=reactions.type(HAHA).limit(0).summary(total_count)&access_token={page.token}"
url_haha = f"https://graph.facebook.com/{object.id}?fields=reactions.type(SAD).limit(0).summary(total_count)&access_token={page.token}"
url_angry = f"https://graph.facebook.com/{object.id}?fields=reactions.type(ANGRY).limit(0).summary(total_count)&access_token={page.token}"
urls = [url_love, url_like, url_wow, url_haha, url_angry]
reaction_dict = {"love": 0, "like": 0, "wow": 0, "haha": 0, "sad": 0, "angry": 0}
for url in urls:
response = requests.request("GET", url);
data = json.loads(response.text)
count = data['reactions']['summary']['total_count']
#updates dictionary with total count of all reactions
if "LOVE" in url:
reaction_dict["love"] = count
elif "LIKE" in url:
reaction_dict["like"] = count
elif "WOW" in url:
reaction_dict["wow"] = count
elif "HAHA" in url:
reaction_dict["haha"] = count
elif "SAD" in url:
reaction_dict["sad"] = count
elif "ANGRY" in url:
reaction_dict["angry"] = count
return reaction_dict
# gets the profile page photo from a page
def getPagePhoto(page):
page_photo_url = f"https://graph.facebook.com/v19.0/{page.id}/photos?access_token={page.token}"
response = requests.request("GET", page_photo_url);
page_photo_data = json.loads(response.text)
photo_id = page_photo_data['data'][0]['id']
photo_url = f"https://graph.facebook.com/v19.0/{photo_id}/picture?access_token={page.token}"
response = requests.request("GET", photo_url);
with open(page.photo_path, 'wb') as f: #writes the page into /photos/<photo_path>
f.write(response.content)
return response
# intitializing user
def user_init():
print("Initializing User...")
user_data = getUser()
user = mo.User(user_data['id'], user_data['name'], USER_TOKEN)
user.pages = page_init(user)
return user
# intitializing page
def page_init(user):
user_pages = getPages(user)
pages = []
for i in range(0, len(user_pages)):
page = user_pages[i]
pages.append(mo.Page(page['id'], page['name'], page['access_token']))
page = pages[i]
print("Scraping {}...".format(page.name))
page.photo_path = f"./photos/photo{i}.jpg"
page.posts = posts_init(page)
return pages
# intitializing posts
def posts_init(page):
page_posts = getPostsFromPage(page)
posts = []
for i in range(0, len(page_posts)):
print("Scraping Post...")
post = page_posts[i]
try:
posts.append(mo.Post(post['id'], post['message']))
except KeyError:
posts.append(None)
if (posts[i]):
post = posts[i]
posts[i].reactions = getReactions(page, post)
posts[i].comments = comments_init(page, post)
return posts
# intitializing comments
def comments_init(page, post):
print("Scraping Comment...")
post_comments = getCommentsFromPost(page, post)
comments = []
for i in range(0, len(post_comments)):
comment = post_comments[i]
comments.append(mo.Comment(comment['id'], comment['message']))
comment = comments[i]
comments[i].reactions = getReactions(page, comment)
return comments