Created
December 26, 2018 22:51
-
-
Save multun/f487fc648de893c136298a8491ad5f16 to your computer and use it in GitHub Desktop.
Pretty-print a facebook message.json, fixing up broken encoding
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
import sys | |
import json | |
from datetime import datetime | |
def fixup_str(text): | |
return text.encode('latin1').decode('utf8') | |
def fixup_list(l): | |
return [fixup(e) for e in l] | |
def fixup_dict(dct): | |
return {fixup_str(k): fixup(v) for k, v in dct.items()} | |
def fixup(e): | |
if isinstance(e, dict): | |
return fixup_dict(e) | |
if isinstance(e, list): | |
return fixup_list(e) | |
if isinstance(e, str): | |
return fixup_str(e) | |
return e | |
def filter_messages(messages): | |
for message in messages: | |
if message['type'] == 'Generic': | |
yield message | |
def format_message(message): | |
sender = message['sender_name'] | |
timestamp = datetime.fromtimestamp(message['timestamp_ms']/1000) | |
content = message['content'] | |
return f'{timestamp}\t{sender}\t{content}' | |
def main(args): | |
fname = 'message.json' | |
if args: | |
fname = args[0] | |
with open(fname, 'r') as f: | |
messages = fixup(json.loads(f.read()))['messages'] | |
for message in filter_messages(messages): | |
print(format_message(message)) | |
if __name__ == '__main__': | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey. Fixed some issues in my fork:
See details here: