rubycdp / ferrum

Headless Chrome Ruby API
https://ferrum.rubycdp.com
MIT License
1.71k stars 123 forks source link

Youtube Keyboard Shortcuts Navigation #240

Closed daBee closed 2 years ago

daBee commented 2 years ago

I'm trying to mimic a Youtube stream setup. I can't get it to work. The window is closing for some reason.

ENV['BROWSER_PATH'] = '/Applications/RF_Utilities/Google Chrome.app/Contents/MacOS/Google Chrome'
b = Ferrum::Browser.new(headless: false, process_timeout: 10, window_size: [1024, 768])
b.go_to("https://www.youtube.com/watch?v=CwriDd8STdI")
sleep 3

## ==> Move to playback quality
8.times do |i|
  b.keyboard.type(:tab)
  sleep 0.3
end

## ==> Choose playback quality
b.keyboard.type(:Return)                        # issue is here, browser closing
b.keyboard.type(:down_arrow)                    # Is this proper syntax?
b.keyboard.type(:Enter)                             # synonymous for :Return?
8.times do |i|
  b.keyboard.type(:up_arrow)
  sleep 0.2
end
b.keyboard.type(:Enter)

b.mouse.move(x: 939, y: 150).down.up            # Rid Log-In dialog box
sleep 1

b.keyboard.type('t')                                 # theatre mode
sleep 1

The window closes for some reason. It's a bit of a navigation, but the tools are supposedly there.

What am I doing wrong? Sleeps are for the unit (Raspberry Pi 2 and Zero 2 W) to catch up and properly manage the browser stream.

Mifrill commented 2 years ago

Hi, @daBee , let's see closer.

b.keyboard.type(:down_arrow) # Is this proper syntax?

No, should be Down or down: https://github.com/rubycdp/ferrum/blob/006c5fc385596c218402de8b0d24dbe6739f1c72/lib/ferrum/keyboard.json#L279 https://github.com/rubycdp/ferrum/blob/006c5fc385596c218402de8b0d24dbe6739f1c72/lib/ferrum/keyboard.rb#L17 Same for b.keyboard.type(:up_arrow), should be Up or up: https://github.com/rubycdp/ferrum/blob/006c5fc385596c218402de8b0d24dbe6739f1c72/lib/ferrum/keyboard.json#L253 https://github.com/rubycdp/ferrum/blob/006c5fc385596c218402de8b0d24dbe6739f1c72/lib/ferrum/keyboard.rb#L16

b.keyboard.type(:Enter) # synonymous for :Return?

Correct, see: https://github.com/rubycdp/ferrum/blob/006c5fc385596c218402de8b0d24dbe6739f1c72/lib/ferrum/keyboard.rb#L12 return: "Enter", enter: "Enter" - aliases

b.keyboard.type(:Return) # issue is here

What is exact the issue?

Try to use FERRUM_DEBUG=true env variable for the scrip run and see the error trace.

Is BROWSER_PATH contain a browser with a logged user? if not then we have a possible block by Cookie policy popup: Selection_1050 Needs to run something like this to agreed:

      7.times do |i|
        b.keyboard.type(:tab)
        sleep 0.3
      end
      b.keyboard.type(:enter)
      sleep 3

As I understand, this script needs to enable theatre mode with specific quality, so you can try this:

    b = Ferrum::Browser.new(headless: true, process_timeout: 10, window_size: [1024, 768])
      b.go_to("https://www.youtube.com/watch?v=CwriDd8STdI")
      sleep 3
      7.times do |i|
        b.keyboard.type(:tab)
        sleep 0.7
      end
      b.keyboard.type(:enter)
      sleep 3
      ## ==> Move to playback quality
      17.times do |i|
        b.keyboard.type(:tab)
        sleep 0.9
      end
      b.keyboard.type(:return)
      sleep 0.3
      b.keyboard.type(:down)
      b.keyboard.type(:enter)
      8.times do |i|
        b.keyboard.type(:up)
        sleep 0.2
      end
      b.keyboard.type(:enter)
      b.keyboard.type('t')
      sleep 1

Apart from that, I think you can play with query params to open link with prepared settings, see for example: Video: https://www.youtube.com/embed/CwriDd8STdI?vq=hd1080&autoplay=1&mute=0 (mute=1 to disable sound) Dedicated link for chat: https://www.youtube.com/live_chat?v=CwriDd8STdI Source: https://stackoverflow.com/questions/23145257/is-there-a-way-to-link-someone-to-a-youtube-video-in-hd-1080p-quality as other option: use the extension https://browsersolution.com/always-open-youtube-videos-theater-mode-chrome

Sleeps are for the unit (Raspberry Pi 2 and Zero 2 W) to catch up and properly manage the browser stream.

could you rephrase, please? (didn't get what you are talking about)

daBee commented 2 years ago

Yeah, at the time I wasn't thinking about Youtube URL params and wanted to focus on exercising Ferrum capabilities. I've not encountered a cookie policy dialog box yet. I am meaning to NOT log into my account, so as an anon user. This comes up with a small login window top right, but I deal with that with code not presented here. Seems that doesn't always present itself, and now Youtube is asking to use my microphone, which is odd. At this point, maybe it is better to sign in, to avoid all these pop-up dialogs.

The issue was that the browser was closing (edited post). Now I'm juggling several issues. I know this isn't a Ferrum issue, but any insight how to avoid these?

daBee commented 2 years ago

I'm finding that this small dialog SOMETIMES shows up. As well, tabbing alongside the bottom of the video, the CC option sometimes shows up, which throws off the tab count. I'm also finding &mode=theatre does not apply if not logged in.

Screen Shot 2022-01-13 at 4 21 27 PM

daBee commented 2 years ago

Update: Solution was right in front of my nose. After typing t for theatre mode (params don't work on non-signed-in users), I literally click on the new location for the settings gear. Then choose the resolution, et voila.

b = Ferrum::Browser.new(headless: false, process_timeout: 10, window_size: [1024, 1600])
b.go_to("https://www.youtube.com/watch?v=CwriDd8STdI")
sleep 3

b.keyboard.type('t')                                    # theatre video
puts "large size now"
sleep 2

b.mouse.move(x: 834, y: 611).down.up       # click settings button
sleep 0.5
b.keyboard.type(:down)                      # down menu option
sleep 0.5
b.keyboard.type(:Enter)                     # select this menu option
sleep 0.5
8.times do |i|
  b.keyboard.type(:up)                      # move up to highest res
  sleep 0.5
end
b.keyboard.type(:Enter)                     # select highest res

sleep 6