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
if tweet.card.isNone:
tweet.video = some(parseVideo(json, tweet.id))
tweet.video = some parseVideo(json, tweet.id)
else:
get(tweet.card).video = some(parseVideo(json, tweet.id))
tweet.video = none(Video)
get(tweet.card).video = some parseVideo(json, tweet.id)
tweet.video = none Video
tokenUses.inc
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.} =
withCustomDb("cache.db", "", "", ""):
try:
getVideoVar(tweet) = some(Video.getOne("videoId = ?", tweet.id))
getVideoVar(tweet) = some Video.getOne("videoId = ?", tweet.id)
except KeyError:
await getVideoFetch(tweet, agent, token)
var video = getVideoVar(tweet)
@ -126,7 +126,7 @@ proc getPoll*(tweet: Tweet; agent: string) {.async.} =
let html = await fetchHtml(url, headers)
if html == nil: return
tweet.poll = some(parsePoll(html))
tweet.poll = some parsePoll(html)
proc getCard*(tweet: Tweet; agent: string) {.async.} =
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),
maxId: json.getOrDefault("max_position").getStr(""),
minId: json.getOrDefault("min_position").getStr("").cleanPos(),
query: query.some,
query: some query,
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))
when T is Tweet:
result = await finishTimeline(json, some(query), after, agent)
result = await finishTimeline(json, some query, after, agent)
elif T is Profile:
result.hasMore = json["items_html"].to(string) != "\n"
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}
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.} =
let headers = newHttpHeaders({

View file

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

View file

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

View file

@ -180,9 +180,9 @@ proc getTweetMedia*(tweet: Tweet; node: XmlNode) =
if player == nil: return
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"):
tweet.video = some(Video())
tweet.video = some Video()
proc getQuoteMedia*(quote: var Quote; node: XmlNode) =
if node.select(".QuoteTweet--sensitive") != nil:
@ -209,7 +209,7 @@ proc getTweetCard*(tweet: Tweet; node: XmlNode) =
cardType = cardType.split(":")[^1]
if "poll" in cardType:
tweet.poll = some(Poll())
tweet.poll = some Poll()
return
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:
card.url = n.attr("data-expanded-url")
tweet.card = some(card)
tweet.card = some card
proc getMoreReplies*(node: XmlNode): int =
let text = node.innerText().strip()

View file

@ -21,12 +21,12 @@ proc createRssRouter*(cfg: Config) =
router rss:
get "/@name/rss":
cond '.' notin @"name"
respRss(await showRss(@"name", none(Query)))
respRss(await showRss(@"name", none Query))
get "/@name/replies/rss":
cond '.' notin @"name"
respRss(await showRss(@"name", some(getReplyQuery(@"name"))))
respRss(await showRss(@"name", some getReplyQuery(@"name")))
get "/@name/media/rss":
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:
get(q).fromUser = names
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)
@ -79,25 +79,25 @@ proc createTimelineRouter*(cfg: Config) =
get "/@name/?":
cond '.' notin @"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))
get "/@name/search":
cond '.' notin @"name"
let query = initQuery(@"filter", @"include", @"not", @"sep", @"text", @"name")
respTimeline(await showTimeline(@"name", @"after", some(query),
let query = some initQuery(@"filter", @"include", @"not", @"sep", @"text", @"name")
respTimeline(await showTimeline(@"name", @"after", query,
cookiePrefs(), getPath(), cfg.title, ""))
get "/@name/replies":
cond '.' notin @"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))
get "/@name/media":
cond '.' notin @"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))
get "/@name/status/@id":