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

View file

@ -358,21 +358,27 @@ private module Parsers
end end
end end
likes = short_text_to_number(item_contents["voteCount"]["simpleText"].as_s.split(" ")[0]) # Youtube doesn't provide dislikes... # YouTube doesn't provide dislikes.
published = item_contents["publishedTimeText"]?.try &.["simpleText"]?.try { |t| decode_date(t.as_s) } || Time.local likes = short_text_to_number(item_contents["voteCount"]["simpleText"].as_s.split(" ")[0])
YouTubeStructs::CommunityPost.new({ comments = item_contents.dig?("actionButtons", "commentActionButtonsRenderer",
author: author_name, "replyButton", "buttonRenderer", "text", "simpleText").try { |t| short_text_to_number(t.as_s) } || 0
author_id: author_id,
author_thumbnail: author_thumbnail, published = item_contents["publishedTimeText"]?.try &.["simpleText"]?.try { |t| decode_date(t.as_s) } || Time.local
post_id: post_id,
content_html: contents, YouTubeStructs::CommunityPost.new({
attachment: attachment, author: author_name,
likes: likes, author_id: author_id,
published: published, author_thumbnail: author_thumbnail,
post_id: post_id,
content_html: contents,
attachment: attachment,
likes: likes,
comments: comments,
published: published,
}) })
end end
end end
end end
# The following are the extractors for extracting an array of items from # 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 %>"> <img style="margin-right:1em;margin-top:1em;width:90%" src="/ggpht<%= URI.parse(thread.author_thumbnail).request_target %>">
</div> </div>
<div class="pure-u-20-24 pure-u-md-22-24"> <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><b><a href=<%="/channel/#{thread.author_id}"%>><%= thread.author %></a></p></b>
<p> <%= thread.content_html -%> </p> <p> <%= thread.content_html -%> </p>
<% # Handles attachments: %> <% # Handles attachments: %>
<% attachment = thread.attachment %> <% attachment = thread.attachment %>
<% if attachment.is_a? YouTubeStructs::VideoRenderer %> <div class="pure-u-1 pure-u-md-1-2">
<div style="position:relative;width:100%;height:0;padding-bottom:56.25%;margin-bottom:5px"> <% if attachment.is_a? YouTubeStructs::VideoRenderer %>
<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 style="position:relative;width:100%;height:0;padding-bottom:56.25%;margin-bottom:5px">
</div> <iframe id='ivplayer' style='position:absolute;width:100%;height:100%;left:0;top:0' src='/embed/<%=attachment.id%>?autoplay=0' style='border:none;'></iframe>
<% elsif attachment.is_a? YouTubeStructs::PlaylistRenderer %> </div>
<% elsif attachment.is_a? YouTubeStructs::CommunityPoll %> <% elsif attachment.is_a? YouTubeStructs::PlaylistRenderer %>
<% elsif attachment.nil? %> <% elsif attachment.is_a? YouTubeStructs::CommunityPoll %>
<% else %> <% elsif attachment.nil? %>
<img style="width: 90%" src="/ggpht<%= URI.parse(attachment).request_target%>"/> <% else %>
<% end %> <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>
<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>
</div> </div>
<%- end%> <%- end%>