-
Notifications
You must be signed in to change notification settings - Fork 1
/
forscraper.nim
181 lines (153 loc) · 5.01 KB
/
forscraper.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
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
import std / [
asyncdispatch, httpclient, sequtils,
json, htmlparser, times, options,
strformat, strutils
]
import karax / [karaxdsl, vdom]
import types
type
ForumThreadId = distinct int
const
ThreadsUrl = "https://forum.nim-lang.org/threads.json"
PostsUrl = "https://forum.nim-lang.org/posts.json"
proc getPostContent(p: Post): string =
if p.history.len > 0:
p.history[^1].content
else:
p.info.content
proc getLastThreads(start = 0, count = 30): Future[seq[ForumThread]] {.async.} =
var client = newAsyncHttpClient()
var resp = await client.get(fmt"{ThreadsUrl}?start={start}&count={count}")
if resp.code != Http200:
client.close()
return
let body = parseJson(await resp.body).to(ThreadList)
client.close()
result = body.threads
proc getThreadInfo(id: ForumThreadId): Future[Option[PostList]] {.async.} =
var client = newAsyncHttpClient()
var resp = await client.get(fmt"{PostsUrl}?id={int(id)}")
if resp.code != Http200:
client.close()
return
result = some(parseJson(await resp.body).to(PostList))
client.close()
proc renderActivity*(activity: int64): string =
let currentTime = getTime()
let activityTime = fromUnix(activity)
let duration = currentTime - activityTime
if currentTime.local().year != activityTime.local().year:
activityTime.local().format("d MMM yyyy")
elif duration.inDays > 30 and duration.inDays < 300:
activityTime.local().format("d MMM")
elif duration.inDays != 0:
$duration.inDays & "d"
elif duration.inHours != 0:
$duration.inHours & "h"
elif duration.inMinutes != 0:
$duration.inMinutes & "m"
else:
$duration.inSeconds & "s"
proc makeThreadEntry(thr: ForumThread): VNode =
result = buildHtml():
tr:
td(class="thread-title"):
a(href="/t/" & $int(thr.id)):
text thr.topic
td(class="thread-title"):
text renderActivity(thr.activity)
td(class="thread-title"):
text renderActivity(thr.creation)
td(class="hide-sm views-text"):
text $thr.views
proc makeThreadsList(p: int, tl: seq[ForumThread]): VNode =
result = buildHtml():
table(id="threads-list", class="table"):
thead:
tr:
th: text "Topic"
th: text "Last activity"
th: text "Created"
th: text "Views"
tbody:
if p > 1:
tr(class = "load-more-separator"):
td(colspan = "6"):
a(href = "/p/" & $(p - 1)):
tdiv:
text "Go to the previous page"
for thr in tl:
makeThreadEntry(thr)
tr(class = "load-more-separator"):
td(colspan = "6"):
a(href = "/p/" & $(p + 1)):
tdiv:
text "Go to the next page"
proc makeMainPage(page: int, tl: seq[ForumThread]): string =
let vnode = buildHtml():
html:
head:
link(
rel="stylesheet",
href="https://forum.nim-lang.org/css/nimforum.css"
)
title: text "Test forum"
meta(name="viewport", content="width=device-width, initial-scale=1")
body:
section(class = "thread-list"):
makeThreadsList(page, tl)
result = $vnode
proc makePosts(pl: PostList): VNode =
result = buildHtml():
section(class = "container grid-xl"):
tdiv(id = "thread-title", class = "title"):
p(class = "title-text"): text pl.thread.topic
tdiv(class = "posts"):
for post in pl.posts:
tdiv(id = $post.id, class = "post"):
tdiv(class = "post-icon"):
figure(class = "post-avatar"):
img(src = post.author.avatarUrl, title = $post.author)
tdiv(class = "post-main"):
tdiv(class = "post-title"):
tdiv(class = "post-username"):
text $post.author
tdiv(class = "post-content"):
verbatim(getPostContent(post))
proc makeThreadPage(pl: PostList): string =
let vnode = buildHtml():
html:
head:
link(
rel="stylesheet",
href="https://forum.nim-lang.org/css/nimforum.css"
)
title: text fmt"{pl.thread.topic} - Nim forum"
# Google said that this makes the mobile experience better, and
# it truly seems so
meta(name="viewport", content="width=device-width, initial-scale=1")
body:
makePosts(pl)
result = $vnode
import jester
routes:
get "/":
let page = 1
let thrs = await getLastThreads(start = (page - 1) * 30)
resp makeMainPage(page, thrs)
get "/p/@page":
let page =
try: parseInt(@"page")
except ValueError: resp "Invalid page count"
let thrs = await getLastThreads(start = (page - 1) * 30)
resp makeMainPage(page, thrs)
get "/t/@id":
let id = try:
ForumThreadId(parseInt(@"id"))
except ValueError:
resp "Invalid thread id"
let thr = await getThreadInfo(id)
if thr.isSome():
resp makeThreadPage(thr.get())
else:
resp Http404, "Thread not found! It was either deleted or never existed in the first place!"