nitter/src/query.nim

111 lines
2.7 KiB
Nim
Raw Normal View History

2021-12-27 02:37:38 +01:00
# SPDX-License-Identifier: AGPL-3.0-only
2019-09-20 02:20:09 +02:00
import strutils, strformat, sequtils, tables, uri
import types
const
2019-09-17 21:01:44 +02:00
validFilters* = @[
2019-08-11 21:26:44 +02:00
"media", "images", "twimg", "videos",
2019-07-04 11:55:19 +02:00
"native_video", "consumer_video", "pro_video",
"links", "news", "quote", "mentions",
"replies", "retweets", "nativeretweets",
"verified", "safe"
]
emptyQuery* = "include:nativeretweets"
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 22:11:38 +02:00
since: @"since",
2019-09-19 23:36:21 +02:00
until: @"until",
near: @"near"
)
2019-09-19 01:11:35 +02:00
if name.len > 0:
result.fromUser = name.split(",")
proc getMediaQuery*(name: string): Query =
Query(
2019-07-11 19:22:23 +02:00
kind: media,
2019-07-04 11:55:19 +02:00
filters: @["twimg", "native_video"],
2019-08-06 17:41:06 +02:00
fromUser: @[name],
2019-07-04 11:55:19 +02:00
sep: "OR"
)
proc getReplyQuery*(name: string): Query =
2019-07-04 11:55:19 +02:00
Query(
2019-07-11 19:22:23 +02:00
kind: replies,
2019-08-06 17:41:06 +02:00
fromUser: @[name]
2019-07-04 11:55:19 +02:00
)
proc genQueryParam*(query: Query): string =
2020-05-20 07:06:45 +02:00
var
filters: seq[string]
param: string
if query.kind == users:
2019-09-13 22:24:58 +02:00
return query.text
2019-08-06 17:41:06 +02:00
for i, user in query.fromUser:
param &= &"from:{user} "
2019-08-06 17:41:06 +02: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 11:55:19 +02:00
for f in query.filters:
filters.add "filter:" & f
2019-07-04 11:55:19 +02:00
for e in query.excludes:
if e == "nativeretweets": continue
filters.add "-filter:" & e
2019-09-20 03:35:27 +02:00
for i in query.includes:
filters.add "include:" & i
2019-09-13 13:20:08 +02:00
result = strip(param & filters.join(&" {query.sep} "))
2019-09-19 22:11:38 +02:00
if query.since.len > 0:
result &= " since:" & query.since
if query.until.len > 0:
result &= " until:" & query.until
2019-09-19 23:36:21 +02:00
if query.near.len > 0:
result &= &" near:\"{query.near}\" within:15mi"
2019-09-13 13:20:08 +02:00
if query.text.len > 0:
if result.len > 0:
result &= " " & query.text
else:
result = query.text
2020-04-29 15:06:15 +02:00
2019-09-20 22:56:27 +02:00
proc genQueryUrl*(query: Query): string =
if query.kind notin {tweets, users}: return
var params = @[&"f={query.kind}"]
2019-09-13 13:20:08 +02: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-21 01:11:03 +02:00
for i in query.includes.filterIt(it != "nativeretweets"):
params.add "i-" & i & "=on"
2019-09-19 22:11:38 +02:00
if query.since.len > 0:
params.add "since=" & query.since
if query.until.len > 0:
params.add "until=" & query.until
2019-09-19 23:36:21 +02:00
if query.near.len > 0:
params.add "near=" & query.near
2019-09-19 22:11:38 +02:00
if params.len > 0:
2019-09-13 22:24:58 +02:00
result &= params.join("&")