Skip to content

Instantly share code, notes, and snippets.

@canassa
Created January 12, 2019 17:14
Show Gist options
  • Save canassa/e56f00a3253193b21e8ad779e0ee76f7 to your computer and use it in GitHub Desktop.
Save canassa/e56f00a3253193b21e8ad779e0ee76f7 to your computer and use it in GitHub Desktop.
Converts the Instagram messages.json to text files
import json
from dateutil import parser
data = json.loads(open('messages.json').read())
for c in data:
with open("conversations/" + "-".join(c["participants"]) + ".txt", "w") as f:
for m in c["conversation"]:
created_at = parser.parse(m["created_at"]).strftime("%Y-%m-%d %H:%M")
f.write(f"[{created_at}] {m['sender']}: ")
if 'story_share' in m:
f.write(f"[story_share]: {m['story_share']}")
elif 'text' in m and m['text']:
f.write(f"{m['text']}")
elif 'heart' in m:
f.write(f"{m['heart']}")
elif 'voice_media' in m:
f.write(f"[voice]: {m['voice_media']}")
elif 'media_share_url' in m:
f.write(f"[media_share_url]: {m['media_share_url']}")
elif 'media' in m:
f.write(f"[media]: {m['media']}")
elif 'action' in m:
f.write(f"[action]: {m['action']}")
elif 'media_url' in m:
f.write(f"[media_url]: {m['media_url']}")
elif 'animated_media_images' in m:
f.write(f"[animated_media_images]: {m['animated_media_images']['original']['url']}")
elif 'video_call_action' in m:
f.write(f"[video_call_action]: {m['video_call_action']}")
elif 'profile_share_name' in m:
f.write(f"[profile_share_name]: {m['profile_share_name']}")
elif 'live_video_invite' in m:
f.write(f"[live_video_invite]: {m['live_video_invite']}")
else:
raise Exception(m)
f.write("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment