From d8e5a6b40c7d50de7fe2d53fde9c6eb8bf0768cc Mon Sep 17 00:00:00 2001 From: Victor Date: Tue, 26 May 2020 18:30:46 +0300 Subject: [PATCH] Fix urlencode values, no need to manually encode chars --- redditimages2telegram.own | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/redditimages2telegram.own b/redditimages2telegram.own index 6c55152..68e2a1e 100644 --- a/redditimages2telegram.own +++ b/redditimages2telegram.own @@ -4,7 +4,7 @@ use ["std", "math", "http", "json", "functional", "files", "jdbc"] // 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 createUrl(method, params, token = "") = createRawUrl(method, toParams(params), token) 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) { url = "https://www.reddit.com/r/" + subreddit + ".json" data = sync(def(ret) = http(url, combine(::jsondecode, ret))).data ?? [] - if (data.isEmpty()) return [] + if (!length(data)) return [] return stream(data.children) .map(def(child) = child.data) .limit(min(config["items-in-top"], data.dist ?? 0)) @@ -97,27 +97,21 @@ def multireplace(str, replacements) { return str } -r10s = [ - [">", ">"], ["<", "<"], ["!", "%21"], ["#", "%23"], - ["$", "%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 + ["\"", """]) +def safe(str) = str.replace("&", "&") + .replace("<", "<").replace(">", ">") + def getCaption(post) { tag = "" if (length(post.flair_text) > 0) { - tag = " %23" + strToHashtag(post.sub + "_" + post.flair_text) + tag = " #" + strToHashtag(post.sub + "_" + post.flair_text) } return sprintf( "%s\n" + "🗨 Comments%s", - safeLink(post.url), - safeText(post.title), - safeLink(post.permalink), + safe(post.url), + safe(post.title), + safe(post.permalink), tag ) }