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"]
// 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 = [
[">", "&gt;"], ["<", "&lt;"], ["!", "%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 + ["\"", "&quot;"])
def safe(str) = str.replace("&", "&amp;")
.replace("<", "&lt;").replace(">", "&gt;")
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(
"<a href=\"%s\">%s</a>\n" +
"<a href=\"https://reddit.com%s\">🗨 Comments</a>%s",
safeLink(post.url),
safeText(post.title),
safeLink(post.permalink),
safe(post.url),
safe(post.title),
safe(post.permalink),
tag
)
}