main.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from __future__ import unicode_literals
  2. import json
  3. import os.path
  4. import youtube_dl
  5. from youtube_dl.utils import DateRange
  6. def create_default_settings():
  7. channels = []
  8. channels.append({})
  9. channels[0]['url'] = 'https://www.youtube.com/channel/UCVBfIU1_zO-P_R9keEGdDHQ/videos'
  10. channels[0]['path'] = '/home/bkeys/Videos/'
  11. channels[0]['proxy'] = 'None'
  12. channels[0]['date_limit'] = '20180828'
  13. channels[0]['name'] = 'Paul Cockshott'
  14. with open(os.environ['HOME'] + '.config/subscriber/settings.json', 'w') as outfile:
  15. json.dump(channels, outfile, indent=2)
  16. def download_channel(channel):
  17. ydl_opts = {'quiet': True}
  18. ydl = youtube_dl.YoutubeDL(ydl_opts)
  19. result = ydl.extract_info(channel['url'], download=False)
  20. if 'entries' in result:
  21. dl_dir = channel['path'] + result['entries'][0]['uploader'] + '/'
  22. if not os.path.exists(dl_dir):
  23. os.makedirs(dl_dir)
  24. print(result['entries'][0]['uploader'])
  25. for entry in result['entries']:
  26. if not os.path.isfile(dl_dir + entry['title'] + ".webm"):
  27. ydl = None
  28. ydl_opts = {
  29. 'format': 'webm',
  30. 'quiet': True,
  31. 'outtmpl': dl_dir + entry['title'] + '.%(ext)s',
  32. 'keepvideo': True
  33. }
  34. if not channel['proxy'] == 'None':
  35. ydl_opts['proxy'] = channel['proxy']
  36. if not channel['date_limit'] == 'None':
  37. ydl_opts['daterange'] = DateRange(channel['date_limit'])
  38. ydl = youtube_dl.YoutubeDL(ydl_opts)
  39. print(dl_dir + entry['title'] + ".webm")
  40. ydl.download([entry['webpage_url']])
  41. def main():
  42. if not os.path.exists(os.environ['HOME'] + ".config/subscriber/"):
  43. os.makedirs(os.environ['HOME'] + ".config/subscriber/")
  44. if not os.path.isfile(os.environ['HOME'] + '.config/subscriber/settings.json'):
  45. create_default_settings()
  46. settings = json.load(open(os.environ['HOME'] + '.config/subscriber/settings.json', 'r'))
  47. for channel in settings:
  48. download_channel(channel)
  49. if __name__ == "__main__":
  50. main()