Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/python3
- from __future__ import unicode_literals
- import youtube_dl
- import csv
- class CSV_Downloader():
- def __init__(self, filename):
- self.filename = filename
- def get_videos(self):
- videos = []
- with open(self.filename, newline='') as csvfile:
- reader = csv.reader(csvfile, delimiter=' ', quotechar='|')
- for row in reader:
- for col in row:
- videos.append(col)
- videos = filter(None, list(set(videos)))
- return videos
- def save_file(self, videos):
- with open(self.filename, 'w', newline='') as csvfile:
- writer = csv.writer(csvfile, delimiter=' ', quotechar='|')
- for video in videos:
- writer.writerow([video])
- def get_first_item(self, videos):
- return next(iter(videos or []), None)
- def start(self):
- ydl_opts = {}
- with youtube_dl.YoutubeDL(ydl_opts) as ydl:
- while True:
- videos = self.get_videos() # getting list of all videos from file
- print('{} videos to go'.format(len(videos))) # print no. of video remaining
- video = self.get_first_item(videos) # get next video for downloading
- if video is None: # check if video is there or not
- break
- ydl.download([video]) # downloading video
- videos.remove(video) # remove video from list
- self.save_file(videos) # save updated list to file
- print('All downloaded')
- def main():
- handler = CSV_Downloader('download.csv')
- handler.start()
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment