youtube.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #
  2. # This file is part of stov, written by Helmut Pozimski 2012-2015.
  3. #
  4. # stov is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, version 2 of the License.
  7. #
  8. # stov is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with stov. If not, see <http://www.gnu.org/licenses/>.
  15. # -*- coding: utf8 -*-
  16. """This module takes care of managing and downloading single videos."""
  17. import os
  18. import logging
  19. from lib_stov import stov_exceptions
  20. from lib_stov import youtubedl_wrapper
  21. LOGGER = logging.getLogger("stov")
  22. class Video(object):
  23. """This class stores all the attributes of a single youtube video and is
  24. also able to download it using youtube-dl.
  25. """
  26. def __init__(self, title, ytid, conf, downloaded, failcount=0,
  27. video_id=0):
  28. self._id = video_id
  29. self.title = title
  30. self.ytid = ytid
  31. self.__conf = conf
  32. self.downloaded = downloaded
  33. self.failcnt = int(failcount)
  34. def download_video(self, directory, itag_value, video_codec):
  35. """Downloads the video by calling youtube-dl as an external process"""
  36. targetdir = self.__conf.values["downloaddir"] + "/" + directory
  37. if not os.access(targetdir, os.F_OK):
  38. try:
  39. LOGGER.debug(_("Creating directory %s"), targetdir)
  40. os.makedirs(targetdir, 0o750)
  41. except os.error:
  42. raise stov_exceptions.DirectoryCreationFailedException()
  43. os.chdir(targetdir)
  44. if self.downloaded == 0:
  45. try:
  46. url = "http://www.youtube.com/watch?v=%s" % self.ytid
  47. youtubedl_wrapper.download_video(self.__conf, url,
  48. itag_value, video_codec)
  49. except stov_exceptions.YoutubeDlCallFailed:
  50. self.failcnt = int(self.failcnt) + 1
  51. return False
  52. else:
  53. self.downloaded = 1
  54. return True
  55. def get_id(self):
  56. """Resturns the id attribute assigned to the object."""
  57. return self._id