61 lines
1.4 KiB
Crystal
61 lines
1.4 KiB
Crystal
require "http/client"
|
|
require "json"
|
|
|
|
class RedditWrapper(T)
|
|
JSON.mapping(
|
|
kind: String,
|
|
data: T)
|
|
end
|
|
|
|
class RedditChild
|
|
JSON.mapping(
|
|
url: String,
|
|
name: String,
|
|
title: String)
|
|
end
|
|
|
|
class RedditResponse
|
|
JSON.mapping(
|
|
modhash: String,
|
|
dist: Int32,
|
|
children: Array(RedditWrapper(RedditChild)))
|
|
|
|
def self.from_subreddits(subreddits : Array(String))
|
|
request_url = URI.new scheme: "https", host: "www.reddit.com", path: "/r/hot.json", query: "limit=30"
|
|
response = HTTP::Client.get(request_url, headers: HTTP::Headers {
|
|
"User-agent" => "Joann-Pupper-Bot"
|
|
})
|
|
|
|
return nil unless body = response.body?
|
|
|
|
begin
|
|
RedditWrapper(RedditResponse).from_json body
|
|
rescue
|
|
nil
|
|
end
|
|
end
|
|
|
|
def self.from_subreddit(subreddit)
|
|
from_subreddits [subreddit]
|
|
end
|
|
|
|
def posts_matching(&block)
|
|
children
|
|
.map(&.data)
|
|
.select { |it| yield it }
|
|
end
|
|
end
|
|
|
|
def filter_reddit_json(json, completed, extensions = ["png", "json"])
|
|
json.data.posts_matching do |post|
|
|
extensions.any? { |it| post.url.ends_with? it } && !completed.includes? post.url
|
|
end
|
|
end
|
|
|
|
def get_reddit_post(subreddit, completed)
|
|
return nil unless json = RedditResponse.from_subreddit subreddit
|
|
return nil unless post = filter_reddit_json(json, completed).first?
|
|
completed.push(post.name)
|
|
return { post.url, post.title }
|
|
end
|