Switch to okhttp with cookies requests
This commit is contained in:
parent
78215d8268
commit
afd8fed176
22
Reddit.own
22
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)
|
||||
|
@ -1,5 +1,6 @@
|
||||
config = {
|
||||
"token": "1234567890:AABBCCDDEE",
|
||||
"cookie": "Reddit cookie string",
|
||||
"peer": 1234, // chat_id
|
||||
"items-in-top": 3,
|
||||
"subreddits": [
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user