Compare commits

...

3 Commits

Author SHA1 Message Date
Danila Fedorin 5150d965a3 Try to fix URL lookup errors 2019-04-14 15:02:08 -07:00
Danila Fedorin 3f1039e5dc Refactor some reddit polling code 2019-04-14 14:39:38 -07:00
Danila Fedorin 92cdd200ba Update to 0.27.2 2019-04-14 14:05:56 -07:00
2 changed files with 52 additions and 19 deletions

View File

@ -38,7 +38,7 @@ end
spawn do spawn do
loop do loop do
time = Time.now time = Time.local
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

View File

@ -1,27 +1,60 @@
require "http/client" require "http/client"
require "json" require "json"
def get_reddit_json(subreddit) class RedditWrapper(T)
request_url = "https://www.reddit.com/r/#{subreddit}/hot.json?limit=30" JSON.mapping(
response = HTTP::Client.get(request_url, headers: HTTP::Headers { kind: String,
"User-agent" => "Joann-Pupper-Bot" data: T)
})
response.body?.try { |body| JSON.parse(body) }
end end
def filter_reddit_json(json, completed) class RedditChild
json["data"]["children"] JSON.mapping(
.as_a url: String,
.map(&.["data"]) name: String,
.select do |it| title: String)
url = it["url"].as_s end
name = it["name"].as_s
!completed.includes?(name) && (url.ends_with?(".png") || url.ends_with?(".jpg")) class RedditResponse
end 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 end
def get_reddit_post(subreddit, completed) def get_reddit_post(subreddit, completed)
json = get_reddit_json(subreddit) return nil unless json = RedditResponse.from_subreddit subreddit
post = json.try { |json| filter_reddit_json(json, completed).first? } return nil unless post = filter_reddit_json(json, completed).first?
post.try { |post| completed.push(post["name"].as_s); { post["url"].as_s, post["title"].as_s } } completed.push(post.name)
return { post.url, post.title }
end end