Skip to content

Instantly share code, notes, and snippets.

@jehuamanna
Created July 7, 2017 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jehuamanna/d882b9a0233ebc0e6561873a718f8b87 to your computer and use it in GitHub Desktop.
Save jehuamanna/d882b9a0233ebc0e6561873a718f8b87 to your computer and use it in GitHub Desktop.
automatically downloads nptel videos from youtube
from __future__ import unicode_literals
import sys
import urllib.request
import youtube_dl
class MyLogger(object):
def debug(self, msg):
pass
def warning(self, msg):
pass
def error(self, msg):
print(msg)
def my_hook(d):
if d['status'] == 'finished':
print('Done downloading, now converting ...')
ydl_opts = {
'format': '18',
'logger': MyLogger(),
'progress_hooks': [my_hook],
}
Urls = sys.stdin.read()
urls = list(filter(None, Urls.split("\n")))
for url in urls:
response = urllib.request.urlopen(url)
print(url)
webContent = response.read().decode("iso-8859-1")
s_i = webContent.find("v/", webContent.find("youtube.com"))
e_i = webContent.find("?", s_i)
youtube_url = "youtube.com/watch?v=" + webContent[s_i+2:e_i]
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([youtube_url])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment