nitter/src/query.nim

128 lines
3.1 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"
]
# Experimental, this might break in the future
# Till then, it results in shorter urls
const
2019-09-19 20:11:38 +00:00
posPrefix = "thGAVUV0VFVB"
posSuffix = "EjUAFQAlAFUAFQAA"
template `@`(param: string): untyped =
if param in pms: pms[param]
else: ""
proc initQuery*(pms: Table[string, string]; name=""): Query =
result = Query(
kind: parseEnum[QueryKind](@"kind", custom),
text: @"text",
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
)
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-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:
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
2019-09-20 01:35:27 +00:00
proc genQueryUrl*(query: Query; onlyParam=false): string =
2019-09-13 20:24:58 +00:00
if query.fromUser.len > 0:
result = "/" & query.fromUser.join(",")
2019-09-19 00:23:22 +00:00
if query.fromUser.len > 1:
2019-09-13 20:24:58 +00:00
return result & "?"
if query.kind notin {custom, users}:
return result & &"/{query.kind}?"
2019-08-08 17:19:27 +00:00
2019-09-20 01:35:27 +00:00
if onlyParam:
result = ""
else:
result &= &"/search?"
2019-09-13 20:24:58 +00:00
var params = @[&"kind={query.kind}"]
2019-09-13 11:20:08 +00:00
if query.text.len > 0:
2019-09-20 00:20:09 +00:00
params.add "text=" & 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 01:35:27 +00:00
for i in query.includes:
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("&")
proc cleanPos*(pos: string): string =
pos.multiReplace((posPrefix, ""), (posSuffix, ""))
proc genPos*(pos: string): string =
2019-09-19 20:11:38 +00:00
result = posPrefix & pos
if "A==" notin result:
result &= posSuffix