-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3c6aa9a
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Alex Land | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Facebook Message Converter | ||
There are a few good projects on GitHub for pulling statistics from Facebook message archives, like [conversation-analyzer](https://github.com/5agado/conversation-analyzer) and [FacebookChatStatistics](https://github.com/davidkrantz/FacebookChatStatistics), but recent changes to the format of Facebook export archives have broken compatibility. This script allows for conversion from the new JSON message archive format to formats that work with the above tools. | ||
|
||
# Formats | ||
This script exports in a simple text format used by [conversation-analyzer](https://github.com/5agado/conversation-analyzer), as well as the JSON format defined by [fbchat-archive-parser](https://github.com/ownaginatious/fbchat-archive-parser) (used by [FacebookChatStatistics](https://github.com/davidkrantz/FacebookChatStatistics)). | ||
|
||
# Usage | ||
``` | ||
python3 facebook-message-converter.py --help | ||
usage: facebook-message-converter.py [-h] --in ARCHIVEPATH --out OUTPATH | ||
--format FORMAT | ||
FB Message Archive Converter | ||
optional arguments: | ||
-h, --help show this help message and exit | ||
--in ARCHIVEPATH Path to JSON archive | ||
--out OUTPATH Path to output file | ||
--format FORMAT txt or json, for conversation-analyzer or | ||
FacebookChatStatistics respectively | ||
``` | ||
|
||
### Example usage: | ||
``` | ||
# Convert to a json format for FacebookChatStatistics | ||
python3 facebook-message-converter.py --in message.json --out converted.json --format json | ||
``` | ||
|
||
``` | ||
# Convert to a txt format for conversation-analyzer | ||
python3 facebook-message-converter.py --in message.json --out converted.txt --format txt | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import json | ||
import argparse | ||
import heapq | ||
from datetime import datetime | ||
|
||
DATE_FORMAT = '%Y.%m.%d' | ||
TIME_FORMAT = '%H:%M:%S' | ||
MESSAGE_KEY = 2 | ||
|
||
parser = argparse.ArgumentParser(description='FB Message Archive Converter') | ||
parser.add_argument('--in', dest='archivePath', required=True, help="Path to JSON archive") | ||
parser.add_argument('--out', dest='outPath', required=True, help="Path to output file") | ||
parser.add_argument('--format', dest='format', required=True, help='txt or json, for conversation-analyzer or FacebookChatStatistics respectively') | ||
args = parser.parse_args() | ||
|
||
with open(args.archivePath, 'r') as json_file: | ||
data = json.load(json_file) | ||
|
||
heap = [] | ||
senders = set() | ||
tiebreaker_counter = 0 | ||
for message in data['messages']: | ||
message_datetime = datetime.fromtimestamp(int(message['timestamp'])) | ||
if 'content' not in message: | ||
continue | ||
if 'json' in args.format: | ||
sender = message['sender_name'].encode('iso-8859-1').decode('utf-8') | ||
senders.add(sender) | ||
new_message = { | ||
"date": message_datetime.isoformat(), | ||
"sender": sender, | ||
"message": message['content'].encode('iso-8859-1').decode('utf-8') | ||
} | ||
else: | ||
new_message = "{date} {time} {sender} {message}\n".format(date=message_datetime.strftime(DATE_FORMAT), | ||
time=message_datetime.strftime(TIME_FORMAT), | ||
sender=message['sender_name'].replace(' ', '').encode( | ||
'iso-8859-1').decode('utf-8'), | ||
message=message['content'].replace('\n', ' ').encode( | ||
'iso-8859-1').decode('utf-8')) | ||
heapq.heappush(heap, (int(message['timestamp']), tiebreaker_counter, new_message)) | ||
tiebreaker_counter += 1 | ||
|
||
messages = sorted(heap, key=lambda x: x[0]) | ||
messages = [message[MESSAGE_KEY] for message in messages] | ||
if 'json' in args.format: | ||
json_output = { | ||
"threads": [ | ||
{ | ||
"participants": list(senders), | ||
"messages": messages | ||
} | ||
] | ||
} | ||
with open(args.outPath, 'w', encoding='utf-8') as out_file: | ||
out_file.write(json.dumps(json_output)) | ||
else: | ||
with open(args.outPath, 'w', encoding='utf-8') as out_file: | ||
out_file.writelines(messages) |