@@ -500,7 +500,11 @@ module YoutubeAPI
500
500
data[" params" ] = params
501
501
end
502
502
503
- return self ._post_json(" /youtubei/v1/player" , data, client_config)
503
+ if ! CONFIG .invidious_companion.empty?
504
+ return self ._post_invidious_companion(" /youtubei/v1/player" , data)
505
+ else
506
+ return self ._post_json(" /youtubei/v1/player" , data, client_config)
507
+ end
504
508
end
505
509
506
510
# ###################################################################
@@ -615,19 +619,12 @@ module YoutubeAPI
615
619
616
620
headers = HTTP ::Headers {
617
621
" Content-Type" => " application/json; charset=UTF-8" ,
622
+ " Accept-Encoding" => " gzip, deflate" ,
618
623
" x-goog-api-format-version" => " 2" ,
619
624
" x-youtube-client-name" => client_config.name_proto,
620
625
" x-youtube-client-version" => client_config.version,
621
626
}
622
627
623
- if CONFIG .invidious_companion && endpoint == " /youtubei/v1/player"
624
- headers[" Authorization" ] = " Bearer " + CONFIG .hmac_key
625
- end
626
-
627
- if ! CONFIG .invidious_companion
628
- headers[" Accept-Encoding" ] = " gzip, deflate"
629
- end
630
-
631
628
if user_agent = client_config.user_agent
632
629
headers[" User-Agent" ] = user_agent
633
630
end
@@ -641,37 +638,18 @@ module YoutubeAPI
641
638
LOGGER .trace(" YoutubeAPI: ClientConfig: #{ client_config } " )
642
639
LOGGER .trace(" YoutubeAPI: POST data: #{ data } " )
643
640
644
- invidious_companion_urls = CONFIG .invidious_companion
645
-
646
641
# Send the POST request
647
- if invidious_companion_urls && endpoint == " /youtubei/v1/player"
648
- begin
649
- invidious_companion_response = make_client(URI .parse(invidious_companion_urls.sample),
650
- & .post(endpoint, headers: headers, body: data.to_json))
651
- body = invidious_companion_response.body
652
- if (invidious_companion_response.status_code != 200 )
653
- raise Exception .new(" status code: " + invidious_companion_response.status_code.to_s + " and body: " + body)
654
- end
655
- rescue ex
656
- raise InfoException .new(" Error while communicating with Invidious companion: " + (ex.message || " no extra info found" ))
657
- end
658
- else
659
- body = YT_POOL .client() do |client |
660
- client.post(url, headers: headers, body: data.to_json) do |response |
661
- if response.status_code != 200
662
- raise InfoException .new(" Error: non 200 status code. Youtube API returned \
663
- status code #{ response.status_code } . See <a href=\" https://docs.invidious.io/youtube-errors-explained/\" > \
664
- https://docs.invidious.io/youtube-errors-explained/</a> for troubleshooting." )
665
- end
666
- self ._decompress(response.body_io, response.headers[" Content-Encoding" ]?)
642
+ body = YT_POOL .client() do |client |
643
+ client.post(url, headers: headers, body: data.to_json) do |response |
644
+ if response.status_code != 200
645
+ raise InfoException .new(" Error: non 200 status code. Youtube API returned \
646
+ status code #{ response.status_code } . See <a href=\" https://docs.invidious.io/youtube-errors-explained/\" > \
647
+ https://docs.invidious.io/youtube-errors-explained/</a> for troubleshooting." )
667
648
end
649
+ self ._decompress(response.body_io, response.headers[" Content-Encoding" ]?)
668
650
end
669
651
end
670
652
671
- if body.nil? && CONFIG .invidious_companion
672
- raise InfoException .new(" Error while communicating with Invidious companion: no response data." )
673
- end
674
-
675
653
# Convert result to Hash
676
654
initial_data = JSON .parse(body).as_h
677
655
@@ -692,6 +670,53 @@ module YoutubeAPI
692
670
return initial_data
693
671
end
694
672
673
+ # ###################################################################
674
+ # _post_invidious_companion(endpoint, data)
675
+ #
676
+ # Internal function that does the actual request to Invidious companion
677
+ # and handles errors.
678
+ #
679
+ # The requested data is an endpoint (URL without the domain part)
680
+ # and the data as a Hash object.
681
+ #
682
+ def _post_invidious_companion (
683
+ endpoint : String ,
684
+ data : Hash
685
+ ) : Hash (String , JSON ::Any )
686
+ headers = HTTP ::Headers {
687
+ " Content-Type" => " application/json; charset=UTF-8" ,
688
+ " Accept-Encoding" => " gzip" ,
689
+ " Authorization" => " Bearer " + CONFIG .invidious_companion_key,
690
+ }
691
+
692
+ # Logging
693
+ LOGGER .debug(" Invidious companion: Using endpoint: \" #{ endpoint } \" " )
694
+ LOGGER .trace(" Invidious companion: POST data: #{ data } " )
695
+
696
+ # Send the POST request
697
+
698
+ begin
699
+ response = make_client(CONFIG .invidious_companion.sample,
700
+ & .post(endpoint, headers: headers, body: data.to_json))
701
+ body = self ._decompress(response.body_io, response.headers[" Content-Encoding" ]?)
702
+ if (response.status_code != 200 )
703
+ raise Exception .new(" Error while communicating with Invidious companion: \
704
+ status code: " + response.status_code.to_s + " and body: " + body)
705
+ end
706
+ rescue ex
707
+ raise InfoException .new(" Error while communicating with Invidious companion: " + (ex.message || " no extra info found" ))
708
+ end
709
+
710
+ if body.nil?
711
+ raise InfoException .new(" Error while communicating with Invidious companion: no response data." )
712
+ end
713
+
714
+ # Convert result to Hash
715
+ initial_data = JSON .parse(body).as_h
716
+
717
+ return initial_data
718
+ end
719
+
695
720
# ###################################################################
696
721
# _decompress(body_io, headers)
697
722
#
0 commit comments