Reputation: 2402
I am writing a simple script to download .mp4 TEDTalks given a list of TEDTalk website links:
# Run through a list of TEDTalk website links and download each
# TEDTalk in high quality MP4
import urllib.request
#List of website links
l = [
"http://www.ted.com/index.php/talks/view/id/28",
"http://www.ted.com/index.php/talks/view/id/29",
]
# Function which takes the location of the string "-480p.mp4",
# d = 1 less that location, and a string and returns the
# full movie download link
def findFullURL(d, e, s):
a = s[d]
if a != "/":
#Subtract from d to move back another letter
d = d - 1
findFullURL(d, e, s)
else:
fullURL = "http://download.ted.com/talks/" + s[(d+1):e] + "-480p.mp4"
#print(fullURL)
return fullURL
#Iterate through a list of links to download each movie
def iterateList(l):
for x in l:
#get the HTML
f = urllib.request.urlopen(x)
#Convert the HTML file into a string
s = str(f.read(), "utf-8")
f.close()
#Find the location in the string where the interesting bit ends
e = s.find("-480p.mp4")
d = e - 1
#The problem is with this variable url:
url = findFullURL(d, e, s)
print("Downloading " + url)
#TODO: Download the file
I am certain that the function findFullURL works. If you uncomment the print(fullURL)
line at the end of the findFullURL
function, you will see it output the download link exactly as I need it.
However, in the iterateList
function where I try to capture that string via url = findFullURL(d, e, s)
, the variable url seems to take on the value None
. I do not understand this at all. It should be as simple as the following example, which works when I try it in the interpreter:
def hello():
return "Hello"
url = hello()
print(url)
Upvotes: 2
Views: 1538
Reputation: 838806
I am certain that the function findFullURL works.
Being certain that a certain piece of code works is the best way to waste hours of debugging time looking in the wrong place.
In fact that function does not work. You are missing a return:
def findFullURL(d, e, s):
a = s[d]
if a != "/":
#Subtract from d to move back another letter
d = d - 1
return findFullURL(d, e, s) # <<<<<<< here
else:
fullURL = "http://download.ted.com/talks/" + s[(d+1):e] + "-480p.mp4"
#print(fullURL)
return fullURL
Also, you shouldn't be using recursion to solve this task. You can use rfind
instead.
def findFullURL(d, e, s):
d = s.rfind('/', 0, d + 1)
# You probably want to handle the condition where '/' is not found here.
return "http://download.ted.com/talks/" + s[(d+1):e] + "-480p.mp4"
Upvotes: 10
Reputation: 28846
findFullURL
doesn't have a return
statement on the first branch of the if
statement. In Python, this means that it returns None
.
Upvotes: 2