Add experimental user search parser

This commit is contained in:
Zed
2022-01-26 20:27:11 +01:00
parent 49a2fbb070
commit 4738ec3385
8 changed files with 74 additions and 33 deletions

View File

@@ -0,0 +1,28 @@
import std/[strutils, tables]
import jsony
import user, ../types/timeline
from ../../types import Result, User
proc getId(id: string): string {.inline.} =
let start = id.rfind("-")
if start < 0: return id
id[start + 1 ..< id.len]
proc parseUsers*(json: string; after=""): Result[User] =
result = Result[User](beginning: after.len == 0)
let raw = json.fromJson(Search)
if raw.timeline.instructions.len == 0:
return
for e in raw.timeline.instructions[0].addEntries.entries:
let id = e.entryId.getId
if e.entryId.startsWith("user"):
if id in raw.globalObjects.users:
result.content.add toUser raw.globalObjects.users[id]
elif e.entryId.startsWith("cursor"):
let cursor = e.content.operation.cursor
if cursor.cursorType == "Top":
result.top = cursor.value
elif cursor.cursorType == "Bottom":
result.bottom = cursor.value

View File

@@ -1,4 +1,4 @@
import std/[algorithm, unicode, re, strutils, strformat]
import std/[algorithm, unicode, re, strutils, strformat, options]
import jsony
import utils, slices
import ../types/user as userType
@@ -38,9 +38,11 @@ proc getBanner(user: RawUser): string =
if user.profileLinkColor.len > 0:
return '#' & user.profileLinkColor
if user.profileImageExtensions.mediaColor.r.ok.palette.len > 0:
let color = user.profileImageExtensions.mediaColor.r.ok.palette[0].rgb
return &"#{color.red:02x}{color.green:02x}{color.blue:02x}"
if user.profileImageExtensions.isSome:
let ext = get(user.profileImageExtensions)
if ext.mediaColor.r.ok.palette.len > 0:
let color = ext.mediaColor.r.ok.palette[0].rgb
return &"#{color.red:02x}{color.green:02x}{color.blue:02x}"
proc toUser*(raw: RawUser): User =
result = User(

View File

@@ -0,0 +1,23 @@
import std/tables
import user
type
Search* = object
globalObjects*: GlobalObjects
timeline*: Timeline
GlobalObjects = object
users*: Table[string, RawUser]
Timeline = object
instructions*: seq[Instructions]
Instructions = object
addEntries*: tuple[entries: seq[Entry]]
Entry = object
entryId*: string
content*: tuple[operation: Operation]
Operation = object
cursor*: tuple[value, cursorType: string]

View File

@@ -1,3 +1,4 @@
import options
import common
type
@@ -16,10 +17,10 @@ type
mediaCount*: int
verified*: bool
protected*: bool
profileLinkColor*: string
profileBannerUrl*: string
profileImageUrlHttps*: string
profileImageExtensions*: ImageExtensions
profileLinkColor*: string
profileImageExtensions*: Option[ImageExtensions]
pinnedTweetIdsStr*: seq[string]
Entities* = object