@ -5,7 +5,7 @@ class ChannelSearchException < InfoException
end
end
end
end
def channel_search ( query , page , channel )
def channel_search ( query , page , channel ) : Array ( SearchItem )
response = YT_POOL . client & . get ( " /channel/ #{ channel } " )
response = YT_POOL . client & . get ( " /channel/ #{ channel } " )
if response . status_code == 404
if response . status_code == 404
@ -24,7 +24,7 @@ def channel_search(query, page, channel)
continuation_items = response_json [ " onResponseReceivedActions " ]?
continuation_items = response_json [ " onResponseReceivedActions " ]?
. try & . [ 0 ] [ " appendContinuationItemsAction " ] [ " continuationItems " ]
. try & . [ 0 ] [ " appendContinuationItemsAction " ] [ " continuationItems " ]
return 0 , [ ] of SearchItem if ! continuation_items
return [ ] of SearchItem if ! continuation_items
items = [ ] of SearchItem
items = [ ] of SearchItem
continuation_items . as_a . select ( & . as_h . has_key? ( " itemSectionRenderer " ) ) . each { | item |
continuation_items . as_a . select ( & . as_h . has_key? ( " itemSectionRenderer " ) ) . each { | item |
@ -32,17 +32,16 @@ def channel_search(query, page, channel)
. try { | t | items << t }
. try { | t | items << t }
}
}
return items . size , items
return items
end
end
def search ( query , search_params = produce_search_params ( content_type : " all " ) , region = nil )
def search ( query , search_params = produce_search_params ( content_type : " all " ) , region = nil ) : Array ( SearchItem )
return 0 , [ ] of SearchItem if query . empty?
return [ ] of SearchItem if query . empty?
client_config = YoutubeAPI :: ClientConfig . new ( region : region )
client_config = YoutubeAPI :: ClientConfig . new ( region : region )
initial_data = YoutubeAPI . search ( query , search_params , client_config : client_config )
initial_data = YoutubeAPI . search ( query , search_params , client_config : client_config )
items = extract_items ( initial_data )
return items. size , items
return extract_items( initial_data )
end
end
def produce_search_params ( page = 1 , sort : String = " relevance " , date : String = " " , content_type : String = " " ,
def produce_search_params ( page = 1 , sort : String = " relevance " , date : String = " " , content_type : String = " " ,
@ -217,7 +216,7 @@ def process_search_query(query, page, user, region)
search_query = ( query . split ( " " ) - operators ) . join ( " " )
search_query = ( query . split ( " " ) - operators ) . join ( " " )
if channel
if channel
count, items = channel_search ( search_query , page , channel )
items = channel_search ( search_query , page , channel )
elsif subscriptions
elsif subscriptions
if view_name
if view_name
items = PG_DB . query_all ( " SELECT id,title,published,updated,ucid,author,length_seconds FROM (
items = PG_DB . query_all ( " SELECT id,title,published,updated,ucid,author,length_seconds FROM (
@ -227,16 +226,14 @@ def process_search_query(query, page, user, region)
as document
as document
FROM #{view_name}
FROM #{view_name}
) v_search WHERE v_search . document @ @ plainto_tsquery ( $1 ) LIMIT 20 OFFSET $2 ; " , search_query, (page - 1) * 20, as: ChannelVideo)
) v_search WHERE v_search . document @ @ plainto_tsquery ( $1 ) LIMIT 20 OFFSET $2 ; " , search_query, (page - 1) * 20, as: ChannelVideo)
count = items . size
else
else
items = [ ] of ChannelVideo
items = [ ] of ChannelVideo
count = 0
end
end
else
else
search_params = produce_search_params ( page : page , sort : sort , date : date , content_type : content_type ,
search_params = produce_search_params ( page : page , sort : sort , date : date , content_type : content_type ,
duration : duration , features : features )
duration : duration , features : features )
count, items = search ( search_query , search_params , region ) . as ( Tuple )
items = search ( search_query , search_params , region )
end
end
# Light processing to flatten search results out of Categories.
# Light processing to flatten search results out of Categories.
@ -254,5 +251,5 @@ def process_search_query(query, page, user, region)
end
end
end
end
{ search_query , items_without_category . size , items_without_category , operators }
{ search_query , items_without_category , operators }
end
end