jaseg / python-mpv

Python interface to the awesome mpv media player
https://git.jaseg.de/python-mpv.git
Other
556 stars 69 forks source link

How to set User-Agent? #284

Closed Loukious closed 4 months ago

Loukious commented 4 months ago

I tried both player['user-agent'] = "MyCustomUserAgent/1.0" and player = mpv.MPV(log_handler=my_log, ytdl=True, user_agent="MyCustomUserAgent/1.0", loglevel='debug') and both don't seem to modify the user-agent. Is there a solution for this?

jaseg commented 4 months ago

When streaming something that is handled by mpv's youtube-dl backend, there are two places where you can set the user agent:

  1. You can set the user agent that youtube-dl will use to fetch the page and extract the stream URL. That you set by passing mpv.MPV(..., ytdl_raw_options="user-agent=foobar").
  2. You can set the user agent that mpv will later use to fetch the stream URL that youtube-dl has extracted. That you set by passing mpv.MPV(..., user_agent="foobar").

You probably want to use both.

Loukious commented 4 months ago

When streaming something that is handled by mpv's youtube-dl backend, there are two places where you can set the user agent:

  1. You can set the user agent that youtube-dl will use to fetch the page and extract the stream URL. That you set by passing mpv.MPV(..., ytdl_raw_options="user-agent=foobar").
  2. You can set the user agent that mpv will later use to fetch the stream URL that youtube-dl has extracted. That you set by passing mpv.MPV(..., user_agent="foobar").

You probably want to use both.

Thank you that seems to be the case. But now I'm facing another issue, the user-agent I'm using contains some non ASCII characters image which seems to cause this error: ValueError: ('Invalid value for mpv option', -7, (<MpvHandle object at 0x0000021D7D603350>, b'ytdl-raw-options', b'user-agent=\xe2\x80\x8e Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.37 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.37 \xe2\x80\x8e \xe2\x80\x81'))

jaseg commented 4 months ago

The non-ascii characters there are unicode bidi control characters. You should delete them, they don't belong in the user agent string.

Loukious commented 4 months ago

The non-ascii characters there are unicode bidi control characters. You should delete them, they don't belong in the user agent string.

I'm aware but they are required by the server (that I have no access to in order to change the user-agent whitelist). Any user-agent other than that is blocked.

jaseg commented 4 months ago

Oof, lol. I'll push a release tonight that will add support for arbitrary binary data in properties. In the meantime, you can use the current main branch with the code below.

player = mpv.MPV()
player.user_agent = ua
player.ytdl_raw_options = {'user-agent': ua}
Loukious commented 4 months ago

Oof, lol. I'll push a release tonight that will add support for arbitrary binary data in properties. In the meantime, you can use the current main branch with the code below.

player = mpv.MPV()
player.user_agent = ua
player.ytdl_raw_options = {'user-agent': ua}

Works great. Thank you.