Created
December 3, 2023 16:27
-
-
Save techtide/724ccf8773071c23e0617863e6e1d5c3 to your computer and use it in GitHub Desktop.
Parse exported/saved messages from Telegram into Markdown for Obsidian or Roam Research
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 json | |
import jsonschema | |
import os | |
from datetime import datetime | |
schema = { | |
"$schema": "http://json-schema.org/draft-04/schema#", | |
"type": "object", | |
"properties": { | |
"type": {"type": "string"}, | |
"id": {"type": "integer"}, | |
"messages": { | |
"type": "array", | |
"items": [ | |
{ | |
"type": "object", | |
"properties": { | |
"id": {"type": "integer"}, | |
"type": {"type": "string"}, | |
"date": {"type": "string"}, | |
"date_unixtime": {"type": "string"}, | |
"from": {"type": "string"}, | |
"from_id": {"type": "string"}, | |
"text": {"type": "string"}, | |
"text_entities": { | |
"type": "array", | |
"items": [ | |
{ | |
"type": "object", | |
"properties": { | |
"type": {"type": "string"}, | |
"text": {"type": "string"}, | |
}, | |
"required": ["type", "text"], | |
} | |
], | |
}, | |
}, | |
"required": [ | |
"id", | |
"type", | |
"date", | |
"date_unixtime", | |
"from", | |
"from_id", | |
"text", | |
"text_entities", | |
], | |
} | |
], | |
}, | |
}, | |
"required": ["type", "id", "messages"], | |
} | |
def parse_json(json_data): | |
jsonschema.validate(instance=json_data, schema=schema) | |
output_dir = "obsidian_notes" | |
if not os.path.exists(output_dir): | |
os.makedirs(output_dir) | |
for message in json_data["messages"]: | |
# Create a unique filename based on message id and date | |
filename = f"{output_dir}/{message['id']}_{message['date']}.md" | |
# Convert date string to datetime object | |
message_date = datetime.strptime(message["date"], "%Y-%m-%dT%H:%M:%S") | |
# Write Markdown content to file | |
with open(filename, "w") as file: | |
file.write(f"# {message['from']} - {message_date}\n\n") | |
file.write(f"{message['text']}\n\n") | |
for entity in message.get("text_entities", []): | |
file.write(f"**{entity['type']}**: {entity['text']}\n") | |
# Example usage | |
if __name__ == "__main__": | |
with open("result.json", "r") as json_file: | |
json_data = json.load(json_file) | |
# Parse and create Markdown files | |
parse_json(json_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment