Options cleanup

This commit is contained in:
Zed 2019-09-18 20:54:07 +02:00
parent 4c748b61a5
commit 4971491dfe
8 changed files with 28 additions and 28 deletions

View File

@ -89,10 +89,10 @@ proc getVideoFetch(tweet: Tweet; agent, token: string) {.async.} =
return return
if tweet.card.isNone: if tweet.card.isNone:
tweet.video = some(parseVideo(json, tweet.id)) tweet.video = some parseVideo(json, tweet.id)
else: else:
get(tweet.card).video = some(parseVideo(json, tweet.id)) get(tweet.card).video = some parseVideo(json, tweet.id)
tweet.video = none(Video) tweet.video = none Video
tokenUses.inc tokenUses.inc
proc getVideoVar(tweet: Tweet): var Option[Video] = proc getVideoVar(tweet: Tweet): var Option[Video] =
@ -104,7 +104,7 @@ proc getVideoVar(tweet: Tweet): var Option[Video] =
proc getVideo*(tweet: Tweet; agent, token: string; force=false) {.async.} = proc getVideo*(tweet: Tweet; agent, token: string; force=false) {.async.} =
withCustomDb("cache.db", "", "", ""): withCustomDb("cache.db", "", "", ""):
try: try:
getVideoVar(tweet) = some(Video.getOne("videoId = ?", tweet.id)) getVideoVar(tweet) = some Video.getOne("videoId = ?", tweet.id)
except KeyError: except KeyError:
await getVideoFetch(tweet, agent, token) await getVideoFetch(tweet, agent, token)
var video = getVideoVar(tweet) var video = getVideoVar(tweet)
@ -126,7 +126,7 @@ proc getPoll*(tweet: Tweet; agent: string) {.async.} =
let html = await fetchHtml(url, headers) let html = await fetchHtml(url, headers)
if html == nil: return if html == nil: return
tweet.poll = some(parsePoll(html)) tweet.poll = some parsePoll(html)
proc getCard*(tweet: Tweet; agent: string) {.async.} = proc getCard*(tweet: Tweet; agent: string) {.async.} =
if tweet.card.isNone(): return if tweet.card.isNone(): return

View File

