-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from DuckBoss/youtube-plugin-search-fix
Fix index out of range error and new metadata option
- Loading branch information
Showing
7 changed files
with
127 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
JJMumbleBot/plugins/extensions/youtube/licenses/youtube-search/LICENSE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
JJMumbleBot/plugins/extensions/youtube/utility/youtube_search.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters