mirror of
https://github.com/KnugiHK/WhatsApp-Chat-Exporter.git
synced 2026-04-25 07:21:36 +00:00
Mainly work on CSS and add time into the template
This commit is contained in:
37
extract.py
37
extract.py
@@ -4,6 +4,16 @@ import sqlite3
|
|||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
import jinja2
|
import jinja2
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
def determine_day(last, current):
|
||||||
|
last = datetime.fromtimestamp(last).date()
|
||||||
|
current = datetime.fromtimestamp(current).date()
|
||||||
|
#print(last, current)
|
||||||
|
if last == current:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return current
|
||||||
|
|
||||||
data = {}
|
data = {}
|
||||||
wa = sqlite3.connect("wa.db")
|
wa = sqlite3.connect("wa.db")
|
||||||
@@ -18,22 +28,41 @@ while row is not None:
|
|||||||
msg = sqlite3.connect("msgstore.db")
|
msg = sqlite3.connect("msgstore.db")
|
||||||
c = msg.cursor()
|
c = msg.cursor()
|
||||||
|
|
||||||
c.execute("""SELECT key_remote_jid, _id, key_from_me, data FROM "main"."messages"; """)
|
c.execute("""SELECT key_remote_jid, _id, key_from_me, timestamp, data FROM "main"."messages"; """)
|
||||||
content = c.fetchone()
|
content = c.fetchone()
|
||||||
while content is not None:
|
while content is not None:
|
||||||
if content[0] not in data:
|
if content[0] not in data:
|
||||||
data[content[0]] = {"name": None, "messages": {}}
|
data[content[0]] = {"name": None, "messages": {}}
|
||||||
data[content[0]]["messages"][content[1]] = {"from_me": bool(content[2]), "data": content[3]}
|
data[content[0]]["messages"][content[1]] = {
|
||||||
|
"from_me": bool(content[2]),
|
||||||
|
"timestamp": content[3]/1000,
|
||||||
|
"time": datetime.fromtimestamp(content[3]/1000).strftime("%H:%M"),
|
||||||
|
"data": content[4]
|
||||||
|
}
|
||||||
content = c.fetchone()
|
content = c.fetchone()
|
||||||
|
|
||||||
templateLoader = jinja2.FileSystemLoader(searchpath="./")
|
templateLoader = jinja2.FileSystemLoader(searchpath="./")
|
||||||
templateEnv = jinja2.Environment(loader=templateLoader)
|
templateEnv = jinja2.Environment(loader=templateLoader)
|
||||||
|
templateEnv.globals.update(determine_day=determine_day)
|
||||||
TEMPLATE_FILE = "whatsapp.html"
|
TEMPLATE_FILE = "whatsapp.html"
|
||||||
template = templateEnv.get_template(TEMPLATE_FILE)
|
template = templateEnv.get_template(TEMPLATE_FILE)
|
||||||
|
|
||||||
for i in data:
|
for i in data:
|
||||||
with open(f"temp/{i.split('@')[0]}.html", "w", encoding="utf-8") as f:
|
if len(data[i]["messages"]) == 0:
|
||||||
f.write(template.render(name=data[i]["name"], msgs=data[i]["messages"].values()))
|
continue
|
||||||
|
phone_number = i.split('@')[0]
|
||||||
|
if "-"in i:
|
||||||
|
file_name = ""
|
||||||
|
else:
|
||||||
|
file_name = phone_number
|
||||||
|
|
||||||
|
if data[i]["name"] is not None:
|
||||||
|
if file_name != "":
|
||||||
|
file_name += "-"
|
||||||
|
file_name += data[i]["name"].replace("/", "-")
|
||||||
|
|
||||||
|
with open(f"temp/{file_name}.html", "w", encoding="utf-8") as f:
|
||||||
|
f.write(template.render(name=data[i]["name"] if data[i]["name"] is not None else phone_number, msgs=data[i]["messages"].values()))
|
||||||
|
|
||||||
with open("result.json", "w") as f:
|
with open("result.json", "w") as f:
|
||||||
f.write(json.dumps(data))
|
f.write(json.dumps(data))
|
||||||
@@ -1,25 +1,75 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Whatsapp - {{ name }}</title>
|
<title>Whatsapp - {{ name }}</title>
|
||||||
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
|
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
|
||||||
</head>
|
<style>
|
||||||
<body>
|
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+HK:wght@300;400&display=swap');
|
||||||
<h1 class="w3-center w3-top" style="position:sticky">Chat history with {{ name }}</h1>
|
html {
|
||||||
<div class="w3-container w3-sand w3-center" style="width:60%;margin:auto;z-index: 999999;">
|
font-family: 'Noto Sans HK', sans-serif;
|
||||||
<table class="w3-center" style="width:100%;margin-right:;">
|
font-size: 12px;
|
||||||
<div style="width:100%">
|
}
|
||||||
{% for msg in msgs -%}
|
header {
|
||||||
<tr>
|
position: fixed;
|
||||||
{% if msg.from_me == true %}
|
z-index: 20;
|
||||||
<td style="text-align: right;height:auto;">{{ msg.data }}</td>
|
border-bottom: 2px solid #e3e6e8;
|
||||||
{% else %}
|
font-size: 2em;
|
||||||
<td style="text-align: left;height:auto;">{{ msg.data }}</td>
|
font-weight: bolder;
|
||||||
{% endif %}
|
background-color: white;
|
||||||
</tr>
|
padding: 20px 0 20px 0;
|
||||||
{% endfor %}
|
}
|
||||||
</div>
|
footer {
|
||||||
</table>
|
border-top: 2px solid #e3e6e8;
|
||||||
</div>
|
font-size: 2em;
|
||||||
</body>
|
padding: 20px 0 20px 0;
|
||||||
|
}
|
||||||
|
article {
|
||||||
|
width:500px;
|
||||||
|
margin:100px auto;
|
||||||
|
z-index:10;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header class="w3-center w3-top">Chat history with {{ name }}</header>
|
||||||
|
<article class="w3-container">
|
||||||
|
<div class="table" style="width:100%">
|
||||||
|
{% set last = {'last': 946688461.001} %}
|
||||||
|
{% for msg in msgs -%}
|
||||||
|
<div class="w3-row" style="padding-bottom: 10px">
|
||||||
|
{% if determine_day(last.last, msg.timestamp) is not none %}
|
||||||
|
<div class="w3-center" style="color:#70777c;padding: 10px 0 10px 0;">{{ determine_day(last.last, msg.timestamp) }}</div>
|
||||||
|
{% if last.update({'last': msg.timestamp}) %}{% endif %}
|
||||||
|
{% endif %}
|
||||||
|
{% if msg.from_me == true %}
|
||||||
|
<div class="w3-row">
|
||||||
|
<div style="float: left; color:#70777c;">{{ msg.time }}</div>
|
||||||
|
<div style="padding-left: 10px; text-align: right; color: #3892da;">You</div>
|
||||||
|
</div>
|
||||||
|
<div class="w3-row">
|
||||||
|
<div class="w3-col m10 l10">
|
||||||
|
<div style="text-align: right;">{% filter escape %}{{ msg.data }}{% endfilter %}</div>
|
||||||
|
</div>
|
||||||
|
<div class="w3-col m2 l2" style="padding-left: 10px"><span>{icon}</span></div>
|
||||||
|
</div>
|
||||||
|
{% else %}
|
||||||
|
<div class="w3-row">
|
||||||
|
<div style="padding-right: 10px; float: left; color: #3892da;">{{ name }}</div>
|
||||||
|
<div style="text-align: right; color:#70777c;">{{ msg.time }}</div>
|
||||||
|
</div>
|
||||||
|
<div class="w3-row">
|
||||||
|
<div class="w3-col m2 l2"><span>{icon}</span></div>
|
||||||
|
<div class="w3-col m10 l10">
|
||||||
|
<div style="text-align: left;">{% filter escape %}{{ msg.data }}{% endfilter %}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
</article>
|
||||||
|
<footer class="w3-center">
|
||||||
|
End of history
|
||||||
|
</footer>
|
||||||
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user