Last active
November 22, 2024 10:32
-
-
Save StefanoCecere/533c0d54583db254059d6d6da1afce2a to your computer and use it in GitHub Desktop.
a Python script that converts Facebook exported posts (in JSON format) and saves them as multiple markdown files
This file contains hidden or 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 json | |
import html | |
from datetime import datetime | |
def save_post(filename, date, text): | |
print('----- NEW POST') | |
print(filename) | |
# print(text) | |
body = f"""--- | |
title: | |
date: {date} | |
updated: {date} | |
categories: facebook | |
tags: | |
--- | |
{text} | |
""" | |
f = open("out/" + filename + ".md", "w") | |
f.write(body) | |
f.close() | |
with open('posts/your_posts_1.json', 'r', encoding='utf-8') as json_file: | |
posts = json.load(json_file) | |
previous_date = ""; | |
duplicate_counter = 0; | |
for i in posts: | |
date_formatted = datetime.fromtimestamp(i['timestamp']).strftime('%Y-%m-%d') | |
if (date_formatted == previous_date): | |
duplicate_counter = duplicate_counter + 1 | |
else: | |
previous_date = date_formatted | |
duplicate_counter = 0 | |
for content in i['data']: | |
if content.get('post'): | |
text = content.get('post').encode('latin-1').decode('unicode_escape').encode('latin-1').decode('utf-8') | |
filename = date_formatted | |
if (duplicate_counter > 0): | |
filename = date_formatted + '_' + str(duplicate_counter) | |
save_post(filename, date_formatted, text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment