Compare commits
No commits in common. "5150d965a34deae354a34e24d384644024473e5b" and "1c6625400a0665d38d51af88254db2687397f2a7" have entirely different histories.
5150d965a3
...
1c6625400a
@ -38,7 +38,7 @@ end
|
|||||||
|
|
||||||
spawn do
|
spawn do
|
||||||
loop do
|
loop do
|
||||||
time = Time.local
|
time = Time.now
|
||||||
url_tuple = get_reddit_post(subreddit, completed) if (active_hours.includes? time.hour)
|
url_tuple = get_reddit_post(subreddit, completed) if (active_hours.includes? time.hour)
|
||||||
if url_tuple
|
if url_tuple
|
||||||
url, title = url_tuple
|
url, title = url_tuple
|
||||||
|
@ -1,60 +1,27 @@
|
|||||||
require "http/client"
|
require "http/client"
|
||||||
require "json"
|
require "json"
|
||||||
|
|
||||||
class RedditWrapper(T)
|
def get_reddit_json(subreddit)
|
||||||
JSON.mapping(
|
request_url = "https://www.reddit.com/r/#{subreddit}/hot.json?limit=30"
|
||||||
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 {
|
response = HTTP::Client.get(request_url, headers: HTTP::Headers {
|
||||||
"User-agent" => "Joann-Pupper-Bot"
|
"User-agent" => "Joann-Pupper-Bot"
|
||||||
})
|
})
|
||||||
|
response.body?.try { |body| JSON.parse(body) }
|
||||||
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
|
end
|
||||||
|
|
||||||
def filter_reddit_json(json, completed, extensions = ["png", "json"])
|
def filter_reddit_json(json, completed)
|
||||||
json.data.posts_matching do |post|
|
json["data"]["children"]
|
||||||
extensions.any? { |it| post.url.ends_with? it } && !completed.includes? post.url
|
.as_a
|
||||||
end
|
.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
|
end
|
||||||
|
|
||||||
def get_reddit_post(subreddit, completed)
|
def get_reddit_post(subreddit, completed)
|
||||||
return nil unless json = RedditResponse.from_subreddit subreddit
|
json = get_reddit_json(subreddit)
|
||||||
return nil unless post = filter_reddit_json(json, completed).first?
|
post = json.try { |json| filter_reddit_json(json, completed).first? }
|
||||||
completed.push(post.name)
|
post.try { |post| completed.push(post["name"].as_s); { post["url"].as_s, post["title"].as_s } }
|
||||||
return { post.url, post.title }
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user