Clean up Reddit mapping

This commit is contained in:
Omar Roth 2018-04-21 18:04:01 -05:00
parent 19f5729e18
commit 8477579e0b
2 changed files with 50 additions and 32 deletions

View file

@ -114,36 +114,50 @@ class User
}) })
end end
class RedditSubmit class RedditThing
JSON.mapping({ JSON.mapping({
data: RedditSubmitData, kind: String,
data: RedditComment | RedditLink | RedditMore | RedditListing,
}) })
end end
class RedditSubmitData class RedditComment
JSON.mapping({ JSON.mapping({
children: Array(RedditThread), author: String,
body_html: String,
replies: RedditThing | String,
score: Int32,
depth: Int32,
}) })
end end
class RedditThread class RedditLink
JSON.mapping({ JSON.mapping({
data: RedditThreadData,
})
end
class RedditThreadData
JSON.mapping({
subreddit: String,
id: String,
num_comments: Int32,
score: Int32,
author: String, author: String,
score: Int32,
subreddit: String,
num_comments: Int32,
id: String,
permalink: String, permalink: String,
title: String, title: String,
}) })
end end
class RedditMore
JSON.mapping({
children: Array(String),
count: Int32,
depth: Int32,
})
end
class RedditListing
JSON.mapping({
children: Array(RedditThing),
modhash: String,
})
end
# See http://www.evanmiller.org/how-not-to-sort-by-average-rating.html # See http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
def ci_lower_bound(pos, n) def ci_lower_bound(pos, n)
if n == 0 if n == 0
@ -376,35 +390,39 @@ def get_reddit_comments(id, client, headers)
search_results = client.get("/search.json?q=#{query}", headers) search_results = client.get("/search.json?q=#{query}", headers)
if search_results.status_code == 200 if search_results.status_code == 200
search_results = RedditSubmit.from_json(search_results.body) search_results = RedditThing.from_json(search_results.body)
thread = search_results.data.children.sort_by { |child| child.data.score }[-1] thread = search_results.data.as(RedditListing).children.sort_by { |child| child.data.as(RedditLink).score }[-1]
result = client.get("/r/#{thread.data.subreddit}/comments/#{thread.data.id}?limit=100&sort=top", headers).body thread = thread.data.as(RedditLink)
result = JSON.parse(result)
result = client.get("/r/#{thread.subreddit}/comments/#{thread.id}?limit=100&sort=top", headers).body
result = Array(RedditThing).from_json(result)
elsif search_results.status_code == 302 elsif search_results.status_code == 302
search_results = client.get(search_results.headers["Location"], headers).body result = client.get(search_results.headers["Location"], headers).body
result = Array(RedditThing).from_json(result)
result = JSON.parse(search_results) thread = result[0].data.as(RedditListing).children[0].data.as(RedditLink)
thread = RedditThread.from_json(result[0]["data"]["children"][0].to_json)
else else
raise "Got error code #{search_results.status_code}" raise "Got error code #{search_results.status_code}"
end end
comments = result[1]["data"]["children"] comments = result[1].data.as(RedditListing).children
return comments, thread return comments, thread
end end
def template_comments(root) def template_comments(root)
html = "" html = ""
root.each do |child| root.each do |child|
if child["data"]["body_html"]? if child.data.is_a?(RedditComment)
author = child["data"]["author"] child = child.data.as(RedditComment)
score = child["data"]["score"] author = child.author
body_html = HTML.unescape(child["data"]["body_html"].as_s) score = child.score
body_html = HTML.unescape(child.body_html)
replies_html = "" replies_html = ""
if child["data"]["replies"] != "" if child.replies.is_a?(RedditThing)
replies_html = template_comments(child["data"]["replies"]["data"]["children"]) replies = child.replies.as(RedditThing)
replies_html = template_comments(replies.data.as(RedditListing).children)
end end
content = <<-END_HTML content = <<-END_HTML
@ -417,7 +435,7 @@ def template_comments(root)
</div> </div>
END_HTML END_HTML
if child["data"]["depth"].as_i > 0 if child.depth > 0
html += <<-END_HTML html += <<-END_HTML
<div class="pure-g"> <div class="pure-g">
<div class="pure-u-1-24"> <div class="pure-u-1-24">

View file

@ -186,10 +186,10 @@ player.src(currentSources);
<div> <div>
<h3> <h3>
<a href="javascript:void(0)" onclick="toggle_comments(this)">[ - ]</a> <a href="javascript:void(0)" onclick="toggle_comments(this)">[ - ]</a>
<%= reddit_thread.data.title %> <%= reddit_thread.title %>
</h3> </h3>
<b> <b>
<a target="_blank" href="https://reddit.com<%= reddit_thread.data.permalink %>">View more comments on Reddit</a> <a target="_blank" href="https://reddit.com<%= reddit_thread.permalink %>">View more comments on Reddit</a>
</b> </b>
</div> </div>
<div> <div>