Extend CommunityPost with comment count

This commit is contained in:
syeopite 2021-07-26 21:49:20 -07:00
parent 34f9159277
commit debff6dae9
No known key found for this signature in database
GPG key ID: 6FA616E5A5294A82
3 changed files with 39 additions and 25 deletions

View file

@ -38,6 +38,7 @@ module YouTubeStructs
property content_html : String
property attachment : AttachmentType
property likes : Int32
property comments : Int32
property published : Time
def to_json(locale, json : JSON::Builder)
@ -52,6 +53,7 @@ module YouTubeStructs
json.field "contents", html_to_content(self.content_html)
json.field "attachment", self.attachment.to_json
json.field "likes", self.likes
json.field "comments", self.comments
json.field "published", self.published.to_unix
end
end

View file

@ -358,21 +358,27 @@ private module Parsers
end
end
likes = short_text_to_number(item_contents["voteCount"]["simpleText"].as_s.split(" ")[0]) # Youtube doesn't provide dislikes...
published = item_contents["publishedTimeText"]?.try &.["simpleText"]?.try { |t| decode_date(t.as_s) } || Time.local
# YouTube doesn't provide dislikes.
likes = short_text_to_number(item_contents["voteCount"]["simpleText"].as_s.split(" ")[0])
YouTubeStructs::CommunityPost.new({
author: author_name,
author_id: author_id,
author_thumbnail: author_thumbnail,
post_id: post_id,
content_html: contents,
attachment: attachment,
likes: likes,
published: published,
comments = item_contents.dig?("actionButtons", "commentActionButtonsRenderer",
"replyButton", "buttonRenderer", "text", "simpleText").try { |t| short_text_to_number(t.as_s) } || 0
published = item_contents["publishedTimeText"]?.try &.["simpleText"]?.try { |t| decode_date(t.as_s) } || Time.local
YouTubeStructs::CommunityPost.new({
author: author_name,
author_id: author_id,
author_thumbnail: author_thumbnail,
post_id: post_id,
content_html: contents,
attachment: attachment,
likes: likes,
comments: comments,
published: published,
})
end
end
end
end
# The following are the extractors for extracting an array of items from

View file

@ -74,24 +74,30 @@
<img style="margin-right:1em;margin-top:1em;width:90%" src="/ggpht<%= URI.parse(thread.author_thumbnail).request_target %>">
</div>
<div class="pure-u-20-24 pure-u-md-22-24">
<p><b><a href=<%="/channel/#{thread.author_id}"%>><%= thread.author %></a></p></b>
<p> <%= thread.content_html -%> </p>
<% # Handles attachments: %>
<% attachment = thread.attachment %>
<% if attachment.is_a? YouTubeStructs::VideoRenderer %>
<div style="position:relative;width:100%;height:0;padding-bottom:56.25%;margin-bottom:5px">
<iframe id='ivplayer' style='position:absolute;width:100%;height:100%;left:0;top:0' src='/embed/<%=attachment.id%>?autoplay=0' style='border:none;'></iframe>
</div>
<% elsif attachment.is_a? YouTubeStructs::PlaylistRenderer %>
<% elsif attachment.is_a? YouTubeStructs::CommunityPoll %>
<% elsif attachment.nil? %>
<% else %>
<img style="width: 90%" src="/ggpht<%= URI.parse(attachment).request_target%>"/>
<% end %>
<p id="likes"><i class="icon ion-ios-thumbs-up" style="margin-right: 5px;"></i><%= thread.likes %></p>
<div class="pure-u-1 pure-u-md-1-2">
<% if attachment.is_a? YouTubeStructs::VideoRenderer %>
<div style="position:relative;width:100%;height:0;padding-bottom:56.25%;margin-bottom:5px">
<iframe id='ivplayer' style='position:absolute;width:100%;height:100%;left:0;top:0' src='/embed/<%=attachment.id%>?autoplay=0' style='border:none;'></iframe>
</div>
<% elsif attachment.is_a? YouTubeStructs::PlaylistRenderer %>
<% elsif attachment.is_a? YouTubeStructs::CommunityPoll %>
<% elsif attachment.nil? %>
<% else %>
<img style="width: 90%" src="/ggpht<%= URI.parse(attachment).request_target%>"/>
<% end %>
</div>
<div class="pure-g pure-u-1"></div>
<div class="pure-menu pure-menu-horizontal">
<ul class="pure-menu-list">
<li class="pure-menu-item" id="likes" style="margin-right: 15px;"><i class="icon ion-ios-thumbs-up" style="margin-right: 5px;"></i><%= thread.likes %></li>
<li class="pure-menu-item" id="comments"><i class="icon ion-ios-chatbubbles" style="margin-right: 5px;"></i><%= thread.comments %></li>
</ul>
</div>
</div>
</div>
<%- end%>