-
Notifications
You must be signed in to change notification settings - Fork 1
/
aggregated.py
173 lines (129 loc) · 4.59 KB
/
aggregated.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
162
163
164
165
166
167
168
169
from urllib.request import urlopen
import json
import datetime
import csv
import time
import pandas as pd
# input group name
# input access_token
# group_name is the name of the group as it appears in the url
# access_token can be attained from Graph API Explorer
def getGroupID(group_name, access_token):
base = "https://graph.facebook.com/v2.12"
group_query = "/search?q=%s&type=group" % group_name
token = "&access_token=%s" % access_token
url = base + group_query + token
response = urlopen(url)
# data = json.loads(resp)
# return type(response.read())
data = json.loads(response.read())
return data["data"][0]["id"]
def getPostIDs(group_id):
base = "https://graph.facebook.com/v2.12/" + group_id
group_query = "/feed?fields=name"
token = "&access_token=%s" % access_token
url = base + group_query + token
response = urlopen(url)
data = json.loads(response.read())
list_of_dicts = data["data"]
list_of_group_post_ids = [each["id"] for each in list_of_dicts]
post_order_dict = {}
iter_range = len(list_of_group_post_ids)
for i in range(iter_range):
back = list_of_group_post_ids.pop()
post_order_dict[i+1] = back
return post_order_dict
# return data["data"]
# string_identifier is a sample string from the post you're looking for.
# use only words (avoid emojis or other ill-formed units of meaning)
string_identifier = """
Looking forwards to the upcoming practice! We have a lotta announcements for y'all, and have summarized them here (we will also email a more in-depth version of announcements for your convenience). Respond to the fun prompt at the end of the post! We highly value team engagement :)
"""
def findCorrectPost(group_id, string_identifier):
base = "https://graph.facebook.com/v2.12/" + group_id
group_query = "/feed?fields=name,message,comments"
token = "&access_token=%s" % access_token
url = base + group_query + token
# print(url)
response = urlopen(url)
data = json.loads(response.read())
correct_post = None
for post in data["data"]:
# print(type(list(post.keys())))
if "message" not in list(post.keys()):
continue
check = post["message"]
identical = False
for word in string_identifier.split():
if word not in check:
break
identical = True
if identical:
print(post["id"])
correct_post = post
break
# print(word)
return correct_post
def getListOfComments(correct_post):
# TRY BLOCK 1
comments = []
data = []
# Method 1A: getting comments on current page
try:
# print("try1")
data = correct_post["comments"]["data"]
for comment in data:
comments.append(comment)
# print("hsdf", next_page_responders)
except:
pass
# Method 2A: getting comments on current page
try:
data = correct_post["data"]
for comment in data:
comments.append(comment)
except:
pass
# TRY BLOCK 2
url = None
# Method 1B: accessing the next page of responses
try:
url = correct_post["comments"]["paging"]["next"]
except:
pass
try:
# Method 2B: accessing the next page of responses
url = correct_post["paging"]["next"]
except:
pass
# Open the url (found by Method 1B/2B) if it exists.
next_page_responders = []
if url:
response = urlopen(url)
next_page = json.loads(response.read())
next_page_responders = getListOfComments(next_page)
comments.extend(next_page_responders)
return comments
def getNamesFromListOfComments(list_of_comments):
responders = []
for comment in list_of_comments:
responders.append(comment["from"]["name"])
return responders
def getListOfResponders(correct_post):
responders = []
for comment in correct_post["comments"]["data"]:
responders.append(comment["from"]["name"])
return responders
def main():
group_name = "afxlowkey"
access_token = "EAALJfWzkKZAgBACDGJ7SWpGS6nbJB8XnpK2M2otvKw27OjMwhaEvVJeh1JjHCEbkqDbzEOIrCDIFhE7BOvxYBQGYdDDXHEN9Anu8gTKw7lYKMy9HsMga7OqzScZBPYreqLTmVPJTHDiWnvfxlCmC17HZCBZBUdIMzIMrtwmRoQZDZD"
group_id = getGroupID(group_name, access_token)
print(group_id)
if __name__ == "__main__":
main()
# group_id = getGroupID(group_name, access_token)
# group_id
# correct_post = findCorrectPost(group_id, string_identifier)
# correct_post
# list_of_comments = getListOfComments(correct_post)
# list_of_comments