nitter/src/query.nim

108 lines
2.6 KiB
Nim
Raw Normal View History

2019-09-20 00:20:09 +00:00
import strutils, strformat, sequtils, tables, uri
import types
const
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(",")
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-08-06 15:41:06 +00:00
fromUser: @[name]
2019-07-04 09:55:19 +00:00
)
proc genQueryParam*(query: Query): string =
2020-05-20 05:06:45 +00:00
var
filters: seq[string]
param: string
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:
param &= &"from:{user} "
2019-08-06 15:41:06 +00:00
if i < query.fromUser.high:
param &= "OR "
if query.fromUser.len > 0 and query.kind in {posts, media}:
param &= "filter:self_threads OR-filter:replies "
if "nativeretweets" notin query.excludes:
param &= "include:nativeretweets "
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:
if e == "nativeretweets": 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:
if result.len > 0:
result &= " " & query.text
else:
result = query.text
2020-04-29 13:06:15 +00:00
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("&")