2018-08-05 02:00:44 +05:30
|
|
|
class RedditThing
|
|
|
|
JSON.mapping({
|
|
|
|
kind: String,
|
|
|
|
data: RedditComment | RedditLink | RedditMore | RedditListing,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
class RedditComment
|
2018-08-13 21:00:27 +05:30
|
|
|
module TimeConverter
|
|
|
|
def self.from_json(value : JSON::PullParser) : Time
|
|
|
|
Time.epoch(value.read_float.to_i)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.to_json(value : Time, json : JSON::Builder)
|
|
|
|
json.number(value.epoch)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-05 02:00:44 +05:30
|
|
|
JSON.mapping({
|
|
|
|
author: String,
|
|
|
|
body_html: String,
|
|
|
|
replies: RedditThing | String,
|
|
|
|
score: Int32,
|
|
|
|
depth: Int32,
|
2018-08-13 21:00:27 +05:30
|
|
|
created: {
|
|
|
|
type: Time,
|
|
|
|
converter: RedditComment::TimeConverter,
|
|
|
|
},
|
2018-08-05 02:00:44 +05:30
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
class RedditLink
|
|
|
|
JSON.mapping({
|
|
|
|
author: String,
|
|
|
|
score: Int32,
|
|
|
|
subreddit: String,
|
|
|
|
num_comments: Int32,
|
|
|
|
id: String,
|
|
|
|
permalink: String,
|
|
|
|
title: String,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
class RedditMore
|
|
|
|
JSON.mapping({
|
|
|
|
children: Array(String),
|
|
|
|
count: Int32,
|
|
|
|
depth: Int32,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
class RedditListing
|
|
|
|
JSON.mapping({
|
|
|
|
children: Array(RedditThing),
|
|
|
|
modhash: String,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_reddit_comments(id, client, headers)
|
|
|
|
query = "(url:3D#{id}%20OR%20url:#{id})%20(site:youtube.com%20OR%20site:youtu.be)"
|
|
|
|
search_results = client.get("/search.json?q=#{query}", headers)
|
|
|
|
|
|
|
|
if search_results.status_code == 200
|
|
|
|
search_results = RedditThing.from_json(search_results.body)
|
|
|
|
|
|
|
|
thread = search_results.data.as(RedditListing).children.sort_by { |child| child.data.as(RedditLink).score }[-1]
|
|
|
|
thread = thread.data.as(RedditLink)
|
|
|
|
|
|
|
|
result = client.get("/r/#{thread.subreddit}/comments/#{thread.id}.json?limit=100&sort=top", headers).body
|
|
|
|
result = Array(RedditThing).from_json(result)
|
|
|
|
elsif search_results.status_code == 302
|
|
|
|
result = client.get(search_results.headers["Location"], headers).body
|
|
|
|
result = Array(RedditThing).from_json(result)
|
|
|
|
|
|
|
|
thread = result[0].data.as(RedditListing).children[0].data.as(RedditLink)
|
|
|
|
else
|
|
|
|
raise "Got error code #{search_results.status_code}"
|
|
|
|
end
|
|
|
|
|
|
|
|
comments = result[1].data.as(RedditListing).children
|
|
|
|
return comments, thread
|
|
|
|
end
|
|
|
|
|
|
|
|
def template_youtube_comments(comments)
|
|
|
|
html = ""
|
|
|
|
|
|
|
|
root = comments["comments"].as_a
|
|
|
|
root.each do |child|
|
|
|
|
if child["replies"]?
|
|
|
|
replies_html = <<-END_HTML
|
|
|
|
<div id="replies" class="pure-g">
|
2018-08-05 09:57:41 +05:30
|
|
|
<div class="pure-u-1-24"></div>
|
|
|
|
<div class="pure-u-23-24">
|
2018-08-05 02:00:44 +05:30
|
|
|
<p>
|
|
|
|
<a href="javascript:void(0)" data-continuation="#{child["replies"]["continuation"]}"
|
2018-08-24 03:25:26 +05:30
|
|
|
onclick="get_youtube_replies(this)">View #{child["replies"]["replyCount"]} replies</a>
|
2018-08-05 02:00:44 +05:30
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
END_HTML
|
|
|
|
end
|
|
|
|
|
2018-09-18 05:09:28 +05:30
|
|
|
author_thumbnail = "/ggpht#{URI.parse(child["authorThumbnails"][-1]["url"].as_s).full_path}"
|
|
|
|
|
2018-08-05 02:00:44 +05:30
|
|
|
html += <<-END_HTML
|
|
|
|
<div class="pure-g">
|
2018-08-13 20:46:01 +05:30
|
|
|
<div class="pure-u-2-24">
|
2018-09-18 05:09:28 +05:30
|
|
|
<img style="width:90%; padding-right:1em; padding-top:1em;" src="#{author_thumbnail}">
|
2018-08-13 20:46:01 +05:30
|
|
|
</div>
|
|
|
|
<div class="pure-u-22-24">
|
2018-08-05 02:00:44 +05:30
|
|
|
<p>
|
2018-08-13 20:46:01 +05:30
|
|
|
<a href="javascript:void(0)" onclick="toggle(this)">[ - ]</a>
|
|
|
|
<i class="icon ion-ios-thumbs-up"></i> #{child["likeCount"]}
|
|
|
|
<b><a href="#{child["authorUrl"]}">#{child["author"]}</a></b>
|
|
|
|
- #{recode_date(Time.epoch(child["published"].as_i64))} ago
|
|
|
|
</p>
|
|
|
|
<div>
|
2018-08-31 06:36:08 +05:30
|
|
|
<p style="white-space:pre-wrap">#{child["contentHtml"]}</p>
|
2018-08-13 20:46:01 +05:30
|
|
|
#{replies_html}
|
|
|
|
</div>
|
2018-08-05 02:00:44 +05:30
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
END_HTML
|
|
|
|
end
|
|
|
|
|
|
|
|
if comments["continuation"]?
|
|
|
|
html += <<-END_HTML
|
|
|
|
<div class="pure-g">
|
|
|
|
<div class="pure-u-1">
|
|
|
|
<p>
|
|
|
|
<a href="javascript:void(0)" data-continuation="#{comments["continuation"]}"
|
2018-08-24 03:25:26 +05:30
|
|
|
onclick="get_youtube_replies(this)">Load more</a>
|
2018-08-05 02:00:44 +05:30
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
END_HTML
|
|
|
|
end
|
|
|
|
|
|
|
|
return html
|
|
|
|
end
|
|
|
|
|
|
|
|
def template_reddit_comments(root)
|
|
|
|
html = ""
|
|
|
|
root.each do |child|
|
|
|
|
if child.data.is_a?(RedditComment)
|
|
|
|
child = child.data.as(RedditComment)
|
|
|
|
author = child.author
|
|
|
|
score = child.score
|
|
|
|
body_html = HTML.unescape(child.body_html)
|
|
|
|
|
|
|
|
replies_html = ""
|
|
|
|
if child.replies.is_a?(RedditThing)
|
|
|
|
replies = child.replies.as(RedditThing)
|
|
|
|
replies_html = template_reddit_comments(replies.data.as(RedditListing).children)
|
|
|
|
end
|
|
|
|
|
|
|
|
content = <<-END_HTML
|
|
|
|
<p>
|
|
|
|
<a href="javascript:void(0)" onclick="toggle(this)">[ - ]</a>
|
|
|
|
<i class="icon ion-ios-thumbs-up"></i> #{score}
|
|
|
|
<b><a href="https://www.reddit.com/user/#{author}">#{author}</a></b>
|
2018-08-13 21:00:27 +05:30
|
|
|
- #{recode_date(child.created)} ago
|
2018-08-05 02:00:44 +05:30
|
|
|
</p>
|
|
|
|
<div>
|
|
|
|
#{body_html}
|
|
|
|
#{replies_html}
|
|
|
|
</div>
|
|
|
|
END_HTML
|
|
|
|
|
|
|
|
if child.depth > 0
|
|
|
|
html += <<-END_HTML
|
|
|
|
<div class="pure-g">
|
|
|
|
<div class="pure-u-1-24">
|
|
|
|
</div>
|
|
|
|
<div class="pure-u-23-24">
|
|
|
|
#{content}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
END_HTML
|
|
|
|
else
|
|
|
|
html += <<-END_HTML
|
|
|
|
<div class="pure-g">
|
|
|
|
<div class="pure-u-1">
|
|
|
|
#{content}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
END_HTML
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return html
|
|
|
|
end
|
|
|
|
|
2018-09-04 08:45:47 +05:30
|
|
|
def replace_links(html)
|
|
|
|
html = XML.parse_html(html)
|
2018-08-05 02:00:44 +05:30
|
|
|
|
2018-09-04 08:45:47 +05:30
|
|
|
html.xpath_nodes(%q(//a)).each do |anchor|
|
2018-08-05 02:00:44 +05:30
|
|
|
url = URI.parse(anchor["href"])
|
|
|
|
|
2018-09-09 19:48:31 +05:30
|
|
|
if {"www.youtube.com", "m.youtube.com", "youtu.be"}.includes?(url.host)
|
2018-08-05 02:00:44 +05:30
|
|
|
if url.path == "/redirect"
|
|
|
|
params = HTTP::Params.parse(url.query.not_nil!)
|
2018-09-04 08:45:47 +05:30
|
|
|
anchor["href"] = params["q"]?
|
2018-08-05 02:00:44 +05:30
|
|
|
else
|
2018-09-04 08:45:47 +05:30
|
|
|
anchor["href"] = url.full_path
|
2018-08-05 02:00:44 +05:30
|
|
|
end
|
|
|
|
elsif url.to_s == "#"
|
2018-08-07 09:30:39 +05:30
|
|
|
begin
|
|
|
|
length_seconds = decode_length_seconds(anchor.content)
|
|
|
|
rescue ex
|
|
|
|
length_seconds = decode_time(anchor.content)
|
|
|
|
end
|
|
|
|
|
2018-09-04 08:45:47 +05:30
|
|
|
anchor["href"] = "javascript:void(0)"
|
|
|
|
anchor["onclick"] = "player.currentTime(#{length_seconds})"
|
2018-08-05 02:00:44 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-04 08:45:47 +05:30
|
|
|
html = html.to_xml(options: XML::SaveOptions::NO_DECL)
|
2018-08-05 02:00:44 +05:30
|
|
|
return html
|
|
|
|
end
|
|
|
|
|
|
|
|
def fill_links(html, scheme, host)
|
|
|
|
html = XML.parse_html(html)
|
|
|
|
|
|
|
|
html.xpath_nodes("//a").each do |match|
|
|
|
|
url = URI.parse(match["href"])
|
|
|
|
# Reddit links don't have host
|
|
|
|
if !url.host && !match["href"].starts_with?("javascript") && !url.to_s.ends_with? "#"
|
|
|
|
url.scheme = scheme
|
|
|
|
url.host = host
|
|
|
|
match["href"] = url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if host == "www.youtube.com"
|
2018-08-15 20:52:36 +05:30
|
|
|
html = html.xpath_node(%q(//body)).not_nil!.to_xml
|
2018-08-05 02:00:44 +05:30
|
|
|
else
|
|
|
|
html = html.to_xml(options: XML::SaveOptions::NO_DECL)
|
|
|
|
end
|
|
|
|
|
2018-09-04 08:45:47 +05:30
|
|
|
return html
|
2018-08-05 02:00:44 +05:30
|
|
|
end
|