Fix urlencode values, no need to manually encode chars

This commit is contained in:
Victor 2020-05-26 18:30:46 +03:00
parent ef32739e3c
commit d8e5a6b40c

View File

@ -4,7 +4,7 @@
use ["std", "math", "http", "json", "functional", "files", "jdbc"] use ["std", "math", "http", "json", "functional", "files", "jdbc"]
// Telegram // Telegram
def toParams(obj) = reduce(obj, "", def(acc, k, v) = acc + k + "=" + v + "&") def toParams(obj) = reduce(obj, "", def(acc, k, v) = acc + k + "=" + urlencode(v) + "&")
def createRawUrl(method, params, token = "") = "https://api.telegram.org/bot" + token + "/" + method + "?" + params + "access_token=" + token def createRawUrl(method, params, token = "") = "https://api.telegram.org/bot" + token + "/" + method + "?" + params + "access_token=" + token
def createUrl(method, params, token = "") = createRawUrl(method, toParams(params), token) def createUrl(method, params, token = "") = createRawUrl(method, toParams(params), token)
def invokeJson(method, params, callback) = http(createUrl(method, params, config.token), combine(::jsondecode, callback)) def invokeJson(method, params, callback) = http(createUrl(method, params, config.token), combine(::jsondecode, callback))
@ -26,7 +26,7 @@ def sendMediaGroup(chatId, media) {
def fetchSubreddit(subreddit) { def fetchSubreddit(subreddit) {
url = "https://www.reddit.com/r/" + subreddit + ".json" url = "https://www.reddit.com/r/" + subreddit + ".json"
data = sync(def(ret) = http(url, combine(::jsondecode, ret))).data ?? [] data = sync(def(ret) = http(url, combine(::jsondecode, ret))).data ?? []
if (data.isEmpty()) return [] if (!length(data)) return []
return stream(data.children) return stream(data.children)
.map(def(child) = child.data) .map(def(child) = child.data)
.limit(min(config["items-in-top"], data.dist ?? 0)) .limit(min(config["items-in-top"], data.dist ?? 0))
@ -97,27 +97,21 @@ def multireplace(str, replacements) {
return str return str
} }
r10s = [ def safe(str) = str.replace("&", "&")
[">", "&gt;"], ["<", "&lt;"], ["!", "%21"], ["#", "%23"], .replace("<", "&lt;").replace(">", "&gt;")
["$", "%24"], ["&", "%26"], ["'", "%27"], ["(", "%28"],
[")", "%29"], ["*", "%2A"], ["+", "%2B"], [",", "%2C"],
["/", "%2F"], [":", "%3A"], [";", "%3B"], ["=", "%3D"],
["?", "%3F"], ["@", "%40"], ["[", "%5B"], ["]", "%5D"]
]
def safeText(str) = multireplace(str, r10s)
def safeLink(str) = multireplace(str, r10s + ["\"", "&quot;"])
def getCaption(post) { def getCaption(post) {
tag = "" tag = ""
if (length(post.flair_text) > 0) { if (length(post.flair_text) > 0) {
tag = " %23" + strToHashtag(post.sub + "_" + post.flair_text) tag = " #" + strToHashtag(post.sub + "_" + post.flair_text)
} }
return sprintf( return sprintf(
"<a href=\"%s\">%s</a>\n" + "<a href=\"%s\">%s</a>\n" +
"<a href=\"https://reddit.com%s\">🗨 Comments</a>%s", "<a href=\"https://reddit.com%s\">🗨 Comments</a>%s",
safeLink(post.url), safe(post.url),
safeText(post.title), safe(post.title),
safeLink(post.permalink), safe(post.permalink),
tag tag
) )
} }