Guest User

Untitled

a guest
Nov 21st, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #! /usr/bin/python3
  2. from __future__ import unicode_literals
  3. import youtube_dl
  4. import csv
  5.  
  6. class CSV_Downloader():
  7. def __init__(self, filename):
  8. self.filename = filename
  9.  
  10. def get_videos(self):
  11. videos = []
  12. with open(self.filename, newline='') as csvfile:
  13. reader = csv.reader(csvfile, delimiter=' ', quotechar='|')
  14. for row in reader:
  15. for col in row:
  16. videos.append(col)
  17. videos = filter(None, list(set(videos)))
  18. return videos
  19.  
  20. def save_file(self, videos):
  21. with open(self.filename, 'w', newline='') as csvfile:
  22. writer = csv.writer(csvfile, delimiter=' ', quotechar='|')
  23. for video in videos:
  24. writer.writerow([video])
  25.  
  26. def get_first_item(self, videos):
  27. return next(iter(videos or []), None)
  28.  
  29. def start(self):
  30. ydl_opts = {}
  31. with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  32. while True:
  33. videos = self.get_videos() # getting list of all videos from file
  34. print('{} videos to go'.format(len(videos))) # print no. of video remaining
  35. video = self.get_first_item(videos) # get next video for downloading
  36. if video is None: # check if video is there or not
  37. break
  38.  
  39. ydl.download([video]) # downloading video
  40. videos.remove(video) # remove video from list
  41. self.save_file(videos) # save updated list to file
  42.  
  43. print('All downloaded')
  44.  
  45. def main():
  46. handler = CSV_Downloader('download.csv')
  47. handler.start()
  48.  
  49. if __name__ == '__main__':
  50. main()
Add Comment
Please, Sign In to add comment