Skip to content

Instantly share code, notes, and snippets.

@multun
Created December 26, 2018 22:51
Show Gist options
  • Save multun/f487fc648de893c136298a8491ad5f16 to your computer and use it in GitHub Desktop.
Save multun/f487fc648de893c136298a8491ad5f16 to your computer and use it in GitHub Desktop.
Pretty-print a facebook message.json, fixing up broken encoding
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:])
@glensc
Copy link

glensc commented Jun 27, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment