Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ChatBot-ALL-Project/youtube.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
15 lines (13 sloc)
1015 Bytes
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
#Code from https://www.codeproject.com/Articles/873060/Python-Search-Youtube-for-Video , accessed at 19 Nov. 2017 | |
import urllib.request | |
import urllib.parse | |
import re | |
#Grant Currel's code, the author of the article cited above. I added the function call to easily use this code and modified the variable names in order to make it easier to understand. | |
def ytLinksearch(userInput): | |
"""The input is looked up on Youtube, the best result is returned and merged with the predictible part of a Youtube link""" | |
#Code from https://www.codeproject.com/Articles/873060/Python-Search-Youtube-for-Video , accessed at 19 Nov. 2017, where Andrei slightly modified it and added the docstring | |
userRequest = urllib.parse.urlencode({"search_query" : userInput}) | |
ytPreffix = urllib.request.urlopen("http://www.youtube.com/results?" + userRequest) | |
result = re.findall(r'href=\"\/watch\?v=(.{11})', ytPreffix.read().decode()) | |
linkToVid = str("http://www.youtube.com/watch?v=" + result[1]) | |
return(linkToVid) | |