Use try instead of ternary

This commit is contained in:
Omar Roth 2018-03-14 18:06:21 -05:00
parent c8a798e13c
commit e8013c6d5c
2 changed files with 8 additions and 5 deletions

View File

@ -131,10 +131,12 @@ def fetch_video(id, client)
views = info["view_count"].to_i64 views = info["view_count"].to_i64
likes = html.xpath_node(%q(//button[@title="I like this"]/span)) likes = html.xpath_node(%q(//button[@title="I like this"]/span))
likes = likes ? likes.content.delete(",").to_i : 0 likes = likes.try &.content.delete(",").try &.to_i
likes ||= 0
dislikes = html.xpath_node(%q(//button[@title="I dislike this"]/span)) dislikes = html.xpath_node(%q(//button[@title="I dislike this"]/span))
dislikes = dislikes ? dislikes.content.delete(",").to_i : 0 dislikes = dislikes.try &.content.delete(",").try &.to_i
dislikes ||= 0
description = html.xpath_node(%q(//p[@id="eow-description"])) description = html.xpath_node(%q(//p[@id="eow-description"]))
description = description ? description.to_xml : "" description = description ? description.to_xml : ""

View File

@ -187,8 +187,8 @@ get "/watch" do |env|
fmt_stream = [] of HTTP::Params fmt_stream = [] of HTTP::Params
video.info["url_encoded_fmt_stream_map"].split(",") do |string| video.info["url_encoded_fmt_stream_map"].split(",") do |string|
if !string.empty? if !string.empty?
fmt_stream << HTTP::Params.parse(string) fmt_stream << HTTP::Params.parse(string)
end end
end end
adaptive_fmts = [] of HTTP::Params adaptive_fmts = [] of HTTP::Params
@ -271,7 +271,8 @@ get "/search" do |env|
next next
end end
page = env.params.query["page"]? && env.params.query["page"].to_i? ? env.params.query["page"].to_i : 1 page = env.params.query["page"]?.try &.to_i
page ||= 1
client = get_client(youtube_pool) client = get_client(youtube_pool)