from pytube import YouTube
import os
class YouTubeDownloader:
def __init__(self,URL,FolderPath):
self.Url = URL
self.FolderPath = FolderPath
def onProgress(self,stream,chunk,remains):
total = stream.filesize
percent = (total-remains)/total*100
print(f'下載中… {percent:05.2f}%', end='\r')
def YTDownloader(self):
yt = YouTube(self.Url,on_progress_callback=self.onProgress)
print(yt.title+".mp4"+" downloading....")
yt.streams.filter().get_highest_resolution().download(self.FolderPath)
print("Download "+".mp4"+yt.title+".mp4"+" OK!")
def YTMp3Downloader(self):
yt = YouTube(self.Url,on_progress_callback=self.onProgress)
print(yt.title+".mp4"+" downloading....")
yt.streams.filter().get_audio_only().download(self.FolderPath)
print("Download "+yt.title+".mp4"+" OK!")
class OpenFiles():
#開啟檔案
def __init__(self,FileName,FolderName):
self.FileName = FileName
self.FolderName = FolderName
def FileContentList(self):
#設定信件內容檔名並取得檔案內容並回傳List
ContentFileName = str(os.path.abspath(os.getcwd()))+"\\"+self.FileName
ContentFile = open(ContentFileName,'r')
FileContentList = ContentFile.readlines()
ContentFile.close()
return FileContentList
def GetFolderPath(self):
#取得資料夾路徑
if os.path.exists(self.FolderName):
FolderPath = str(os.path.abspath(os.getcwd()))+"\\"+self.FolderName
else:
os.mkdir(self.FolderName)
FolderPath = str(os.path.abspath(os.getcwd()))+"\\"+self.FolderName
return FolderPath