How to play media_extractor.play_media through the API?

I am having trouble calling the media extractor service. This works in the front end dev

{"entity_id": "media_player.speaker","media_content_id":"https://www.youtube.com/watch?v=IsgM7HjEVKU","media_content_type": "music"}

but these don’t in python script

  url = "http://192.168.2.181:8125/api/service/media_extractor/play_media"
  url = "http://192.168.2.181:8125/api/service/media_extractor.play_media"

  data = {"entity_id": "media_player.speaker","media_content_id":"https://www.youtube.com/watch?v=IsgM7HjEVKU","media_content_type": "music"}
  r = requests.post(url, data=json.dumps(data), headers=headers)

any help would be appreciated

Are you referring to calling a service with that information?

You may need to actually place them in the ‘data’. request.post just pushes data through. My guess is that home assistant may be looking for the ‘data’ key. If that’s the case, you’d need to do the following:

data = {"data":{"entity_id": "media_player.speaker","media_content_id":"https://www.youtube.com/watch?v=IsgM7HjEVKU","media_content_type": "music"}}

Are you getting any errors on the home assistant side in the logs? I’d expect something to appear in the logs when you attempt to post and it has the wrong information.

I don’t get any errors on the HA logs.

When I go to http://192.168.2.181:8125/api/services to get a list of services, the only extractor one is this, can’t make out if this helps. (it appears there are no services setup yet)

{“domain”: “media_extractor”, “services”: {“play_media”: {“description”: “”, “fields”: {}}}}, {“domain”: “tts”, “services”: {“amazon_polly_say”: {“description”: “”, “fields”: {}}, “clear_cache”: {“description”: “Remove cache files and RAM cache.”, “fields”: {}}}}]

Not sure, I’d have to play around with it myself in order to give you a definitive answer.

I think it might come in the next upgrade.

{“entity_id”: “media_player.mpd”, “media_content_id”: “https://www.youtube.com/watch?v=gHSN4DmjPas”, “media_content_type”: “music”}

Give this a whirl:

hass.services.call('media_extractor', 'play_media', {"entity_id": "media_player.speaker","media_content_id":"https://www.youtube.com/watch?v=IsgM7HjEVKU","media_content_type": "music"}, False)

Thanks, I got it to work in python script on the same machine as the media extractor. Is it possible to run in python command line and not in python scripts and to execute on a different HA? If not, I guess I need to create a python script that will accept a url and try to pass that url from another machine through the api / python scripts. It will essentially create a python script to replace media extractor so it can run from the api.

Good question. I do not know. You could use MQTT to pass the information back and forth. If you are trying to post this information to home assistant, then you already have all the building blocks. You just need to make the code.

Hi,
I got he media_extractor working, and it is really great. The script below streams the Norwegian channel NRK1. However, when streamed it is 3 hours back in time! If I open the same link in VLC player it streams live (at current time). I tried setting the HA time_zone to different value with no luck / change. Any ideas on what is causing this, and how to fix it?

Looks like the time in HA is ok.
image
image

'1542910317131':
  alias: MediaExtractorNRK1
  sequence:
  - data:
      media_content_id: https://httpcache0-47115-httpcache0.dna.qbrick.com/47115-cachelive0/21/0/hls/nrk1/1955020115-123787468-prog_index.m3u8
      media_content_type: video/youtube
    entity_id: media_player.tv
    service: media_extractor.play_media

To somewhat answer my own question… by doing a media seek above 3 hours (used 3600*4) - i got to the live part:-) So I guess I will just add this after media_extractor starts playing.

'1542969354014':
  alias: MediaSeek
  sequence:
  - alias: ''
    data:
      entity_id: media_player.tv
      seek_position: '14400'
    service: media_player.media_seek