@ -9,7 +9,7 @@ proc getResult[T](json: JsonNode; query: Query; after: string): Result[T] =
hasMore: json["has_more_items"].to(bool), hasMore: json["has_more_items"].to(bool),
maxId: json.getOrDefault("max_position").getStr(""), maxId: json.getOrDefault("max_position").getStr(""),
minId: json.getOrDefault("min_position").getStr("").cleanPos(), minId: json.getOrDefault("min_position").getStr("").cleanPos(),
query: query.some, query: some query,
beginning: after.len == 0 beginning: after.len == 0
) )
@ -49,7 +49,7 @@ proc getSearch*[T](query: Query; after, agent: string): Future[Result[T]] {.asyn
let html = parseHtml(json["items_html"].to(string)) let html = parseHtml(json["items_html"].to(string))
when T is Tweet: when T is Tweet:
result = await finishTimeline(json, some(query), after, agent) result = await finishTimeline(json, some query, after, agent)
elif T is Profile: elif T is Profile:
result.hasMore = json["items_html"].to(string) != "\n" result.hasMore = json["items_html"].to(string) != "\n"
for p in html.selectAll(".js-stream-item"): for p in html.selectAll(".js-stream-item"):

View File

@ -49,7 +49,7 @@ proc getTimeline*(username, after, agent: string): Future[Timeline] {.async.} =
params.add {"max_position": after} params.add {"max_position": after}
let json = await fetchJson(base / (timelineUrl % username) ? params, headers) let json = await fetchJson(base / (timelineUrl % username) ? params, headers)
result = await finishTimeline(json, none(Query), after, agent) result = await finishTimeline(json, none Query, after, agent)
proc getProfileAndTimeline*(username, agent, after: string): Future[(Profile, Timeline)] {.async.} = proc getProfileAndTimeline*(username, agent, after: string): Future[(Profile, Timeline)] {.async.} =
let headers = newHttpHeaders({ let headers = newHttpHeaders({

View File

@ -29,9 +29,9 @@ proc hasCachedProfile*(username: string): Option[Profile] =
try: try:
let p = Profile.getOne("lower(username) = ?", toLower(username)) let p = Profile.getOne("lower(username) = ?", toLower(username))
doAssert not p.isOutdated doAssert not p.isOutdated
result = some(p) result = some p
except AssertionError, KeyError: except AssertionError, KeyError:
result = none(Profile) result = none Profile
proc getCachedProfile*(username, agent: string; force=false): Future[Profile] {.async.} = proc getCachedProfile*(username, agent: string; force=false): Future[Profile] {.async.} =
withDb: withDb:

View File

@ -104,20 +104,20 @@ proc parseTweet*(node: XmlNode): Tweet =
let by = tweet.selectText(".js-retweet-text > a > b") let by = tweet.selectText(".js-retweet-text > a > b")
if by.len > 0: if by.len > 0:
result.retweet = some(Retweet( result.retweet = some Retweet(
by: stripText(by), by: stripText(by),
id: tweet.attr("data-retweet-id") id: tweet.attr("data-retweet-id")
)) )
let quote = tweet.select(".QuoteTweet-innerContainer") let quote = tweet.select(".QuoteTweet-innerContainer")
if quote != nil: if quote != nil:
result.quote = some(parseQuote(quote)) result.quote = some parseQuote(quote)
let tombstone = tweet.select(".Tombstone") let tombstone = tweet.select(".Tombstone")
if tombstone != nil: if tombstone != nil:
if "unavailable" in tombstone.innerText(): if "unavailable" in tombstone.innerText():
let quote = Quote(tombstone: getTombstone(node.selectText(".Tombstone-label"))) let quote = Quote(tombstone: getTombstone(node.selectText(".Tombstone-label")))
result.quote = some(quote) result.quote = some quote
proc parseThread*(nodes: XmlNode): Thread = proc parseThread*(nodes: XmlNode): Thread =
if nodes == nil: return if nodes == nil: return
@ -234,7 +234,7 @@ proc parseCard*(card: var Card; node: XmlNode) =
let image = node.select(".tcu-imageWrapper img") let image = node.select(".tcu-imageWrapper img")
if image != nil: if image != nil:
# workaround for issue 11713 # workaround for issue 11713
card.image = some(image.attr("data-src").replace("gname", "g&name")) card.image = some image.attr("data-src").replace("gname", "g&name")
if card.kind == liveEvent: if card.kind == liveEvent:
card.text = card.title card.text = card.title

View File

@ -180,9 +180,9 @@ proc getTweetMedia*(tweet: Tweet; node: XmlNode) =
if player == nil: return if player == nil: return
if "gif" in player.attr("class"): if "gif" in player.attr("class"):
tweet.gif = some(getGif(player.select(".PlayableMedia-player"))) tweet.gif = some getGif(player.select(".PlayableMedia-player"))
elif "video" in player.attr("class"): elif "video" in player.attr("class"):
tweet.video = some(Video()) tweet.video = some Video()
proc getQuoteMedia*(quote: var Quote; node: XmlNode) = proc getQuoteMedia*(quote: var Quote; node: XmlNode) =
if node.select(".QuoteTweet--sensitive") != nil: if node.select(".QuoteTweet--sensitive") != nil:
@ -209,7 +209,7 @@ proc getTweetCard*(tweet: Tweet; node: XmlNode) =
cardType = cardType.split(":")[^1] cardType = cardType.split(":")[^1]
if "poll" in cardType: if "poll" in cardType:
tweet.poll = some(Poll()) tweet.poll = some Poll()
return return
let cardDiv = node.select(".card2 > .js-macaw-cards-iframe-container") let cardDiv = node.select(".card2 > .js-macaw-cards-iframe-container")
@ -230,7 +230,7 @@ proc getTweetCard*(tweet: Tweet; node: XmlNode) =
if n.attr("href") == cardUrl: if n.attr("href") == cardUrl:
card.url = n.attr("data-expanded-url") card.url = n.attr("data-expanded-url")
tweet.card = some(card) tweet.card = some card
proc getMoreReplies*(node: XmlNode): int = proc getMoreReplies*(node: XmlNode): int =
let text = node.innerText().strip() let text = node.innerText().strip()

View File

@ -21,12 +21,12 @@ proc createRssRouter*(cfg: Config) =
router rss: router rss:
get "/@name/rss": get "/@name/rss":
cond '.' notin @"name" cond '.' notin @"name"
respRss(await showRss(@"name", none(Query))) respRss(await showRss(@"name", none Query))
get "/@name/replies/rss": get "/@name/replies/rss":
cond '.' notin @"name" cond '.' notin @"name"
respRss(await showRss(@"name", some(getReplyQuery(@"name")))) respRss(await showRss(@"name", some getReplyQuery(@"name")))
get "/@name/media/rss": get "/@name/media/rss":
cond '.' notin @"name" cond '.' notin @"name"
respRss(await showRss(@"name", some(getMediaQuery(@"name")))) respRss(await showRss(@"name", some getMediaQuery(@"name")))

View File

@ -47,7 +47,7 @@ proc fetchMultiTimeline*(names: seq[string]; after, agent: string;
if q.isSome: if q.isSome:
get(q).fromUser = names get(q).fromUser = names
else: else:
q = some(Query(kind: multi, fromUser: names, excludes: @["replies"])) q = some Query(kind: multi, fromUser: names, excludes: @["replies"])
return await getSearch[Tweet](get(q), after, agent) return await getSearch[Tweet](get(q), after, agent)
@ -79,25 +79,25 @@ proc createTimelineRouter*(cfg: Config) =
get "/@name/?": get "/@name/?":
cond '.' notin @"name" cond '.' notin @"name"
let rss = "/$1/rss" % @"name" let rss = "/$1/rss" % @"name"
respTimeline(await showTimeline(@"name", @"after", none(Query), cookiePrefs(), respTimeline(await showTimeline(@"name", @"after", none Query, cookiePrefs(),
getPath(), cfg.title, rss)) getPath(), cfg.title, rss))
get "/@name/search": get "/@name/search":
cond '.' notin @"name" cond '.' notin @"name"
let query = initQuery(@"filter", @"include", @"not", @"sep", @"text", @"name") let query = some initQuery(@"filter", @"include", @"not", @"sep", @"text", @"name")
respTimeline(await showTimeline(@"name", @"after", some(query), respTimeline(await showTimeline(@"name", @"after", query,
cookiePrefs(), getPath(), cfg.title, "")) cookiePrefs(), getPath(), cfg.title, ""))
get "/@name/replies": get "/@name/replies":
cond '.' notin @"name" cond '.' notin @"name"
let rss = "/$1/replies/rss" % @"name" let rss = "/$1/replies/rss" % @"name"
respTimeline(await showTimeline(@"name", @"after", some(getReplyQuery(@"name")), respTimeline(await showTimeline(@"name", @"after", some getReplyQuery(@"name"),
cookiePrefs(), getPath(), cfg.title, rss)) cookiePrefs(), getPath(), cfg.title, rss))
get "/@name/media": get "/@name/media":
cond '.' notin @"name" cond '.' notin @"name"
let rss = "/$1/media/rss" % @"name" let rss = "/$1/media/rss" % @"name"
respTimeline(await showTimeline(@"name", @"after", some(getMediaQuery(@"name")), respTimeline(await showTimeline(@"name", @"after", some getMediaQuery(@"name"),
cookiePrefs(), getPath(), cfg.title, rss)) cookiePrefs(), getPath(), cfg.title, rss))
get "/@name/status/@id": get "/@name/status/@id":