nitter/src/query.nim

111 lines
2.8 KiB
Nim
Raw Normal View History

2019-09-20 00:20:09 +00:00
import strutils, strformat, sequtils, tables, uri
import types
const
separators = @["AND", "OR"]
2019-09-17 19:01:44 +00:00
validFilters* = @[
2019-08-11 19:26:44 +00:00
"media", "images", "twimg", "videos",
2019-07-04 09:55:19 +00:00
"native_video", "consumer_video", "pro_video",
"links", "news", "quote", "mentions",
"replies", "retweets", "nativeretweets",
"verified", "safe"
]
template `@`(param: string): untyped =
if param in pms: pms[param]
else: ""
proc initQuery*(pms: Table[string, string]; name=""): Query =
result = Query(
kind: parseEnum[QueryKind](@"f", tweets),
text: @"q",
filters: validFilters.filterIt("f-" & it in pms),
excludes: validFilters.filterIt("e-" & it in pms),
2019-09-19 20:11:38 +00:00
since: @"since",
2019-09-19 21:36:21 +00:00
until: @"until",
near: @"near"
)
2019-09-18 23:11:35 +00:00
if name.len > 0:
result.fromUser = name.split(",")
if @"e-nativeretweets".len == 0:
result.includes.add "nativeretweets"
proc getMediaQuery*(name: string): Query =
Query(
2019-07-11 17:22:23 +00:00
kind: media,
2019-07-04 09:55:19 +00:00
filters: @["twimg", "native_video"],
2019-08-06 15:41:06 +00:00
fromUser: @[name],
2019-07-04 09:55:19 +00:00
sep: "OR"
)
proc getReplyQuery*(name: string): Query =
2019-07-04 09:55:19 +00:00
Query(
2019-07-11 17:22:23 +00:00
kind: replies,
2019-07-04 09:55:19 +00:00
includes: @["nativeretweets"],
2019-08-06 15:41:06 +00:00
fromUser: @[name]
2019-07-04 09:55:19 +00:00
)
2020-04-29 13:06:15 +00:00
proc genQueryParam*(query: Query; rewriteReplies=true): string =
var filters: seq[string]
var param: string
2020-04-29 13:05:24 +00:00
let userSearch = query.fromUser.len > 0
if query.kind == users:
2019-09-13 20:24:58 +00:00
return query.text
2019-08-06 15:41:06 +00:00
for i, user in query.fromUser:
if rewriteReplies:
2020-04-29 13:06:15 +00:00
param &= &"(from:{user}(to:{user} OR -filter:replies)) "
else:
param &= &"from:{user} "
2019-08-06 15:41:06 +00:00
if i < query.fromUser.high:
param &= "OR "
2019-07-04 09:55:19 +00:00
for f in query.filters:
filters.add "filter:" & f
2019-07-04 09:55:19 +00:00
for e in query.excludes:
2020-04-29 13:05:24 +00:00
if userSearch and rewriteReplies and e == "replies": continue
filters.add "-filter:" & e
2019-09-20 01:35:27 +00:00
for i in query.includes:
filters.add "include:" & i
2019-09-13 11:20:08 +00:00
result = strip(param & filters.join(&" {query.sep} "))
2019-09-19 20:11:38 +00:00
if query.since.len > 0:
result &= " since:" & query.since
if query.until.len > 0:
result &= " until:" & query.until
2019-09-19 21:36:21 +00:00
if query.near.len > 0:
result &= &" near:\"{query.near}\" within:15mi"
2019-09-13 11:20:08 +00:00
if query.text.len > 0:
result &= " " & query.text
2020-04-29 13:06:15 +00:00
if userSearch and rewriteReplies and result.len >= 500:
return genQueryParam(query, rewriteReplies=false)
2019-09-20 20:56:27 +00:00
proc genQueryUrl*(query: Query): string =
if query.kind notin {tweets, users}: return
var params = @[&"f={query.kind}"]
2019-09-13 11:20:08 +00:00
if query.text.len > 0:
params.add "q=" & encodeUrl(query.text)
for f in query.filters:
params.add "f-" & f & "=on"
for e in query.excludes:
params.add "e-" & e & "=on"
2019-09-20 23:11:03 +00:00
for i in query.includes.filterIt(it != "nativeretweets"):
params.add "i-" & i & "=on"
2019-09-19 20:11:38 +00:00
if query.since.len > 0:
params.add "since=" & query.since
if query.until.len > 0:
params.add "until=" & query.until
2019-09-19 21:36:21 +00:00
if query.near.len > 0:
params.add "near=" & query.near
2019-09-19 20:11:38 +00:00
if params.len > 0:
2019-09-13 20:24:58 +00:00
result &= params.join("&")