-
Notifications
You must be signed in to change notification settings - Fork 0
/
fb-clipper.py
255 lines (215 loc) · 13.1 KB
/
fb-clipper.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from sys import argv
from os import listdir, chdir
from os.path import isfile, isdir
from csv import writer, QUOTE_MINIMAL
from collections import defaultdict
import json
WORD_FILTER = []
def get_N_first(dict_words, N=False, values=False):
'''
Return the N topwords of a list.
'''
aux = []
top_words = []
if N == 0: return aux
for key, value in dict_words.items():
top_words.append([key, value])
top_words.sort(key=lambda x:x[1], reverse=True)
if values: # [key,value]
for item in top_words:
aux.append([item[0], item[1]])
else: # default; key only
for item in top_words:
aux.append(item[0])
return aux[:N] if N else aux
def parse_fb_json(input_path='.', word_filter=[]):
q = []
PROFILE = ""
dict_info = defaultdict(dict)
dict_shares = defaultdict(int)
dict_angry_filter = defaultdict(int)
dict_angry_unfilter = defaultdict(int)
dict_profile = defaultdict(int)
dict_filter = defaultdict(int)
dict_unfilter = defaultdict(int)
for j in listdir(input_path):
if j.endswith('.json'):
print(j)
with open(j, "r") as read_file:
data = json.load(read_file)
if 'data' in data:
pass
# try: # PAGES
# HEADER = ['id', 'message', 'story', 'created_time']
# for i in data['data']:
# story = i['story'].replace('\n',' ').replace('\r','') if 'story' in i else ''
# created_time = i['created_time'] if 'created_time' in i else ''
# try: message = i['message'].replace('\n',' ').replace('\r','') if 'message' in i else ''
# except Exception as e: print(str(e))
# str_id = i['id'] if 'id' in i else ''
# except Exception as e:
# if 'error' in data\
# and 'message' in data['error']:
# print(j, '=>', data['error']['message'])
# else: print(str(e))
else: # single post
try: # POSTS
HEADER = ['id', 'message', 'from_name', 'created_time', 'shares_count', 'comments', 'love', 'wow', 'haha', 'sad', 'angry', 'like']
if any(i in data for i in ['message', 'story']):
str_id = str(data['id']).replace('\r','').replace('\n','')
try: message = str(data['message']).replace('\r','').replace('\n',' ') if 'message' in data else ''
except Exception as e: print(str(e))
from_name = str(data['from']['name']).replace('\r','').replace('\n','')
created_time = str(data['created_time']).replace('\r','').replace('\n','')
if 'shares' in data:
shares_count = str(data['shares']['count']).replace('\r','').replace('\n','')
else: shares_count = '0'
comments_summary_total_count = str(data['comments']['summary']['total_count']).replace('\r','').replace('\n','')
reactions_love_summary_total_count = str(data['reactions_love']['summary']['total_count']).replace('\r','').replace('\n','')
reactions_wow_summary_total_count = str(data['reactions_wow']['summary']['total_count']).replace('\r','').replace('\n','')
reactions_haha_summary_total_count = str(data['reactions_haha']['summary']['total_count']).replace('\r','').replace('\n','')
reactions_sad_summary_total_count = str(data['reactions_sad']['summary']['total_count']).replace('\r','').replace('\n','')
reactions_angry_summary_total_count = str(data['reactions_angry']['summary']['total_count']).replace('\r','').replace('\n','')
reactions_like_summary_total_count = str(data['reactions_like']['summary']['total_count']).replace('\r','').replace('\n','')
dict_info[str_id]['message'] = message
dict_info[str_id]['from_name'] = from_name
dict_info[str_id]['created_time'] = created_time
dict_info[str_id]['shares_count'] = shares_count
dict_info[str_id]['comments_summary_total_count'] = comments_summary_total_count
dict_info[str_id]['reactions_love_summary_total_count'] = reactions_love_summary_total_count
dict_info[str_id]['reactions_wow_summary_total_count'] = reactions_wow_summary_total_count
dict_info[str_id]['reactions_haha_summary_total_count'] = reactions_haha_summary_total_count
dict_info[str_id]['reactions_sad_summary_total_count'] = reactions_sad_summary_total_count
dict_info[str_id]['reactions_angry_summary_total_count'] = reactions_angry_summary_total_count
dict_info[str_id]['reactions_like_summary_total_count'] = reactions_like_summary_total_count
if from_name in ['Renato Casagrande', 'Aridelmo Teixeira', 'Jackeline Rocha', 'André Moreira', 'Manato', 'Rose de Freitas']:
dict_profile[str_id] = int(shares_count)
PROFILE = from_name
else: dict_shares[str_id] = int(shares_count)
if (any(a in message.lower() for a in word_filter)):
dict_filter[str_id] = int(shares_count)
dict_angry_filter[str_id] = int(reactions_angry_summary_total_count)
else: # geral
dict_unfilter[str_id] = int(shares_count)
dict_angry_unfilter[str_id] = int(reactions_angry_summary_total_count)
q.append([
str_id,
message,
from_name,
created_time,
shares_count,
comments_summary_total_count,
reactions_love_summary_total_count,
reactions_wow_summary_total_count,
reactions_haha_summary_total_count,
reactions_sad_summary_total_count,
reactions_angry_summary_total_count,
reactions_like_summary_total_count])
elif 'error' in data\
and 'message' in data['error']:
print(j, '=>', data['error']['message'])
except Exception as e:
raise#print(str(e))
with open('posts.tab', 'w', newline='', encoding='utf8') as f:
file_writer = writer(f, delimiter='\t', quoting=QUOTE_MINIMAL)
file_writer.writerow(HEADER)
for line in q:
file_writer.writerow(line)
with open('clipping.txt', 'w', newline='', encoding='utf8') as f:
count = 0
if PROFILE == "":
for i in get_N_first(dict_filter, 20):
count += 1
m = str(count)+') ' + dict_info[i]['from_name']+'\nMensagem: '+dict_info[i]['message'][:500]+\
'\nRaiva: ' + dict_info[i]['reactions_angry_summary_total_count']+\
' Haha: ' + dict_info[i]['reactions_haha_summary_total_count']+\
' Triste: ' + dict_info[i]['reactions_sad_summary_total_count']+\
' Uau: ' + dict_info[i]['reactions_wow_summary_total_count']+\
' Amei: ' + dict_info[i]['reactions_love_summary_total_count']+\
'\nCurtidas: ' + dict_info[i]['reactions_like_summary_total_count']+\
' Comentários: ' + dict_info[i]['comments_summary_total_count']+\
' Compartilhamentos: ' + dict_info[i]['shares_count']+\
'\nLink: https://www.facebook.com/'+i+'\n'
f.write(m+'\n')
f.write('Com mais reações de indignação:\n\n')
count = 0
for i in get_N_first(dict_angry_filter, 5):
count += 1
m = str(count)+') ' + dict_info[i]['from_name']+'\nMensagem: '+dict_info[i]['message'][:500]+\
'\nRaiva: ' + dict_info[i]['reactions_angry_summary_total_count']+\
' Haha: ' + dict_info[i]['reactions_haha_summary_total_count']+\
' Triste: ' + dict_info[i]['reactions_sad_summary_total_count']+\
' Uau: ' + dict_info[i]['reactions_wow_summary_total_count']+\
' Amei: ' + dict_info[i]['reactions_love_summary_total_count']+\
'\nCurtidas: ' + dict_info[i]['reactions_like_summary_total_count']+\
' Comentários: ' + dict_info[i]['comments_summary_total_count']+\
' Compartilhamentos: ' + dict_info[i]['shares_count']+\
'\nLink: https://www.facebook.com/'+i+'\n'
f.write(m+'\n')
f.write('\n\nGERAL\n\n')
count = 0
for i in get_N_first(dict_unfilter, 20):
count += 1
m = str(count)+') ' + dict_info[i]['from_name']+'\nMensagem: '+dict_info[i]['message'][:500]+\
'\nRaiva: ' + dict_info[i]['reactions_angry_summary_total_count']+\
' Haha: ' + dict_info[i]['reactions_haha_summary_total_count']+\
' Triste: ' + dict_info[i]['reactions_sad_summary_total_count']+\
' Uau: ' + dict_info[i]['reactions_wow_summary_total_count']+\
' Amei: ' + dict_info[i]['reactions_love_summary_total_count']+\
'\nCurtidas: ' + dict_info[i]['reactions_like_summary_total_count']+\
' Comentários: ' + dict_info[i]['comments_summary_total_count']+\
' Compartilhamentos: ' + dict_info[i]['shares_count']+\
'\nLink: https://www.facebook.com/'+i+'\n'
f.write(m+'\n')
f.write('Com mais reações de indignação:\n\n')
count = 0
for i in get_N_first(dict_angry_unfilter, 5):
count += 1
m = str(count)+') ' + dict_info[i]['from_name']+'\nMensagem: '+dict_info[i]['message'][:500]+\
'\nRaiva: ' + dict_info[i]['reactions_angry_summary_total_count']+\
' Haha: ' + dict_info[i]['reactions_haha_summary_total_count']+\
' Triste: ' + dict_info[i]['reactions_sad_summary_total_count']+\
' Uau: ' + dict_info[i]['reactions_wow_summary_total_count']+\
' Amei: ' + dict_info[i]['reactions_love_summary_total_count']+\
'\nCurtidas: ' + dict_info[i]['reactions_like_summary_total_count']+\
' Comentários: ' + dict_info[i]['comments_summary_total_count']+\
' Compartilhamentos: ' + dict_info[i]['shares_count']+\
'\nLink: https://www.facebook.com/'+i+'\n'
f.write(m+'\n')
else:
f.write('\n---\n' + PROFILE + '\n---\n\n')
for i in get_N_first(dict_shares, 7):
count += 1
m = str(count)+') ' + dict_info[i]['from_name']+'\nMensagem: '+dict_info[i]['message'][:500]+\
'\nRaiva: ' + dict_info[i]['reactions_angry_summary_total_count']+\
' Haha: ' + dict_info[i]['reactions_haha_summary_total_count']+\
' Triste: ' + dict_info[i]['reactions_sad_summary_total_count']+\
' Uau: ' + dict_info[i]['reactions_wow_summary_total_count']+\
' Amei: ' + dict_info[i]['reactions_love_summary_total_count']+\
'\nCurtidas: ' + dict_info[i]['reactions_like_summary_total_count']+\
' Comentários: ' + dict_info[i]['comments_summary_total_count']+\
' Compartilhamentos: ' + dict_info[i]['shares_count']+\
'\nLink: https://www.facebook.com/'+i+'\n'
f.write(m+'\n')
f.write('Perfil ' + PROFILE + ':\n\n')
count = 0
for i in get_N_first(dict_profile, 7):
count += 1
m = str(count)+') ' + dict_info[i]['from_name']+'\nMensagem: '+dict_info[i]['message'][:500]+\
'\nRaiva: ' + dict_info[i]['reactions_angry_summary_total_count']+\
' Haha: ' + dict_info[i]['reactions_haha_summary_total_count']+\
' Triste: ' + dict_info[i]['reactions_sad_summary_total_count']+\
' Uau: ' + dict_info[i]['reactions_wow_summary_total_count']+\
' Amei: ' + dict_info[i]['reactions_love_summary_total_count']+\
'\nCurtidas: ' + dict_info[i]['reactions_like_summary_total_count']+\
' Comentários: ' + dict_info[i]['comments_summary_total_count']+\
' Compartilhamentos: ' + dict_info[i]['shares_count']+\
'\nLink: https://www.facebook.com/'+i+'\n'
f.write(m+'\n')
if __name__ == '__main__':
input_name = '.'
if len(argv) > 1:
input_name = argv[1]
parse_fb_json(input_name, WORD_FILTER)