Beispiel #1
0
def playlists_search():
    print('')
    print('------- YOUTUBE SEARCH ENGINE PLAYLISTS --------')
    print('')
    search_term = input('Search playlist: ')

    search = SearchPlaylists(search_term, max_results=5, mode='dict')

    for v in search.result()['search_result']:
        print('------------------------------------------------')
        print('')
        print('index: ' + str(v['index']))
        print('title: ' + v['title'])
        print('count: ' + v['count'])
        print('channel: ' + v['channel'])
        print('link: ' + v['link'])
        print('')
        print('------------------------------------------------')
Beispiel #2
0
def search_playlist(query):
    spinner.start()

    data = SearchPlaylists(query, offset=1, mode='dict', max_results=8)
    final_data = data.result()
    if not final_data:
        spinner.stop()
        print("[bold red]  Something went wrong. Try another query or check network connenctivity[/bold red]")

    else:
        options = []
        for i in final_data['search_result']:
            temp_dict = {}
            temp_dict.update({'name': i['title'], 'value': i['link']})
            options.append(temp_dict)
        spinner.stop()

        return options
Beispiel #3
0
def youtube_search(text,
                   parse_text=True,
                   return_info=False,
                   limit_duration=False,
                   duration_limit=600):
    if text in ('maagnolia', 'magnolia') and return_info:
        text = 'magnolia (Audio)'
    # icon = 'https://cdn4.iconfinder.com/data/icons/social-media-icons-the-circle-set/48/youtube_circle-512.png'
    results, kind = 1, 'video'
    if parse_text:
        p = re.compile('--[1-4][0-9]|--[1-2]')
        with suppress(AttributeError):
            results = int(p.search(text).group()[2:])
        p = re.compile('--playlist')  # defaults to video so I removed --video
        with suppress(AttributeError):
            kind = p.search(text).group()[2:]
        with suppress(ValueError):
            text = text[text.index(' '):text.index('--')]
    max_results = min(results + 5, 50)
    if kind == 'playlist':
        search = SearchPlaylists(text, mode='dict', max_results=max_results)
    else:
        search = SearchVideos(text, mode='dict', max_results=max_results)
    # pprint(search.result())
    search_response = search.result()['search_result']
    duration_dict = {}
    valid_result = None
    for search_result in search_response:
        duration = 0
        if kind == 'video':
            for num in search_result['duration'].split(':'):
                duration = duration * 60 + int(num)
            if not limit_duration or duration < duration_limit:
                valid_result = search_result
                break
        else:  # playlist
            valid_result = search_result
            break
    if valid_result is None: return f'No {kind} found'
    if return_info:
        return valid_result['link'], fix_youtube_title(
            valid_result['title']), valid_result['id']
    return valid_result['link']
Beispiel #4
0
##########https://github.com/alexmercerind/youtube-search-python/##########

from youtubesearchpython import SearchVideos, SearchPlaylists

##########Search For Videos##########

videos = SearchVideos("NoCopyrightSounds",
                      offset=1,
                      mode="json",
                      max_results=20)
videos_result = videos.result(
)  #Getting JSON result (You can set mode = "json", "dict" or "list")
print(videos_result)

##########Search For Playlists##########

playlists = SearchPlaylists("NoCopyrightSounds",
                            offset=1,
                            mode="json",
                            max_results=20)
playlists_result = playlists.result(
)  #Getting JSON result (You can set mode = "json", "dict" or "list")
print(playlists_result)
Beispiel #5
0
class YoutubeSearch:
    # set search key word
    # @parms offset, offset for result pages on YouTube, optional, defaults to 1.
    # @parms mode, search result return type, should be "json", "dict" or "list", optional, defaults to "dict".
    # @parms max_results_videos , maximum number of video results, optional, defaults to 20, maximum value should be 20.
    # @parms max_results_playlist , maximum number of playlist results, optional, defaults to 10, maximum value should be 20.
    # @parms language, use to get results in particular language, optional, defaults to "zh-TW".
    # @parms region, use to get results according to particular language, optional, defaults to "TW".
    def __init__(self,
                 offset: int = 1,
                 mode: str = "dict",
                 max_results_videos: int = 20,
                 max_results_playlist: int = 10,
                 language: str = "zh-TW",
                 region: str = "TW"):
        self.offset = offset
        self.mode = mode
        self.max_results_videos = max_results_videos
        self.max_results_playlist = max_results_playlist
        self.language = language
        self.region = region
        self.videosResult = None
        self.playlistResult = None
        return

    # search video with caption in youtube by keyword
    # @parms keyword, search keyword
    # @return   search result in type of self.mode
    def searchYTVideo(self, keyword: str):
        withoutCaption = "EgIQAQ%3D%3D"  # video w/o caption
        withCaption = "EgQQASgB"  # video with caption ( but it still get video w/o caption in youtube search -.- )
        self.videosResult = SearchVideos(keyword,
                                         offset=self.offset,
                                         mode=self.mode,
                                         max_results=self.max_results_videos,
                                         searchPreferences=withCaption,
                                         language=self.language,
                                         region=self.region)
        return self.videosResult.result()

    # search playlist in youtube by keyword (playlist can't search with caption)
    # @parms keyword, search keyword
    # @return   search result in type of self.mode
    def searchYTPlaylist(self, keyword: str):
        self.playlistResult = SearchPlaylists(
            keyword,
            offset=self.offset,
            mode=self.mode,
            max_results=self.max_results_videos,
            language=self.language,
            region=self.region)
        return self.playlistResult.result()

    # return all video's titles as a list
    # @return   a list of titles of video search result
    def getVideoTitles(self):
        self.checkIsSearch("videosResult", "getVideoTitles")
        return self.videosResult.titles

    # return all playlist's titles as a list
    # @return   a list of titles of playlist search result
    def getPlaylistTitles(self):
        self.checkIsSearch("playlistResult", "getPlaylistTitles")
        return self.playlistResult.titles

    # return all video's link as a list
    # @return   a list of links of video search result
    def getVideoLinks(self):
        self.checkIsSearch("videosResult", "getVideoLinks")
        return self.videosResult.links

    # return all playlist's link as a list
    # @return   a list of links of playlist search result
    def getPlaylistLinks(self):
        self.checkIsSearch("playlistResult", "getPlaylistLinks")
        return self.playlistResult.links

    # check is class already search video/playlist before
    # @parms searchType, search result object name, should be "videosResult" or "playlistResult"
    # @parms callFunction, called function name, use to print exception messages
    def checkIsSearch(self, searchType: str, callFunction: str):
        if searchType == "videosResult" and not self.videosResult:
            raise Exception(
                f"Exception : video result not exist, you should search first before you call {callFunction}!"
            )
        elif searchType == "playlistResult" and not self.playlistResult:
            raise Exception(
                f"Exception : playlist result not exist, you should search first before you call {callFunction}!"
            )
        return
Beispiel #6
0
# from youtubesearchpython import SearchVideos
from youtubesearchpython import SearchPlaylists

# search = SearchVideos("Battlefield 5", offset=2, mode="json", max_results=3)
search = SearchPlaylists("Battlefield 5", offset=2, mode="json", max_results=3)
# print(search.result())
print(search.result())


from youtubesearchpython import SearchVideos, SearchPlaylists

videos = SearchVideos("NoCopyrightSounds",
                      offset=1,
                      mode="json",
                      max_results=20)
videosResult = videos.result()
print(videosResult)

playlists = SearchPlaylists("NoCopyrightSounds",
                            offset=1,
                            mode="json",
                            max_results=20)
playlistsResult = playlists.result()
print(playlistsResult)