This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
split.jl
70 lines (53 loc) · 1.72 KB
/
split.jl
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
# This script splits the main history file to
# the portions of messages, updating the cursors
using JSON3
const messages_dir = joinpath(@__DIR__, "messages")
# For every channel
for dir in readdir(messages_dir, join=true)
# Prepare an empty object to store the messages
history = JSON3.Object()
# Prepare an empty object to store the portions
portion = JSON3.Object()
# Get the path to the main history file
main_file_path = joinpath(dir, "0.json")
# Read the history inside the main file
history = open(main_file_path, "r") do io
JSON3.read(io)
end
# Get the cursor
cursor = history[:cursor]
# Get the messages
messages = history[:messages]
# Get the number of messages
num = length(messages)
# Get the remainder
rem = mod(num, 100)
# Guarantee that there will be at least
# 50 messages in the main history file
if num > 100 && rem ≥ 50
# For every portion of 100 messages
for i in 1:fld(length(messages), 100)
portion = messages[1+100*(i-1):i*100]
# Spit it out to a separate file
open(joinpath(dir, "$(cursor+1).json"), "w") do io
print(
io,
"{\"cursor\": $(cursor), \"messages\": ",
JSON3.write(portion),
'}'
)
end
# And update the cursor
cursor += 1
end
# Update the main history file
open(main_file_path, "w") do io
print(
io,
"{\"cursor\": $(cursor), \"messages\": ",
JSON3.write(messages[end-rem+1:end]),
'}'
)
end
end
end