forked from zedeus/nitter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeline.nim
More file actions
165 lines (144 loc) · 5.36 KB
/
timeline.nim
File metadata and controls
165 lines (144 loc) · 5.36 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
# SPDX-License-Identifier: AGPL-3.0-only
import json, asyncdispatch, strutils, sequtils, uri, options, times
import options
import times
import jester, karax/vdom
import ".."/routes/[router_utils, timeline]
import ".."/[types, redis_cache, formatters, query, api]
# JSON formatting functions
proc formatUserAsJson*(user: User): JsonNode =
return %*{
"id": user.id,
"username": user.username,
"fullname": user.fullname,
"location": user.location,
"website": user.website,
"bio": user.bio,
"userPic": user.userPic,
"banner": user.banner,
"pinnedTweet": user.pinnedTweet,
"following": user.following,
"followers": user.followers,
"tweets": user.tweets,
"likes": user.likes,
"media": user.media,
"verifiedType": $user.verifiedType,
"protected": user.protected,
"suspended": user.suspended,
"joinDate": user.joinDate.toTime.toUnix()
}
proc formatTweetAsJson*(tweet: Tweet): JsonNode =
return %*{
"id": $tweet.id,
"threadId": $tweet.threadId,
"replyId": $tweet.replyId,
"user": formatUserAsJson(tweet.user),
"text": tweet.text,
"time": tweet.time.toTime.toUnix(),
"reply": tweet.reply,
"pinned": tweet.pinned,
"hasThread": tweet.hasThread,
"available": tweet.available,
"tombstone": tweet.tombstone,
"location": tweet.location,
"source": tweet.source,
"stats": %*{
"replies": tweet.stats.replies,
"retweets": tweet.stats.retweets,
"likes": tweet.stats.likes,
"quotes": tweet.stats.quotes
},
"retweet": if tweet.retweet.isSome: formatTweetAsJson(get(
tweet.retweet)) else: newJNull(),
"attribution": if tweet.attribution.isSome: formatUserAsJson(get(
tweet.attribution)) else: newJNull(),
"mediaTags": if tweet.mediaTags.len > 0: %tweet.mediaTags.map(
formatUserAsJson) else: newJNull(),
"quote": if tweet.quote.isSome: formatTweetAsJson(get(
tweet.quote)) else: newJNull(),
"card": if tweet.card.isSome: %*get(tweet.card) else: newJNull(),
"poll": if tweet.poll.isSome: %*get(tweet.poll) else: newJNull(),
"gif": if tweet.gif.isSome: %*get(tweet.gif) else: newJNull(),
"gifs": if tweet.gifs.len > 0: %tweet.gifs else: newJNull(),
"video": if tweet.video.isSome: %*get(tweet.video) else: newJNull(),
"photos": if tweet.photos.len > 0: %tweet.photos else: newJNull()
}
proc formatTimelineAsJson*(results: Timeline): JsonNode =
var retweets: seq[int64]
var timeline = newJArray()
for thread in results.content:
if thread.len == 1:
let tweet = thread[0]
let retweetId = if tweet.retweet.isSome: get(tweet.retweet).id else: 0
if retweetId in retweets or tweet.id in retweets:
continue
if retweetId != 0 and tweet.retweet.isSome:
retweets &= retweetId
timeline.add(formatTweetAsJson(tweet))
else:
var threadTimeline = newJArray()
for tweet in thread:
threadTimeline.add(formatTweetAsJson(tweet))
timeline.add(threadTimeline)
return %*{
"pagination": %*{
"beginning": results.beginning,
"top": results.top,
"bottom": results.bottom
},
"timeline": timeline
}
proc formatUserName*(username: string): JsonNode =
return %*{
"username": username
}
proc formatProfileAsJson*(profile: Profile): JsonNode =
return %*{
"user": formatUserAsJson(profile.user),
"photoRail": %profile.photoRail,
"pinned": if profile.pinned.isSome: formatTweetAsJson(get(
profile.pinned)) else: newJNull()
}
proc createJsonApiTimelineRouter*(cfg: Config) =
router jsonapi_timeline:
get "/api/i/user/@user_id":
cond @"user_id".len > 0
let username = await getCachedUsername(@"user_id")
if username.len > 0:
respJsonSuccess formatUserName(username)
else:
respJsonError("User not found", "not_found", Http404)
get "/api/@name/profile":
cond @"name" notin ["pic", "gif", "video", "search", "settings", "login",
"intent", "i"]
let
prefs = cookiePrefs()
names = getNames(@"name")
var query = request.getQuery("", @"name")
if names.len != 1:
query.fromUser = names
var profile = await fetchProfile("", query, skipRail = false)
if profile.user.username.len == 0: respJsonError("User not found", "not_found", Http404)
respJsonSuccess formatProfileAsJson(profile)
get "/api/@name/?@tab?/?":
cond '.' notin @"name"
cond @"name" notin ["pic", "gif", "video", "search", "settings", "login", "intent", "i"]
cond @"name".allCharsInSet({'a'..'z', 'A'..'Z', '0'..'9', '_', ','})
cond @"tab" in ["with_replies", "media", "search", ""]
let
prefs = cookiePrefs()
after = getCursor()
names = getNames(@"name")
var query = request.getQuery(@"tab", @"name")
if names.len != 1:
query.fromUser = names
if query.fromUser.len != 1:
var timeline = await getGraphTweetSearch(query, after)
if timeline.content.len == 0: respJsonError "No results found"
timeline.beginning = true
respJsonSuccess formatTimelineAsJson(timeline)
else:
var profile = await fetchProfile(after, query, skipRail = true)
if profile.tweets.content.len == 0: respJsonError("User not found", "not_found", Http404)
profile.tweets.beginning = true
respJsonSuccess formatTimelineAsJson(profile.tweets)