Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search for youtube videos #44

Open
DivanX10 opened this issue Oct 15, 2021 · 1 comment
Open

Add search for youtube videos #44

DivanX10 opened this issue Oct 15, 2021 · 1 comment

Comments

@DivanX10
Copy link

Could you add to the Youtube integration the ability to search for videos by any query and a sensor that will output code variants based on the search.

Why is this necessary? There is a speaker with a built-in voice assistant and you can ask the voice assistant to do something. Turning to the voice assistant with a request to find something, an event arrives in the Home Assistant

{
    "event_type": "yandex_intent",
    "data": {
        "text": "Alice find documentaries about space",
        "command": "Alice find documentaries about space",
        "intent": "YANDEX.BOOK.SEARCH",
        "book": "documentaries about space"
    },
    "origin": "LOCAL",
    "time_fired": "2021-10-14T14:20:02.517357+00:00",
    "context": {
        "id": "e802961a0196af60036511eb117d398f",
        "parent_id": null,
        "user_id": null
    }
}

Based on this event, a trigger is used

platform: event
event_type: yandex_intent
event_data:
  intent: YANDEX.BOOK.SEARCH

In the action, a link is indicated with the search for the desired topic and a youtube video search is launched on the TV

service: webostv.command
data:
  entity_id: media_player.tv_bedroom
  command: system.launcher/open
  payload:
    target: >-
      https://www.youtube.com/results?search_query={{ trigger.event.data.book|replace(" ", "+") }}

Thus we get a search result with the topic documentaries about space
www.youtube.com/results?search_query=documentaries+about+space
image

The problem is that not all Smart TVs are able to work with such a code https://www.youtube.com/results?search_query ={{search|replace(" ", "+")}}, and to include any content, you need to specify the content code https://www.youtube.com/watch?v=DKM9d5CZUxk . Below I have given two examples and each example is working, but for example in LG TVs. Does not work https://www.youtube.com/results?search_query ={{ trigger.event.data.book|replace(" ", "+") }}, and only DKM9d5CZUxk works (https://www.youtube.com/watch?v=DKM9d5CZUxk ) . And having created a service that will receive input text on a given topic and video options will be issued in the sensor or there will be one selected code that was selected by random method, then it can be used in TV services to launch videos through a voice assistant.

A variant of work with the indication of the video code for LG TVs

service: webostv.command
data:
  command: system.launcher/launch
  entity_id: media_player.tv_lg_bedroom
  payload:
    id: youtube.leanback.v4
    contentId: DKM9d5CZUxk

The option of specifying the code on request for Android-based TVs

service: media_player.play_media
data:
  media_content_type: video/youtube
  media_content_id: >-
    https://www.youtube.com/results?search_query={{ trigger.event.data.book|replace(" ", "+") }}
target:
  entity_id: media_player.tv_lg_bedroom

For understanding, a link to the source Search for Youtube Videos

Where the author of the article points to a variant of the code that extracts the codes on request

import urllib.request
import re

html = urllib.request.urlopen("https://www.youtube.com/results?search_query=mozart")
video_ids = re.findall(r"watch\?v=(\S{11})", html.read().decode())
print(video_ids)

and the search result is given, where there will be several variants of the code to launch the video

['shoVsQhou-8', 'shoVsQhou-8', 'Rb0UmrCXxVA', 'Rb0UmrCXxVA', 'iUohO2MSot8', 'iUohO2MSot8', 'QEDZd066a2k', 'QEDZd066a2k', 'QHl6wYCwlcQ', 'QHl6wYCwlcQ',
......
(not all identifiers included to keep the output small)
...
'FpK1tjbeeA0', 'FpK1tjbeeA0', 'sjTLIW-qx_A', 'sjTLIW-qx_A', 'pB2p_r5Gvs8']

To start a video, the following code is usually specified
https://www.youtube.com/watch?v=Rb0UmrCXxVA

If you use several code variants, you can selectively run the video
https://www.youtube.com/watch?v={{ ['shoVsQhou-8', 'shoVsQhou-8', 'Rb0UmrCXxVA', 'Rb0UmrCXxVA', 'iUohO2MSot8', 'iUohO2MSot8', 'QEDZd066a2k', 'QEDZd066a2k', 'QHl6wYCwlcQ', 'QHl6wYCwlcQ']|random }}
or
https://www.youtube.com/watch?v={{ states("sensor.search_youtube_video")}}

Of course, you can use this option, but it does not work in all TV
https://www.youtube.com/results?search_query="{{search|replace(" ", "+")}}"

This code is pulled from this source. Here we are talking about pulling out audio, but you can also output video

@commands.command(name='test')
    async def test(self, ctx):

        search = "morpheus tutorials discord bot python"

        if ctx.message.author.voice == None:
            await ctx.send(embed=Embeds.txt("No Voice Channel", "You need to be in a voice channel to use this command!", ctx.author))
            return

        channel = ctx.message.author.voice.channel

        voice = discord.utils.get(ctx.guild.voice_channels, name=channel.name)

        voice_client = discord.utils.get(self.client.voice_clients, guild=ctx.guild)

        if voice_client == None:
            voice_client = await voice.connect()
        else:
            await voice_client.move_to(channel)

        search = search.replace(" ", "+")

        html = urllib.request.urlopen("https://www.youtube.com/results?search_query=" + search)
        video_ids = re.findall(r"watch\?v=(\S{11})", html.read().decode())

        
        await ctx.send("https://www.youtube.com/watch?v=" + video_ids[0])

        song = pafy.new(video_ids[0])  # creates a new pafy object

        audio = song.getbestaudio()  # gets an audio source

        source = FFmpegPCMAudio(audio.url, **FFMPEG_OPTIONS)  # converts the youtube audio source into a source discord can use

        voice_client.play(source)  # play the source 
@pinkywafer
Copy link
Collaborator

Currently this is beyond the intended scope, but I don't see why something like this couldn't be added within this integration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants