Switch to okhttp with cookies requests

This commit is contained in:
aNNiMON 2023-08-13 19:50:17 +03:00
parent 78215d8268
commit afd8fed176
3 changed files with 23 additions and 2 deletions

View File

@ -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)

View File

@ -1,5 +1,6 @@
config = {
"token": "1234567890:AABBCCDDEE",
"cookie": "Reddit cookie string",
"peer": 1234, // chat_id
"items-in-top": 3,
"subreddits": [

View File

@ -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)