2018-03-20 16:31:49 -07:00
|
|
|
require "http/client"
|
|
|
|
require "json"
|
|
|
|
|
|
|
|
def get_reddit_json(subreddit)
|
|
|
|
request_url = "https://www.reddit.com/r/#{subreddit}/hot.json?limit=30"
|
|
|
|
response = HTTP::Client.get(request_url, headers: HTTP::Headers {
|
|
|
|
"User-agent" => "Joann-Pupper-Bot"
|
|
|
|
})
|
|
|
|
response.body?.try { |body| JSON.parse(body) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_reddit_json(json, completed)
|
|
|
|
json["data"]["children"]
|
2018-07-17 21:41:15 -07:00
|
|
|
.as_a
|
2018-03-20 16:31:49 -07:00
|
|
|
.map(&.["data"])
|
|
|
|
.select do |it|
|
|
|
|
url = it["url"].as_s
|
|
|
|
name = it["name"].as_s
|
|
|
|
!completed.includes?(name) && (url.ends_with?(".png") || url.ends_with?(".jpg"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_reddit_post(subreddit, completed)
|
|
|
|
json = get_reddit_json(subreddit)
|
|
|
|
post = json.try { |json| filter_reddit_json(json, completed).first? }
|
2018-07-17 21:41:15 -07:00
|
|
|
post.try { |post| completed.push(post["name"].as_s); { post["url"].as_s, post["title"].as_s } }
|
2018-03-20 16:31:49 -07:00
|
|
|
end
|