Skip to content
Permalink
master
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
#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)