Skip to content

Instantly share code, notes, and snippets.

@anandtrex
Last active October 2, 2021 14:21
Show Gist options
  • Save anandtrex/98caedce1756d973f87a9fdd5ec0c287 to your computer and use it in GitHub Desktop.
Save anandtrex/98caedce1756d973f87a9fdd5ec0c287 to your computer and use it in GitHub Desktop.
Script to convert Telegram json to a html file in the Netscape Bookmarks format.
import re
import json
import dateutil.parser as parser
with open('links.html', 'wt') as fw:
fw.write("""<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Telegram Export</title>
</head>
<body>
<h1>Unread</h1>
<ul>
""")
with open('links.out.json', 'rt') as f:
ss = f.readlines()
js = json.loads(ss[0])
for j in js:
link = j.get('text')
title = j.get('media', {}).get('title', 'No Title')
if link:
m = re.search('http[s]://.*', link)
if m:
actual_link = m[0]
dt = j.get('to', {}).get('when', '')
dtime = parser.parse(dt)
fw.write(f'<li><a href="{actual_link}" time_added="{int(dtime.timestamp())}">{title}</a></li>\n')
fw.write("""
</ul>
</body>
</html>""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment