Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. from __future__ import unicode_literals
  2. from bs4 import BeautifulSoup
  3. import requests
  4. import os
  5. import youtube_dl
  6.  
  7. url = 'https://www.safaribooksonline.com/library/view/ccna-routing-and/9780134580715/'
  8. domain = 'https://www.safaribooksonline.com'
  9. output_folder = 'output'
  10. username = 'username'
  11. password = 'SuperSecretPassword'
  12.  
  13. d = os.path.dirname(os.path.abspath(__file__))
  14.  
  15. req = requests.get(url)
  16.  
  17. soup = BeautifulSoup(req.text, 'html.parser')
  18.  
  19.  
  20. lessons = soup.find_all('li', class_='toc-level-1')
  21. print("All lessons is: ", len(lessons))
  22. print("*"*100)
  23.  
  24. source_category_block = soup.find('div', class_='description t-description')
  25. if 'Book Description' in soup.text:
  26.     source_category = "book"
  27.     print("Your source category is:", source_category)
  28. elif 'Video Description' in soup.text:
  29.     source_category = "video"
  30.     print("Your source category is:", source_category)
  31. else:
  32.     source_category = "undefined"
  33.     print("Your source category is:", source_category)
  34.  
  35. source_name_block = soup.find('h1', class_='t-title')
  36.  
  37. if source_name_block:
  38.     source_name = source_name_block.text
  39.     print("Your source name is:", source_name)
  40. else:
  41.     source_name = None
  42.  
  43. Path = os.path.join(d, output_folder, source_category, source_name)
  44. os.makedirs(Path, exist_ok=True)
  45. module_name = 'Module 0'
  46. for lesson in lessons:
  47.     lesson_name = lesson.a.text
  48.     if lesson_name.startswith('Module') and not 'Summary' in lesson_name:
  49.         module_name = lesson_name
  50.         os.makedirs(Path + '/' + module_name, exist_ok=True)
  51.         for index, video in enumerate(lesson.ol.find_all('a')):
  52.             video_name = str(index) + ' - ' + video.text
  53.             video_url = domain + video.get('href')
  54.             video_out = Path + '/' + module_name + '/' + video_name + '.mp4'
  55.             print("youtube-dl --output '{}' {}".format(video_out, video_url))
  56.     else:
  57.         os.makedirs(Path + '/' + module_name + '/' + lesson_name, exist_ok=True)
  58.         for index, video in enumerate(lesson.ol.find_all('a')):
  59.             video_name = str(index) + ' - ' + video.text
  60.             video_url = domain + video.get('href')
  61.             video_out = Path + '/' + module_name + '/' + lesson_name + '/' + video_name + '.mp4'
  62.             print("youtube-dl --output '{}' {}".format(video_out, video_url))
  63.  
  64. def my_hook(d):
  65.     if d['status'] == 'finished':
  66.         print('Done downloading, now converting ...')
  67.  
  68. ydl_opts = {
  69.     'format': 'bestaudio/best',
  70.     'outtmpl': '%(id)s',
  71.     'noplaylist' : True,
  72.     'progress_hooks': [my_hook],
  73. }
  74.  
  75. with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  76.     ydl.download(['https://www.youtube.com/watch?v=pwp1CH5R-w4'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement