Add 'published' to video component

This commit is contained in:
Omar Roth 2018-08-04 10:22:12 -05:00
parent 2fe3d308f9
commit 3dcb6766fc
2 changed files with 38 additions and 17 deletions

View File

@ -354,31 +354,24 @@ def search(query, page = 1)
html.xpath_nodes(%q(//ol[@class="item-section"]/li)).each do |item| html.xpath_nodes(%q(//ol[@class="item-section"]/li)).each do |item|
root = item.xpath_node(%q(div[contains(@class,"yt-lockup-video")]/div)) root = item.xpath_node(%q(div[contains(@class,"yt-lockup-video")]/div))
if root if !root
id = root.xpath_node(%q(div[contains(@class,"yt-lockup-thumbnail")]/a/@href)) next
if id
id = id.content.lchop("/watch?v=")
end end
id ||= ""
title = root.xpath_node(%q(div[@class="yt-lockup-content"]/h3/a)) id = root.xpath_node(%q(.//div[contains(@class,"yt-lockup-thumbnail")]/a/@href)).not_nil!.content.lchop("/watch?v=")
if title
title = title.content
end
title ||= ""
author = root.xpath_node(%q(div[@class="yt-lockup-content"]/div/a)) title = root.xpath_node(%q(.//div[@class="yt-lockup-content"]/h3/a)).not_nil!.content
if author
author = root.xpath_node(%q(.//div[@class="yt-lockup-content"]/div/a)).not_nil!
ucid = author["href"].rpartition("/")[-1] ucid = author["href"].rpartition("/")[-1]
author = author.content author = author.content
end
author ||= ""
ucid ||= ""
video = ChannelVideo.new(id, title, Time.now, Time.now, ucid, author) published = root.xpath_node(%q(.//ul[@class="yt-lockup-meta-info"]/li[1])).not_nil!.content
published = decode_date(published)
video = ChannelVideo.new(id, title, published, Time.now, ucid, author)
videos << video videos << video
end end
end
return videos return videos
end end
@ -986,6 +979,31 @@ def decode_date(string : String)
return Time.now - delta return Time.now - delta
end end
def recode_date(time : Time)
span = Time.now - time
if span.total_days > 365.0
span = {span.total_days / 365, "year"}
elsif span.total_days > 30.0
span = {span.total_days / 30, "month"}
elsif span.total_days > 7.0
span = {span.total_days / 7, "week"}
elsif span.total_hours > 24.0
span = {span.total_days, "day"}
elsif span.total_minutes > 60.0
span = {span.total_hours, "hour"}
else
span = {0, "units"}
end
span = {span[0].to_i, span[1]}
if span[0] > 1
span = {span[0], span[1] + "s"}
end
return span.join(" ")
end
def produce_playlist_url(ucid, index) def produce_playlist_url(ucid, index)
ucid = ucid.lchop("UC") ucid = ucid.lchop("UC")
ucid = "VLUU" + ucid ucid = "VLUU" + ucid

View File

@ -10,5 +10,8 @@
<p> <p>
<b><a style="width:100%;" href="/channel/<%= video.ucid %>"><%= video.author %></a></b> <b><a style="width:100%;" href="/channel/<%= video.ucid %>"><%= video.author %></a></b>
</p> </p>
<p>
<h5>Shared <%= recode_date(video.published) %> ago</h5>
</p>
</div> </div>
</div> </div>