• youtube-dl
    • https://namu.wiki/w/youtube-dl
    • https://github.com/yt-dlp/yt-dlp
  • pytube
    • https://pypi.org/project/pytube
    • https://github.com/pytube/pytube
    • https://pytube.io
    • import requests
      
      from pytube import Playlist
      from pytube import Search
      
      
      def check(video_id):
          try:
              response = requests.head(f"https://www.youtube.com/watch?v={video_id}", timeout=2)
              if response is None:
                  raise Exception(f"response is None")
              if response.status_code != requests.codes.ok:
                  raise Exception(f"{response.status_code} response {response.headers}")
              video_headers = response.headers
          except Exception as e:
              my_logger.error(f"youtube.check() : e={e}")
              raise e
          return video_headers
          
          
      def search(keywords):
          try:
              search_obj = Search(f"{keywords}")
              if search_obj is None:
                  raise Exception(f"search_obj is None")
              if search_obj.results is None or len(search_obj.results) < 1:
                  raise Exception(f"search_obj.results < 1")
      
              result = None
              for youtube_obj in search_obj.results:
                  if youtube_obj.length is None:
                      continue
                  if youtube_obj.length > 300:
                      continue
                  if youtube_obj.length < 60:
                      continue
      
                  result = youtube_obj
                  break
          except Exception as e:
              my_logger.error(f"youtube.search() : e={e}")
              return None
          return result
          
          
      def download(youtube_obj):
          try:
              # TODO : https://github.com/pytube/pytube/issues (전체검색+F -> Scope -> ...)
              # pytube -> cipher.py Line 411 -> find_object_from_startpoint() -> js
              # https://stackoverflow.com/questions/76539324/pytube-exceptions-regexmatcherror-get-throttling-function-name-could-not-find
              # https://github.com/pytube/pytube/issues/1684
              
              cache_name = f"{youtube_obj.video_id}.mp4"
              cache_dir = f"{my_env['YOUTUBE_DOWNLOAD']}/CACHE"
              cache_path = f"{cache_dir}/{cache_name}"
              download_path = f"{my_env['YOUTUBE_DOWNLOAD']}/{time_util.timestamp()}.mp4"
      
              if not file_util.is_exists(cache_path):
                  first_stream = youtube_obj.streams.filter(res='360p', file_extension='mp4', progressive=True).first()
                  first_stream.download(output_path=cache_dir, filename=cache_name, timeout=100)
              file_util.copy(cache_path, download_path)
      
              result = {
                  "video_id": youtube_obj.video_id,
                  "video_url": f"https://www.youtube.com/watch?v={youtube_obj.video_id}",
                  "thumbnail_url": youtube_obj.thumbnail_url,
                  "author": youtube_obj.author[0:20],
                  "title": youtube_obj.title[0:120],
                  "description": youtube_obj.description,
                  "length": youtube_obj.length,
                  "views": youtube_obj.views,
                  "video_path": download_path,
              }
          except Exception as e:
              my_logger.error(f"youtube.download() : e={e}")
              return None
          return result
          
      
      def load_playlist(playlist_id):
          try:
              playlist_obj = Playlist(f"https://www.youtube.com/watch?list={playlist_id}")
              if playlist_obj is None:
                  raise Exception(f"playlist_obj is None")
      		if len(playlist_obj.videos) < 1:
                  raise Exception(f"playlist_obj.videos is {playlist_obj.videos}")
                  
              playlist = list()
              playlist.extend(playlist_obj.videos)
          except Exception as e:
              my_logger.error(f"youtube.load_playlist() : e={e}")
              raise e
          return playlist
      
      
      def load_video(youtube_obj, refresh_desc=False):
          try:
              video = download(youtube_obj)
              if video is None:
                  raise Exception(f"video is None")
              if refresh_desc:
                  youtube_obj.streams.first()
                  video['description'] = youtube_obj.description
          except Exception as e:
              my_logger.error(f"youtube.load_video() : e={e}")
              raise e
          return video
  • pytchat
    • https://pypi.org/project/pytchat
    • https://github.com/taizan-hokuto/pytchat/wiki
  • ...

-끝-

'빅브로 들' 카테고리의 다른 글

OBS  (0) 2023.11.21
Slack  (0) 2021.06.27
DISCORD  (0) 2021.05.05
sendbird  (0) 2021.04.28
firebase  (0) 2020.12.02

+ Recent posts