2019-08-11 19:26:37 +00:00
|
|
|
import strutils, strformat, sequtils
|
2019-07-03 09:46:03 +00:00
|
|
|
|
|
|
|
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"
|
2019-07-03 09:46:03 +00:00
|
|
|
]
|
2019-09-17 19:01:44 +00:00
|
|
|
commonFilters* = @[
|
|
|
|
"media", "videos", "images", "links", "news", "quote"
|
|
|
|
]
|
|
|
|
advancedFilters* = @[
|
|
|
|
"mentions", "verified", "safe", "twimg", "native_video",
|
|
|
|
"consumer_video", "pro_video"
|
|
|
|
]
|
2019-07-03 09:46:03 +00:00
|
|
|
|
|
|
|
# Experimental, this might break in the future
|
|
|
|
# Till then, it results in shorter urls
|
|
|
|
const
|
|
|
|
posPrefix = "thGAVUV0VFVBa"
|
|
|
|
posSuffix = "EjUAFQAlAFUAFQAA"
|
|
|
|
|
2019-09-13 20:24:58 +00:00
|
|
|
proc initQuery*(filters, includes, excludes, separator, text: string; name=""): Query =
|
2019-07-03 09:46:03 +00:00
|
|
|
var sep = separator.strip().toUpper()
|
|
|
|
Query(
|
2019-07-11 17:22:23 +00:00
|
|
|
kind: custom,
|
2019-09-13 11:20:08 +00:00
|
|
|
text: text,
|
2019-07-04 09:55:19 +00:00
|
|
|
filters: filters.split(",").filterIt(it in validFilters),
|
|
|
|
includes: includes.split(",").filterIt(it in validFilters),
|
|
|
|
excludes: excludes.split(",").filterIt(it in validFilters),
|
2019-08-06 15:41:06 +00:00
|
|
|
fromUser: @[name],
|
2019-07-04 09:55:19 +00:00
|
|
|
sep: if sep in separators: sep else: ""
|
2019-07-03 09:46:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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"
|
2019-07-03 09:46:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
)
|
2019-07-03 09:46:03 +00:00
|
|
|
|
|
|
|
proc genQueryParam*(query: Query): string =
|
|
|
|
var filters: seq[string]
|
|
|
|
var param: string
|
|
|
|
|
2019-09-13 20:24:58 +00:00
|
|
|
if query.kind == users:
|
|
|
|
return query.text
|
|
|
|
|
2019-08-06 15:41:06 +00:00
|
|
|
for i, user in query.fromUser:
|
|
|
|
param &= &"from:{user} "
|
|
|
|
if i < query.fromUser.high:
|
|
|
|
param &= "OR "
|
2019-07-03 09:46:03 +00:00
|
|
|
|
2019-07-04 09:55:19 +00:00
|
|
|
for f in query.filters:
|
2019-07-03 09:46:03 +00:00
|
|
|
filters.add "filter:" & f
|
2019-07-04 09:55:19 +00:00
|
|
|
for i in query.includes:
|
|
|
|
filters.add "include:" & i
|
|
|
|
for e in query.excludes:
|
2019-07-03 09:46:03 +00:00
|
|
|
filters.add "-filter:" & e
|
|
|
|
|
2019-09-13 11:20:08 +00:00
|
|
|
result = strip(param & filters.join(&" {query.sep} "))
|
|
|
|
if query.text.len > 0:
|
|
|
|
result &= " " & query.text
|
2019-07-03 09:46:03 +00:00
|
|
|
|
|
|
|
proc genQueryUrl*(query: Query): string =
|
2019-09-13 20:24:58 +00:00
|
|
|
if query.fromUser.len > 0:
|
|
|
|
result = "/" & query.fromUser.join(",")
|
|
|
|
|
|
|
|
if query.kind == multi:
|
|
|
|
return result & "?"
|
|
|
|
|
|
|
|
if query.kind notin {custom, users}:
|
|
|
|
return result & &"/{query.kind}?"
|
2019-08-08 17:19:27 +00:00
|
|
|
|
2019-09-13 20:24:58 +00:00
|
|
|
result &= &"/search?"
|
2019-07-03 09:46:03 +00:00
|
|
|
|
2019-09-13 20:24:58 +00:00
|
|
|
var params = @[&"kind={query.kind}"]
|
2019-07-04 09:55:19 +00:00
|
|
|
if query.filters.len > 0:
|
|
|
|
params &= "filter=" & query.filters.join(",")
|
|
|
|
if query.includes.len > 0:
|
|
|
|
params &= "include=" & query.includes.join(",")
|
|
|
|
if query.excludes.len > 0:
|
|
|
|
params &= "not=" & query.excludes.join(",")
|
2019-07-03 09:46:03 +00:00
|
|
|
if query.sep.len > 0:
|
|
|
|
params &= "sep=" & query.sep
|
2019-09-13 11:20:08 +00:00
|
|
|
if query.text.len > 0:
|
|
|
|
params &= "text=" & query.text
|
2019-07-03 09:46:03 +00:00
|
|
|
if params.len > 0:
|
2019-09-13 20:24:58 +00:00
|
|
|
result &= params.join("&")
|
2019-07-03 09:46:03 +00:00
|
|
|
|
|
|
|
proc cleanPos*(pos: string): string =
|
|
|
|
pos.multiReplace((posPrefix, ""), (posSuffix, ""))
|
|
|
|
|
|
|
|
proc genPos*(pos: string): string =
|
|
|
|
posPrefix & pos & posSuffix
|