Refactoring

This commit is contained in:
Zed 2019-10-23 09:03:15 +02:00
parent ffce6e21ab
commit 5979d7f588
5 changed files with 35 additions and 37 deletions

View file

@ -4,7 +4,7 @@ import sequtils, strutils, json, uri
import ".."/[types, parser, parserutils, query] import ".."/[types, parser, parserutils, query]
import utils, consts, timeline, search import utils, consts, timeline, search
proc getListTimeline*(username, list, agent, after: string; media=true): Future[Timeline] {.async.} = proc getListTimeline*(username, list, after, agent: string; media=true): Future[Timeline] {.async.} =
let url = base / (listUrl % [username, list]) let url = base / (listUrl % [username, list])
var params = toSeq({ var params = toSeq({
@ -23,20 +23,7 @@ proc getListTimeline*(username, list, agent, after: string; media=true): Future[
result.minId = getLastId(result) result.minId = getLastId(result)
proc getListMembers*(username, list, agent: string): Future[Result[Profile]] {.async.} = proc getListMembersSearch(username, list, after, agent: string): Future[Result[Profile]] {.async.} =
let
url = base / (listMembersUrl % [username, list])
html = await fetchHtml(url, genHeaders(agent, url))
result = Result[Profile](
minId: html.selectAttr(".stream-container", "data-min-position"),
hasMore: html.select(".has-more-items") != nil,
beginning: true,
query: Query(kind: userList),
content: html.selectAll(".account").map(parseListProfile)
)
proc getListMembersSearch*(username, list, agent, after: string): Future[Result[Profile]] {.async.} =
let let
referer = base / (listMembersUrl % [username, list]) referer = base / (listMembersUrl % [username, list])
url = referer / "timeline" url = referer / "timeline"
@ -60,3 +47,19 @@ proc getListMembersSearch*(username, list, agent, after: string): Future[Result[
result.hasMore = html != "\n" result.hasMore = html != "\n"
for p in parseHtml(html).selectAll(".account"): for p in parseHtml(html).selectAll(".account"):
result.content.add parseListProfile(p) result.content.add parseListProfile(p)
proc getListMembers*(username, list, after, agent: string): Future[Result[Profile]] {.async.} =
if after.len > 0:
return await getListMembersSearch(username, list, after, agent)
let
url = base / (listMembersUrl % [username, list])
html = await fetchHtml(url, genHeaders(agent, url))
result = Result[Profile](
minId: html.selectAttr(".stream-container", "data-min-position"),
hasMore: html.select(".has-more-items") != nil,
beginning: true,
query: Query(kind: userList),
content: html.selectAll(".account").map(parseListProfile)
)

View file

@ -23,7 +23,7 @@ proc finishTimeline*(json: JsonNode; query: Query; after, agent: string;
if media: await getMedia(timeline, agent) if media: await getMedia(timeline, agent)
result.content = timeline.content result.content = timeline.content
proc getProfileAndTimeline*(username, agent, after: string; media=true): Future[(Profile, Timeline)] {.async.} = proc getProfileAndTimeline*(username, after, agent: string; media=true): Future[(Profile, Timeline)] {.async.} =
var url = base / username var url = base / username
if after.len > 0: if after.len > 0:
url = url ? {"max_position": after} url = url ? {"max_position": after}
@ -54,7 +54,6 @@ proc getTimeline*(username, after, agent: string; media=true): Future[Timeline]
result = await finishTimeline(json, Query(), after, agent, media) result = await finishTimeline(json, Query(), after, agent, media)
proc getMediaTimeline*(username, after, agent: string; media=true): Future[Timeline] {.async.} = proc getMediaTimeline*(username, after, agent: string; media=true): Future[Timeline] {.async.} =
echo "mediaTimeline"
var params = toSeq({ var params = toSeq({
"include_available_features": "1", "include_available_features": "1",
"include_entities": "1", "include_entities": "1",

View file

@ -18,17 +18,13 @@ proc createListRouter*(cfg: Config) =
get "/@name/lists/@list": get "/@name/lists/@list":
cond '.' notin @"name" cond '.' notin @"name"
let let
list = await getListTimeline(@"name", @"list", getAgent(), @"max_position") list = await getListTimeline(@"name", @"list", @"max_position", getAgent())
tweets = renderTimelineTweets(list, cookiePrefs(), request.path) tweets = renderTimelineTweets(list, cookiePrefs(), request.path)
respList list, tweets respList list, tweets
get "/@name/lists/@list/members": get "/@name/lists/@list/members":
cond '.' notin @"name" cond '.' notin @"name"
let list = let
if @"max_position".len == 0: list = await getListMembers(@"name", @"list", @"max_position", getAgent())
await getListMembers(@"name", @"list", getAgent()) users = renderTimelineUsers(list, cookiePrefs(), request.path)
else:
await getListMembersSearch(@"name", @"list", getAgent(), @"max_position")
let users = renderTimelineUsers(list, cookiePrefs(), request.path)
respList list, users respList list, users

View file

@ -51,5 +51,5 @@ proc createRssRouter*(cfg: Config) =
get "/@name/lists/@list/rss": get "/@name/lists/@list/rss":
cond '.' notin @"name" cond '.' notin @"name"
let list = await getListTimeline(@"name", @"list", getAgent(), "", media=false) let list = await getListTimeline(@"name", @"list", "", getAgent(), media=false)
respRss(renderListRss(list.content, @"name", @"list", cfg.hostname)) respRss(renderListRss(list.content, @"name", @"list", cfg.hostname))

View file

@ -24,7 +24,7 @@ proc fetchSingleTimeline*(name, after, agent: string; query: Query;
if cachedProfile.isSome: if cachedProfile.isSome:
timeline = await getTimeline(name, after, agent, media) timeline = await getTimeline(name, after, agent, media)
else: else:
(profile, timeline) = await getProfileAndTimeline(name, agent, after, media) (profile, timeline) = await getProfileAndTimeline(name, after, agent, media)
cache(profile) cache(profile)
else: else:
var timelineFut = var timelineFut =
@ -59,21 +59,21 @@ proc showTimeline*(request: Request; query: Query; cfg: Config; rss: string): Fu
after = request.get("max_position") after = request.get("max_position")
names = name.strip(chars={'/'}).split(",").filterIt(it.len > 0) names = name.strip(chars={'/'}).split(",").filterIt(it.len > 0)
if names.len == 1: if names.len != 1:
let
rail = getPhotoRail(names[0], agent, skip=(query.kind == media))
(p, t) = await fetchSingleTimeline(names[0], after, agent, query)
r = await rail
if p.username.len == 0: return
let pHtml = renderProfile(p, t, r, prefs, getPath())
return renderMain(pHtml, request, cfg, pageTitle(p), pageDesc(p),
rss=rss, images = @[p.getUserpic("_200x200")])
else:
let let
timeline = await fetchMultiTimeline(names, after, agent, query) timeline = await fetchMultiTimeline(names, after, agent, query)
html = renderTweetSearch(timeline, prefs, getPath()) html = renderTweetSearch(timeline, prefs, getPath())
return renderMain(html, request, cfg, "Multi") return renderMain(html, request, cfg, "Multi")
let
rail = getPhotoRail(names[0], agent, skip=(query.kind == media))
(p, t) = await fetchSingleTimeline(names[0], after, agent, query)
r = await rail
if p.username.len == 0: return
let pHtml = renderProfile(p, t, r, prefs, getPath())
return renderMain(pHtml, request, cfg, pageTitle(p), pageDesc(p),
rss=rss, images = @[p.getUserpic("_200x200")])
template respTimeline*(timeline: typed) = template respTimeline*(timeline: typed) =
if timeline.len == 0: if timeline.len == 0:
resp Http404, showError("User \"" & @"name" & "\" not found", cfg) resp Http404, showError("User \"" & @"name" & "\" not found", cfg)