-
Notifications
You must be signed in to change notification settings - Fork 2
/
facebook_downloader.py
74 lines (56 loc) · 2.37 KB
/
facebook_downloader.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
#!/usr/bin/env python
import requests
import json
import argparse
def get_conversations(pageid, accesstoken):
conversations = []
url = "https://graph.facebook.com/" + pageid + "/conversations?access_token=" + accesstoken
print(f'Loading coversations from page id {pageid}', end='', flush=True)
while True:
r = requests.get(url)
print('.', end='', flush=True)
if r.status_code != 200:
print(f'\nReceived {r.status_code} from facebook')
print(f'Check your page id and access token')
break
try:
conversation = json.loads(r.text)
conversations.extend(conversation["data"])
url = conversation["paging"]["next"]
except (KeyError, ValueError):
break
return conversations
def save_conversation(output, conversation):
with open(output, 'a+') as file:
for c in conversation:
file.write(f"{c['created_time']} # {c['from']['name']} : {c['message']} \n")
file.write('--------------------\n')
def get_messages(conversation, accesstoken):
url = "https://graph.facebook.com/" + conversation[
"id"] + "/messages?fields=message,created_time,from&access_token=" + accesstoken
messages = []
print('.', end='', flush=True)
while True:
r = requests.get(url)
if r.status_code != 200:
break
try:
messagesjson = json.loads(r.text)
messages.extend(messagesjson["data"])
url = messagesjson["paging"]["next"]
except (KeyError, ValueError):
break
return reversed(messages)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Download facebook conversations')
parser.add_argument('-a', dest='accesstoken', required=True,
help='Facebook access token')
parser.add_argument('-p', dest='pageid', required=True,
help='Facebook page_id')
parser.add_argument('-f', dest='output', required=True, default='output.txt',
help='Output filename')
args = parser.parse_args()
convs = get_conversations(args.pageid, args.accesstoken)
print('\nLoading messages', end='', flush=True)
msgs = [save_conversation(args.output, get_messages(conversation, args.accesstoken)) for conversation in convs]
print('\nAll conversations loaded')