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
|
2019-07-03 11:46:03 +02:00
|
|
|
|
|
|
|
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"
|
2019-07-03 11:46:03 +02:00
|
|
|
]
|
|
|
|
|
2021-12-28 07:25:37 +01:00
|
|
|
emptyQuery* = "include:nativeretweets"
|
|
|
|
|
2019-09-19 01:01:47 +02:00
|
|
|
template `@`(param: string): untyped =
|
|
|
|
if param in pms: pms[param]
|
|
|
|
else: ""
|
|
|
|
|
|
|
|
proc initQuery*(pms: Table[string, string]; name=""): Query =
|
|
|
|
result = Query(
|
2019-10-08 13:54:20 +02:00
|
|
|
kind: parseEnum[QueryKind](@"f", tweets),
|
2019-09-30 22:24:01 +02:00
|
|
|
text: @"q",
|
2019-09-19 01:01:47 +02:00
|
|
|
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-07-03 11:46:03 +02:00
|
|
|
)
|
|
|
|
|
2019-09-19 01:11:35 +02:00
|
|
|
if name.len > 0:
|
|
|
|
result.fromUser = name.split(",")
|
|
|
|
|
2019-07-03 11:46:03 +02:00
|
|
|
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"
|
2019-07-03 11:46:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
)
|
2019-07-03 11:46:03 +02:00
|
|
|
|
2020-06-17 14:12:38 +02:00
|
|
|
proc genQueryParam*(query: Query): string =
|
2020-05-20 07:06:45 +02:00
|
|
|
var
|
|
|
|
filters: seq[string]
|
|
|
|
param: string
|
2019-07-03 11:46:03 +02:00
|
|
|
|
2019-10-08 13:45:47 +02:00
|
|
|
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:
|
2020-06-17 14:16:48 +02:00
|
|
|
param &= &"from:{user} "
|
2019-08-06 17:41:06 +02:00
|
|
|
if i < query.fromUser.high:
|
|
|
|
param &= "OR "
|
2019-07-03 11:46:03 +02:00
|
|
|
|
2020-06-17 14:16:48 +02:00
|
|
|
if query.fromUser.len > 0 and query.kind in {posts, media}:
|
|
|
|
param &= "filter:self_threads OR-filter:replies "
|
|
|
|
|
2020-06-17 14:12:38 +02:00
|
|
|
if "nativeretweets" notin query.excludes:
|
|
|
|
param &= "include:nativeretweets "
|
|
|
|
|
2019-07-04 11:55:19 +02:00
|
|
|
for f in query.filters:
|
2019-07-03 11:46:03 +02:00
|
|
|
filters.add "filter:" & f
|
2019-07-04 11:55:19 +02:00
|
|
|
for e in query.excludes:
|
2020-06-17 14:16:48 +02:00
|
|
|
if e == "nativeretweets": continue
|
2019-07-03 11:46:03 +02:00
|
|
|
filters.add "-filter:" & e
|
2019-09-20 03:35:27 +02:00
|
|
|
for i in query.includes:
|
|
|
|
filters.add "include:" & i
|
2019-07-03 11:46:03 +02:00
|
|
|
|
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:
|
2020-06-17 14:16:48 +02:00
|
|
|
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 =
|
2019-10-08 13:54:20 +02:00
|
|
|
if query.kind notin {tweets, users}: return
|
2019-07-03 11:46:03 +02:00
|
|
|
|
2019-10-08 13:45:47 +02:00
|
|
|
var params = @[&"f={query.kind}"]
|
2019-09-13 13:20:08 +02:00
|
|
|
if query.text.len > 0:
|
2019-09-30 22:24:01 +02:00
|
|
|
params.add "q=" & encodeUrl(query.text)
|
2019-09-19 01:01:47 +02:00
|
|
|
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"):
|
2019-09-19 01:01:47 +02:00
|
|
|
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
|
|
|
|
2019-07-03 11:46:03 +02:00
|
|
|
if params.len > 0:
|
2019-09-13 22:24:58 +02:00
|
|
|
result &= params.join("&")
|