Skip to content

Commit

Permalink
Merge pull request #234 from DuckBoss/youtube-plugin-search-fix
Browse files Browse the repository at this point in the history
Fix index out of range error and new metadata option
  • Loading branch information
DuckBoss authored Jul 13, 2020
2 parents 5190a9d + 43a13bd commit feba2aa
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 13 deletions.
2 changes: 1 addition & 1 deletion JJMumbleBot/lib/resources/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,5 @@
###########################################################################
# BOT META INFORMATION STRINGS
META_NAME = "JJMumbleBot"
META_VERSION = "3.1.2"
META_VERSION = "3.1.3"
###########################################################################
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
https://raw.githubusercontent.com/joetats/youtube_search/master/LICENSE

MIT License

Copyright (c) 2019 joe tats

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 3 additions & 1 deletion JJMumbleBot/plugins/extensions/youtube/metadata.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[Plugin Information]
PluginVersion = 3.1.1
PluginVersion = 3.1.3
PluginName = Youtube
PluginDescription = The Youtube plugin plays youtube clips with a queue system.
PluginLanguage = EN
Expand Down Expand Up @@ -31,6 +31,8 @@ VLCDirectory = vlc
VLCRunQuiet = True
; Youtube queues autoplay by default, change to false to disable this feature.
AutoPlay = True
; Max number of search results to be shown by the !yt command.
MaxSearchLength = 10
; Max video length allowed by direct links to youtube queues.
MaxVideoLength = 7000
; Max youtube queue length.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# YOTUUBE CONFIG PARAMETER STRINGS
P_YT_VLC_QUIET = 'VLCRunQuiet'
P_YT_AUTO_PLAY = 'AutoPlay'
P_YT_MAX_SEARCH_LEN = 'MaxSearchLength'
P_YT_MAX_VID_LEN = 'MaxVideoLength'
P_YT_MAX_QUE_LEN = 'MaxQueueLength'
P_YT_DEF_VOL = 'DefaultVolume'
Expand Down
19 changes: 9 additions & 10 deletions JJMumbleBot/plugins/extensions/youtube/utility/youtube_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from JJMumbleBot.plugins.extensions.youtube.resources.strings import *
from JJMumbleBot.lib.resources.strings import *
from JJMumbleBot.lib.utils import runtime_utils
from JJMumbleBot.plugins.extensions.youtube.utility.youtube_search import YoutubeSearch
import requests
from PIL import Image
from bs4 import BeautifulSoup
Expand Down Expand Up @@ -280,22 +281,20 @@ def stop_audio():
GS.audio_dni = (False, None)


def get_vid_list(search):
url = "https://www.youtube.com/results?search_query=" + search.replace(" ", "+")
req = requests.get(url)
html = req.text
soup = BeautifulSoup(html, 'html.parser')
all_searches = soup.findAll(attrs={'class': 'yt-uix-tile-link'})
def get_vid_list(search: str, max_results: int):
search_results_list = []
for i in range(10):
search_dict = {"title": all_searches[i]['title'], 'href': all_searches[i]['href']}
search_results_list.append(search_dict)
search_results = YoutubeSearch(search, max_results=max_results).to_dict()
print(search_results)
for i in range(max_results):
search_results_list.append(search_results[i])
return search_results_list


def get_choices(all_searches):
if len(all_searches) == 0:
return None
list_urls = f"<font color='{GS.cfg[C_PGUI_SETTINGS][P_TXT_HEAD_COL]}'>Search Results:</font><br>"
for i in range(10):
for i, item in enumerate(all_searches):
completed_url = "https://www.youtube.com" + all_searches[i]['href']
list_urls += f"<font color='{GS.cfg[C_PGUI_SETTINGS][P_TXT_IND_COL]}'>[{i}]</font> - <a href='{completed_url}'>[{all_searches[i]['title']}]</a><br>"
return list_urls
Expand Down
82 changes: 82 additions & 0 deletions JJMumbleBot/plugins/extensions/youtube/utility/youtube_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""
THE FOLLOWING CODE HAS BEEN MODIFIED TO FIT THE PURPOSES OF THIS PROJECT
License Link: https://raw.githubusercontent.com/joetats/youtube_search/master/LICENSE
MIT License
Copyright (c) 2019 joe tats
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

import requests
import urllib.parse
import json


class YoutubeSearch:
def __init__(self, search_terms: str, max_results=None):
self.search_terms = search_terms
self.max_results = max_results
self.videos = self.search()

def search(self):
encoded_search = urllib.parse.quote(self.search_terms)
BASE_URL = "https://youtube.com"
url = f"{BASE_URL}/results?search_query={encoded_search}"
response = requests.get(url).text
while 'window["ytInitialData"]' not in response:
response = requests.get(url).text
results = self.parse_html(response)
if self.max_results is not None and len(results) > self.max_results:
return results[: self.max_results]
return results

def parse_html(self, response):
results = []
start = (
response.index('window["ytInitialData"]')
+ len('window["ytInitialData"]')
+ 3
)
end = response.index("};", start) + 1
json_str = response[start:end]
data = json.loads(json_str)

videos = data["contents"]["twoColumnSearchResultsRenderer"]["primaryContents"][
"sectionListRenderer"
]["contents"][0]["itemSectionRenderer"]["contents"]

for video in videos:
res = {}
if "videoRenderer" in video.keys():
video_data = video["videoRenderer"]
res["title"] = video_data["title"]["runs"][0]["text"]
res["href"] = video_data["navigationEndpoint"]["commandMetadata"][
"webCommandMetadata"
]["url"]
results.append(res)
return results

def to_dict(self):
return self.videos

def to_json(self):
return json.dumps({"videos": self.videos})
9 changes: 8 additions & 1 deletion JJMumbleBot/plugins/extensions/youtube/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,15 @@ def process(self, text):
except IndexError:
return

YH.all_searches = YM.get_vid_list(search_term)
YH.all_searches = YM.get_vid_list(search_term, int(self.metadata[C_PLUGIN_SETTINGS][P_YT_MAX_SEARCH_LEN]))
search_results = YM.get_choices(YH.all_searches)
if not search_results:
GS.gui_service.quick_gui(
f"<font color='{GS.cfg[C_PGUI_SETTINGS][P_TXT_HEAD_COL]}'>No search results found.</font>",
text_type='header',
box_align='left',
text_align='left')
return
GS.gui_service.quick_gui(
f"{search_results}\nWhich one would you like to play?",
text_type='header',
Expand Down

0 comments on commit feba2aa

Please sign in to comment.