-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmastoarchive.nim
92 lines (74 loc) · 2.24 KB
/
mastoarchive.nim
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
# std imports
import os
import std/json
import std/options
import std/random
import std/strutils
# external libs
import nigui
# own imports
import masto_archive/gui
import masto_archive/parseargs
import masto_archive/tootdata
proc assertInit(success: bool, errMsg: string) =
if not success:
echo errMsg
quit QuitFailure
type
Outbox = object
totalToots: int
toots: JsonNode
proc summary(outboxJson: JsonNode): Outbox =
Outbox(
totalToots: outboxJson["totalItems"].num.int,
toots: outboxJson["orderedItems"])
proc at(toots: JsonNode, idx: int): Toot =
let node = toots[idx]
let data = node.to(TootData)
let objNode = node["object"]
let t = data.`type`
let obj =
case t:
of "Create": TootObject(kind: tkToot, toot: objNode.to(TootObjectData))
of "Announce": TootObject(kind: tkBoost, boostedUrl: objNode.str)
else: raise newException(ValueError, "toot type unknown: " & t)
Toot(published: data.published, obj: obj)
let
usageText = "usage: " & paramStr(0) &
" [rand|loop|toot <tootNum>|tootrange <from>-<to>|gui] " &
"<path to archive dir>"
# ===
randomize()
let progArgs = parseArgs(paramStr, paramCount())
assertInit(progArgs.isSome(), usageText)
let archiveDir = progArgs.get().archiveDir
assertInit(dirExists(archiveDir),
"error: \"" & archiveDir & "\" is not a directory")
let outbox = readFile(archiveDir & "/outbox.json").parseJson.summary
let openMode = progArgs.get().openMode
case openMode.kind:
of omRand:
let tootIdx = rand(outbox.totalToots - 1)
echo "Reading toot number ", tootIdx
echo outbox.toots.at(tootIdx).toText
of omLoop:
while true:
echo "Which toot do you want to see? "
let tootIdx = stdin.readLine.parseInt
echo outbox.toots.at(tootIdx).toText
echo "======"
of omToot:
echo outbox.toots.at(openMode.tootIdx).toText
of omTootRange:
for idx in openMode.tootIdxFrom..openMode.tootIdxTo:
echo outbox.toots.at(idx).toText
echo "======"
of omGui:
app.init()
let policy = MastoArchiveWindowPolicy(
randomTootIdx: proc(): int = rand(outbox.totalToots - 1),
tootText: proc(i: int): string = outbox.toots.at(i).toText
)
var window = mastoArchiveWindow(policy)
window.show()
app.run()