From afd8fed176c279f9d32919d62a27e12fc4609faa Mon Sep 17 00:00:00 2001 From: aNNiMON Date: Sun, 13 Aug 2023 19:50:17 +0300 Subject: [PATCH] Switch to okhttp with cookies requests --- Reddit.own | 22 +++++++++++++++++++++- main.example.own | 1 + redditimages.own | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Reddit.own b/Reddit.own index 411e99d..2a5aaee 100644 --- a/Reddit.own +++ b/Reddit.own @@ -1,7 +1,27 @@ +use ["okhttp", "types"] + class Reddit { + def Reddit(cookie) { + this.cookie = cookie + } + def fetchSubreddit(subreddit, maxItems = 5) { url = "https://www.reddit.com/r/" + subreddit + ".json" - data = sync(def(ret) = http(url, combine(::jsondecode, ret))).data ?? [] + response = okhttp.request() + .headers({ + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/116.0", + "Cookie": this.cookie + }) + .url(url) + .get() + .newCall(okhttp.client) + .execute() + .body() + .string() + if (!length(response)) return [] + jsonData = jsondecode(response) + if (typeof(jsonData) != MAP || !arrayKeyExists("data", jsonData)) return [] + data = jsonData.data ?? [] if (!length(data)) return [] return stream(data.children) .map(def(child) = child.data) diff --git a/main.example.own b/main.example.own index 01d50f4..5b80be8 100644 --- a/main.example.own +++ b/main.example.own @@ -1,5 +1,6 @@ config = { "token": "1234567890:AABBCCDDEE", + "cookie": "Reddit cookie string", "peer": 1234, // chat_id "items-in-top": 3, "subreddits": [ diff --git a/redditimages.own b/redditimages.own index 23c8172..5c6336b 100644 --- a/redditimages.own +++ b/redditimages.own @@ -5,7 +5,7 @@ include "Reddit.own" include "database.own" bot = new TelegramBot(config.token) -reddit = new Reddit() +reddit = new Reddit(config.cookie) subreddits = reddit.fetchSubreddits(config.subreddits, config["items-in-top"]) media = stream(subreddits)