-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathTedDownloader.py
38 lines (25 loc) · 862 Bytes
/
TedDownloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import requests
from bs4 import BeautifulSoup
import re #regular expression pattern matching
import sys #argument parsing
if len(sys.argv) >1:
url = sys.argv[1]
else:
sys.exit("Error: please enter TED TALK url")
r= requests.get(url)
rcontent= r.content
print("Download about to start")
soup = BeautifulSoup(rcontent,features="lxml")
for val in soup.find_all("script"):
if(re.search("pageProps",str(val))) is not None:
result = str(val)
result_mp4= re.search("(?P<url>https?://[^\s]+)(mp4)",result).group("url")
mp4_url = result_mp4.split('"')[0]
mp4_url += "mp4"
print("Downloading video from ..." + mp4_url)
file_name = mp4_url.split("/")[len(mp4_url.split("/"))-1].split('?')[0]
print ("storing video in ... " +file_name)
r= requests.get(mp4_url)
with open(file_name, 'wb')as f:
f.write(r.content)
print("Process finished")