forked from sytelus/JSON-Messenger-Parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
277 lines (226 loc) · 10.3 KB
/
main.py
File metadata and controls
277 lines (226 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author : Luca Corrieri
# JSON-messenger-exporter
# 2019 MIT License
from datetime import datetime
import sys, getopt, json, time, os.path, os
from DateFormatter import dateFormat, frenchDateFormat
from jinja2 import Environment, FileSystemLoader
# ------------------- Message and Conversation classes -------------------------
class Message():
def __init__(self, sender, contentType, content, addContent, date):
self.sender = sender
self.contentType = contentType # text / photos / audio / gif / sticker / video
self.content = content # plain text or a media link
self.addContent = addContent # sometimes an additional text content comes (like in videos)
self.date = date # pretty formated
class Conversation():
def __init__(self, title, participants, messages, username):
self.title = title
self.participants = participants
self.messages = messages
self.username = username
self.generated_date = datetime.now()
# ---------------------------- JSON file ---------------------------------------
def loadJSONFile(file):
'''
Returns a json object from the json file
@param file: path to the file
'''
try:
with open(file, encoding="utf-8") as file:
data = file.read()
except FileNotFoundError:
print("ERROR: You have to specify a correct path for input folder")
sys.exit(42)
return json.loads(data)
def buildMessageList(messages, language, inputfolder, stickers):
'''
Returns the built list of messages correctly formatted in the chosen language
@param messages: the message dictionnary
@param language: the language (FR/EN)
'''
n = len(messages)
L = []
for i in range(n - 1, -1, -1): # in order to be sorted
sender = encodingCorrection(messages[i]["sender_name"])
addContent = "" # by default
content = []
# 7 types : photos, audio_files, sticker, gifs, videos, content (text), content (share)
if "content" in messages[i].keys() and \
not "videos" in messages[i].keys() and \
not "share" in messages[i].keys(): # content (text)
content.append(encodingCorrection(messages[i]["content"]))
contentType = "text"
elif "photos" in messages[i].keys(): # photos (path)
for photo in messages[i]["photos"]:
content.append(mediaManager(encodingCorrection(photo["uri"]), "photos", inputfolder, stickers))
contentType = "photos"
elif "audio_files" in messages[i].keys(): # audio_files (path)
for audio in messages[i]["audio_files"]:
content.append(mediaManager(encodingCorrection(audio["uri"]), "audio_files", inputfolder, stickers))
contentType = "audio"
elif "gifs" in messages[i].keys(): # gifs (path)
for gif in messages[i]["gifs"]:
content.append(mediaManager(encodingCorrection(gif["uri"]), "gifs", inputfolder, stickers))
contentType = "gif"
elif "videos" in messages[i].keys(): # videos (path)
if "content" in messages[i].keys(): # because sometimes videos come with a text content...
addContent = encodingCorrection(messages[i]["content"])
for video in messages[i]["videos"]:
content.append(mediaManager(encodingCorrection(video["uri"]), "videos", inputfolder, stickers))
contentType = "video"
elif "sticker" in messages[i].keys(): # sticker (path)
content.append(mediaManager(encodingCorrection(messages[i]["sticker"]["uri"]), "sticker", inputfolder, stickers))
contentType = "sticker"
elif "share" in messages[i].keys(): # content (share)
if "content" in messages[i].keys():
content.append(encodingCorrection(messages[i]["content"]))
combined_content = encodingCorrection(messages[i]["share"]["link"])
if "share_text" in messages[i]["share"].keys():
combined_content += " " + encodingCorrection(messages[i]["share"]["share_text"])
content.append(combined_content)
contentType = "text"
timestamp = messages[i]["timestamp_ms"]
date = ""
if language == "FR":
date = frenchDateFormat(timestamp)
elif language == "EN":
date = dateFormat(timestamp)
else:
raise Exception("Unknown language")
message = Message(sender, contentType, content, addContent, date)
L.append(message)
return L
def mediaManager(path, contentType, inputfolder, stickers):
'''
returns the correct path for a media file
'''
filename = os.path.basename(path)
filepath = ""
if contentType == "photos":
filepath = os.path.join(inputfolder, '..', 'photos', filename) # Go up one level
elif contentType == "audio_files":
filepath = os.path.join(inputfolder, '..', 'audio', filename) # Go up one level
elif contentType == "gifs":
filepath = os.path.join(inputfolder, '..', 'gifs', filename) # Go up one level
elif contentType == "videos":
filepath = os.path.join(inputfolder, '..', 'videos', filename) # Go up one level
elif contentType == "sticker" and stickers != '':
filepath = stickers + filename
return os.path.normpath(filepath)
# ------------------------------- Program --------------------------------------
def helpDisplay():
print("Basic usage: main.py -i <inputfolder> -o <htmlouputfile> [-s <stickerfolder>] -n <your_username> -l <FR/EN>")
print("")
print("Arguments:")
print("-i, --input <path>: the path to the folder containing your conversation (the JSON file must be named 'message_1.json')")
print("-o, --output <path>: the path to the HTML output file (created if it does not exist)")
print("-s, --stickers <path>: the path to the folder containing your stickers (optional)")
print("-n, --username <your_username>: your username in the conversation (ex: -n 'John Doe')")
print("-l, --lang <FR/EN>: the language to display dates and other elements")
print("-g, --log: save a log with the messages in [outputfile].log")
print("-h, --help: display this help")
print("")
def wrongArguments():
print("Wrong arguments: main.py -i <inputfolder> -o <htmlouputfile> [-s <stickerfolder>] -n <your_username> -l <FR/EN>")
print("Run main.py -h for more info")
def loadArguments(argv):
inputfolder = ''
outputfile = ''
username = 'NOBODY'
language = 'EN'
saveLog = False
stickers = ''
if len(argv) == 0:
wrongArguments()
sys.exit(2)
try:
opts, args = getopt.getopt(argv, "hi:o:n:l:gs:", ["help", "input=", "output=", "username=", "lang=", "log", "stickers="])
except getopt.GetoptError:
wrongArguments()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
helpDisplay()
sys.exit()
elif opt in ("-i", "--input"):
inputfolder = arg
elif opt in ("-o", "--output"):
outputfile = arg
elif opt in ("-n", "--username"):
username = arg
elif opt in ("-l", "--lang") and arg in ("FR", "EN"):
language = arg
elif opt in ("-g", "--log"):
saveLog = True
elif opt in ("-s", "--stcikers"):
stickers = arg
return (inputfolder, outputfile, username, language, saveLog, stickers)
def encodingCorrection(string):
return string.encode('latin1').decode('utf-8')
def full_path(path:str, create=False)->str:
assert path
path = os.path.abspath(
os.path.expanduser(
os.path.expandvars(path)))
if create:
os.makedirs(path, exist_ok=True)
return path
# ------------------------------ Main ------------------------------------------
def main(argv):
(inputfolder, outputfile, username, language, saveLog, stickers) = loadArguments(argv)
inputfolder = full_path(inputfolder)
outputfile = full_path(outputfile)
if inputfolder[-1] != '/':
inputfolder += '/'
if stickers != '' and stickers[-1] != '/':
stickers += '/'
# Debugging
print("Input folder:", inputfolder)
print("Ouput file:", outputfile)
print("Stickers folder:", stickers)
print("Your name:", username)
print("Your language:", language)
print("")
print("Parsing, this may take a few seconds...")
if os.path.isdir(inputfolder):
inputfolder = os.path.join(inputfolder, "message_1.json")
jsonData = loadJSONFile(inputfolder)
participants = jsonData["participants"]
for participant in participants:
participant = encodingCorrection(participant["name"])
title = encodingCorrection(jsonData["title"])
messages = buildMessageList(jsonData["messages"], language, inputfolder, stickers)
conversation = Conversation(title, participants, messages, username)
# HTML rendering
todaysTimestamp = ""
env = Environment(loader=FileSystemLoader('templates'))
if language == 'FR':
todaysTimestamp = frenchDateFormat(int(round(time.time() * 1000)))
template = env.get_template('FR/base.html')
elif language == 'EN':
todaysTimestamp = dateFormat(int(round(time.time() * 1000)))
template = env.get_template('EN/base.html')
else:
raise Exception("Unknown language")
htmlRender = template.render(conversation=conversation, date=todaysTimestamp)
try:
with open(outputfile, 'w', encoding="utf-8") as output:
output.write(htmlRender)
except FileNotFoundError:
print("ERROR: You have to specify a correct path for output folder")
sys.exit(42)
# Logs
if saveLog:
log = "JSON to HTML Messenger Parser Log\nGenerated on " + time.strftime('%c') +'\n\n'
for message in conversation.messages:
log += message.date + '\n' + message.sender + ': ' + message.content + '\n'
logLocation = os.path.splitext(outputfile)[0] + '.log'
with open(logLocation, 'w', encoding="utf-8") as logFile:
logFile.write(log)
print("Log successfully saved in", logLocation)
print("Conversation successfully parsed into HTML in", outputfile)
if __name__ == "__main__":
main(sys.argv[1:])