Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 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.  
  47. ydl_opts = {
  48. }
  49.  
  50. for lesson in lessons:
  51.     lesson_name = lesson.a.text
  52.     if lesson_name.startswith('Module') and not 'Summary' in lesson_name:
  53.         module_name = lesson_name
  54.         os.makedirs(Path + '/' + module_name, exist_ok=True)
  55.         for index, video in enumerate(lesson.ol.find_all('a')):
  56.             video_name = str(index) + ' - ' + video.text
  57.             video_url = domain + video.get('href')
  58.             video_out = Path + '/' + module_name + '/' + video_name + '.mp4'
  59.             video_out_for_youtube_dl = Path + '/' + module_name
  60.             print("youtube-dl --output '{}' {}".format(video_out, video_url))
  61.             ydl_opts['outtmpl'] = video_out_for_youtube_dl + "/%(title)s-%(id)s.%(ext)s"
  62.             with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  63.                 print("Start downloads:", index, " of ", len(lessons))
  64.                 print("Downloads:", video_name)
  65.                 ydl.download([video_url])
  66.     else:
  67.         os.makedirs(Path + '/' + module_name + '/' + lesson_name, exist_ok=True)
  68.         for index, video in enumerate(lesson.ol.find_all('a')):
  69.             video_name = str(index) + ' - ' + video.text
  70.             video_url = domain + video.get('href')
  71.             video_out = Path + '/' + module_name + '/' + lesson_name + '/' + video_name + '.mp4'
  72.             video_out_for_youtube_dl = Path + '/' + module_name + '/' + lesson_name
  73.             print("youtube-dl --output '{}' {}".format(video_out, video_url))
  74.             ydl_opts['outtmpl'] = video_out_for_youtube_dl + "/%(title)s-%(id)s.%(ext)s"
  75.             with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  76.                 print("Start downloads:", index, " of ", len(lessons))
  77.                 print("Downloads:", video_name)
  78.                 ydl.download([video_url])
  79.  
  80.  
  81. """
  82. def my_hook(d):
  83.    if d['status'] == 'finished':
  84.        print('Done downloading, now converting ...')
  85.  
  86. ydl_opts = {
  87.    'format': 'bestaudio/best',
  88.    'outtmpl': '%(id)s',
  89.    'noplaylist' : True,
  90.    'progress_hooks': [my_hook],
  91. }
  92.  
  93. with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  94.    ydl.download(['https://www.youtube.com/watch?v=pwp1CH5R-w4'])
  95.  
  96. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement