Skip to content

Commit

Permalink
Optimize YouTube searching
Browse files Browse the repository at this point in the history
  • Loading branch information
AnonymousX86 committed Aug 27, 2022
1 parent dff8f54 commit 3e117f5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
16 changes: 11 additions & 5 deletions Spoyt/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
ActivityType, Activity, NotFound, Forbidden

from Spoyt.spotify_api import search_spotify, model_track, TrackEmbed
from Spoyt.youtube_api import create_youtube, search_youtube
from Spoyt.youtube_api import find_youtube_id


def main():
Expand All @@ -24,7 +24,6 @@ async def on_ready():
type=ActivityType.listening
)
)
client.youtube = create_youtube()

@client.event
async def on_message(message: Message):
Expand Down Expand Up @@ -63,18 +62,25 @@ async def on_message(message: Message):
color=Color.blurple()
)])

video_id = search_youtube(
youtube=client.youtube,
video_id_found, result = find_youtube_id(
query='{} {}'.format(track.name, ' '.join(track.artists))
)

if not video_id_found:
await msg.edit(embeds=[track_embed, Embed(
title='Video not found',
description=result,
color=Color.dark_red()
)])
return

await msg.edit(embeds=[track_embed, Embed(
title='Best YouTube result',
color=Color.blurple()
)])

await message.channel.send(
content=f'https://www.youtube.com/watch?v={video_id}'
content=f'https://www.youtube.com/watch?v={result}'
)

client.run(getenv('BOT_TOKEN'))
Expand Down
48 changes: 23 additions & 25 deletions Spoyt/youtube_api.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
# -*- coding: utf-8 -*-
from json import loads as json_loads
from os import getenv

import google_auth_oauthlib.flow
import googleapiclient.discovery
import requests


def create_youtube() -> googleapiclient.discovery:
flow = google_auth_oauthlib.flow.InstalledAppFlow. \
from_client_secrets_file(
getenv('YOUTUBE_CLIENT_SECRET_FILE'),
['https://www.googleapis.com/auth/youtube.force-ssl']
)
credentials = flow.run_local_server()
youtube = googleapiclient.discovery.build(
'youtube',
'v3',
credentials=credentials
def find_youtube_id(query: str) -> [bool, str]:
yt_r = requests.get(
'https://www.googleapis.com/youtube/v3/search'
'?key={}'
'&part=snippet'
'&maxResults=1'
'&q={}'.format(
# getenv('YOUTUBE_API_KEY'),
'',
query
)
)
return youtube


def search_youtube(youtube: googleapiclient.discovery, query: str) -> str:
yt_req = youtube.search().list(
part='snippet',
maxResults=1,
q=query,
type='video'
)
yt_res = yt_req.execute()
return yt_res['items'][0]['id']['videoId']
content = json_loads(yt_r.content)
breakpoint()
if (error_code := content['error']['code']) == 200:
data = content['items'][0]['id']['videoId']
elif error_code == 403:
data = 'Bot is not set properly.' \
' Ask the bot owner for further information.'
else:
data = content['error']['message']
return [error_code == 200, data]
3 changes: 0 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
discord.py
requests
google-api-python-client
google-auth-oauthlib
google-auth-httplib2
spotipy

0 comments on commit 3e117f5

Please sign in to comment.