Beispiel #1
0
 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()
Beispiel #2
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 #3
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 #4
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 #5
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 #6
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 #7
0
        elif 'open youtube' in query:
            webbrowser.open("youtube.com")

        elif 'youtube' in query:
            speak("boss please enter the topic of search")
            searchf = input("enter the input of search\n")
            speak(
                'boss do u want to search for playlists or videos! for playlists type pl and for videos type vi'
            )
            sea = input(
                "do u want to search for playlists or videos for playlists type pl and for videos type vi\n"
            )
            if 'pl' in sea:
                speak("searching for the top 5 playlists")
                search2 = SearchPlaylists(searchf,
                                          offset=1,
                                          mode="json",
                                          max_results=20)

                c = search2.titles
                d = search2.links

                print(c[1], "-", d[1])
                print(c[2], "-", d[2])
                print(c[3], "-", d[3])
                print(c[4], "-", d[4])
                print(c[5], "-", d[5])
                speak("these are the top 5 results")
            elif 'vi' in sea:
                speak("searching for the top 5 videos")
                search1 = SearchVideos(searchf,
                                       offset=1,
Beispiel #8
0
    def main(self):
        while True:

            # os.system('reset')

            if self.results is not None:
                print(self.playing_text)
                self.print_results()

            print('''
 s *: search for * in videos.             v #: play video.                   dv #: download video.        h: show history.
 p *: search for * in playlists.          a #: play audio.                   da #: download audio.        H: show Help.
   a: play all search results.         v # hd: try playing video in HD.       y #: copy (yank) url.'''
                  )

            # https://github.com/kcsaff/getkey

            i = input('\n> ')

            # q is for Quit
            if i == 'q':
                break

            # <empty> does nothing
            elif i == '':
                continue

            # H is for Help
            elif i == 'H':
                self.results = None
                self.print_help()

            # v is to toggle video_playback on/off
            elif i == 'm':
                self.video_playback = not self.video_playback
                print(' (Playback-mode is set to ',
                      'VIDEO' if self.video_playback else 'AUDIO',
                      ')',
                      sep='')

            # show history
            elif i == 'h':
                self.results = []
                self.get_history_from_file()
                self.search_string = '<showing playback history>'
                if self.history is None:
                    print('\n No history is saved on this computer.')
                    self.results = None
                    continue
                for num in range(1, min(len(self.history), 20)):
                    h = self.history[-num].copy()
                    h['index'] = str(num).rjust(3) + '  ' + h['index']
                    self.results.append(h)

            # play all videos
            elif i == 'a':
                self.play(list(range(0, 20)))

            # play the video using default (video/audio) mode!
            elif i.isdigit():
                self.play([int(i) - 1])

            # play the video using v mode
            elif re.match("^v ?\d+$", i):
                number = int(''.join(x for x in i if x.isdigit()))
                self.play([number - 1], mode='v')

            # play the video using v mode in HD!
            elif re.match("^v? ?\d+ ?hd$", i):
                number = int(''.join(x for x in i if x.isdigit()))
                self.play([number - 1], mode='v', quality='hd')

            # play the video using mpv mode
            elif re.match("^v? ?\d+ ?mpv$", i):
                number = int(''.join(x for x in i if x.isdigit()))
                self.play([number - 1], mode='v', player='mpv')

            # play the video using vlc mode
            elif re.match("^v? ?\d+ ?vlc$", i):
                number = int(''.join(x for x in i if x.isdigit()))
                self.play([number - 1], mode='v', player='vlc')

            # play the video using cvlc mode
            elif re.match("^v? ?\d+ ?cvlc$", i):
                number = int(''.join(x for x in i if x.isdigit()))
                self.play([number - 1], mode='v', player='cvlc')

            # play the video using a mode
            elif re.match("^a ?\d+$", i):
                number = int(''.join(x for x in i if x.isdigit()))
                self.play([number - 1], mode='a')

            # download video
            elif re.match("^dv ?\d+$", i):
                number = int(''.join(x for x in i if x.isdigit()))
                self.download(number - 1, 'video')

            # download audio
            elif re.match("^da ?\d+$", i):
                number = int(''.join(x for x in i if x.isdigit()))
                self.download(number - 1, 'audio')

            # yank url
            elif re.match("^y ?\d+$", i):
                number = int(''.join(x for x in i if x.isdigit()))
                self.yank(number - 1)

            # Playlist search
            elif i[:2] == 'p ':
                self.results = SearchPlaylists(
                    i[2:],
                    offset=1,
                    mode='dict',
                    max_results=20,
                    language="en-US",
                    region="AT").result()['search_result']
                self.search_string = i

            # default video search
            elif i[:2] == 's ':
                self.results = SearchVideos(
                    i[2:],
                    offset=1,
                    mode='dict',
                    max_results=19,
                    language="en-US",
                    region="AT").result()['search_result']
                self.search_string = i[2:]

            # default video search
            else:
                self.results = SearchVideos(
                    i,
                    offset=1,
                    mode='dict',
                    max_results=19,
                    language="en-US",
                    region="AT").result()['search_result']
                self.search_string = i
Beispiel #9
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)
from youtubesearchpython import SearchVideos, SearchPlaylists

import sys

print(sys.version)

while True:
    if (sys.version[0]=="2"):
        what = raw_input("Playlist (p) search / Video (v) search? ")
        q = what.lower()
        if (q=="v" or q=="video" or q=="videos"):
            videos = SearchVideos(raw_input("Your video: "), 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)
        if (q=="p" or q=="playlist" or q=="playlists"):
            playlists = SearchPlaylists(raw_input("Your playlist: "), 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)
    else if (sys.version[0]=="3"):
        what = input("Playlist (p) search / Video (v) search? ")
        q = what.lower()
        if (q=="v" or q=="video" or q=="videos"):
            videos = SearchVideos(input("Your video: "), 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)
        if (q=="p" or q=="playlist" or q=="playlists"):
            playlists = SearchPlaylists(input("Your playlist: "), 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)
from youtubesearchpython import SearchVideos, SearchPlaylists

videos = SearchVideos("NoCopyrightSounds",
                      offset=1,
                      mode="json",
                      max_results=20,
                      language="en-US",
                      region="US")
videosResult = videos.result()
print(videosResult)

playlists = SearchPlaylists("NoCopyrightSounds",
                            offset=1,
                            mode="json",
                            max_results=20,
                            language="ru-RU",
                            region="US")
playlistsResult = playlists.result()
print(playlistsResult)