Example #1
0
if 'search' in text:
    #get query string
    pos = text.find('search')
    query = text[pos+len('search'):].strip()

    #mở link đầu tiên trên trình duyệt
    url = next(search(query, tld='com', lang='vi', num=1, start=0, stop=None, pause=2))
    # print(url)
    webbrowser.open(url, autoraise=False)
    end_speech = 'search for ' + query
    speak(end_speech, 'search.mp3')

#open music on youtube
if 'play' in text:
    # subprocess.call(r'C:\Users\Acer\AppData\Roaming\Spotify/Spotify.exe')
    #get query string
    pos = text.find('play')
    query = text[pos+len('play'):].strip()
    if query == 'music' or query == 'some music' or query == 'music please' or query == 'some music please':
        webbrowser.open(DEFAULT_MUSIC, autoraise=False)    
        speak('play music', 'music.mp3')
    else:
        if len(YoutubeSearch(query, max_results=1).to_dict()) > 0:
            results = YoutubeSearch(query, max_results=1).to_dict()[0]
            url = 'youtube.com' + results.get('link')
            print(url)
            webbrowser.open(url, autoraise=False)    #autoraise=false không hoạt động ở windows
            end_speech = 'play ' + query
            speak(end_speech, 'music.mp3')
        else:
            speak('sorry, i could not find ' + query, 'sorry.mp3')