nitter/src/experimental/parser/user.nim

72 lines
2 KiB
Nim
Raw Normal View History

2022-01-16 05:00:11 +00:00
import std/[algorithm, unicode, re, strutils]
import jsony
import utils, slices
import ../types/user as userType
from ../../types import User, Error
2022-01-16 05:00:11 +00:00
let
unRegex = re"(^|[^A-z0-9-_./?])@([A-z0-9_]{1,15})"
unReplace = "$1<a href=\"/$2\">@$2</a>"
htRegex = re"(^|[^\w-_./?])([#$])([\w_]+)"
htReplace = "$1<a href=\"/search?q=%23$3\">$2$3</a>"
proc expandUserEntities(user: var User; raw: RawUser) =
2022-01-16 05:00:11 +00:00
let
orig = user.bio.toRunes
ent = raw.entities
2022-01-16 05:00:11 +00:00
if ent.url.urls.len > 0:
user.website = ent.url.urls[0].expandedUrl
2022-01-16 05:00:11 +00:00
var replacements = newSeq[ReplaceSlice]()
for u in ent.description.urls:
replacements.extractUrls(u, orig.high)
replacements.dedupSlices
replacements.sort(cmp)
user.bio = orig.replacedWith(replacements, 0 .. orig.len)
.replacef(unRegex, unReplace)
.replacef(htRegex, htReplace)
2022-01-16 05:00:11 +00:00
proc getBanner(user: RawUser): string =
2022-01-16 05:00:11 +00:00
if user.profileBannerUrl.len > 0:
return user.profileBannerUrl & "/1500x500"
if user.profileLinkColor.len > 0:
return '#' & user.profileLinkColor
2022-01-26 16:24:03 +00:00
proc toUser*(raw: RawUser): User =
result = User(
id: raw.idStr,
username: raw.screenName,
fullname: raw.name,
location: raw.location,
bio: raw.description,
following: raw.friendsCount,
followers: raw.followersCount,
tweets: raw.statusesCount,
likes: raw.favouritesCount,
media: raw.mediaCount,
verified: raw.verified,
protected: raw.protected,
joinDate: parseTwitterDate(raw.createdAt),
banner: getBanner(raw),
userPic: getImageUrl(raw.profileImageUrlHttps).replace("_normal", "")
)
if raw.pinnedTweetIdsStr.len > 0:
result.pinnedTweet = parseBiggestInt(raw.pinnedTweetIdsStr[0])
result.expandUserEntities(raw)
proc parseUser*(json: string; username=""): User =
2022-01-16 05:00:11 +00:00
handleErrors:
2022-01-16 17:28:40 +00:00
case error.code
of suspended: return User(username: username, suspended: true)
2022-01-16 05:00:11 +00:00
of userNotFound: return
else: echo "[error - parseUser]: ", error
2022-01-26 16:24:03 +00:00
result = toUser json.fromJson(RawUser)