generic_video.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 logging
  18. import os
  19. from lib_stov import configuration
  20. from lib_stov import stov_exceptions
  21. from lib_stov import youtubedl_wrapper
  22. LOGGER = logging.getLogger("stov")
  23. class Video(object):
  24. """This class stores all the attributes of a single video and is
  25. also able to download it using youtube-dl.
  26. """
  27. def __init__(self, title, site_id, downloaded, failcount=0, video_id=0):
  28. self._id = video_id
  29. self.title = title
  30. self.site_id = site_id
  31. self._conf = configuration.Conf.get_instance()
  32. self.downloaded = downloaded
  33. self.failcnt = int(failcount)
  34. def download_video(self, directory, url):
  35. """
  36. Downloads the video by calling youtube-dl as an external process"
  37. :param directory: directory to download to
  38. :type directory: str
  39. :param video_codec: video codec to download
  40. :type video_codec: str
  41. :param url: url to the video
  42. :type url: str
  43. :return: boolean value
  44. :rtype: bool
  45. """
  46. targetdir = self._conf.values["downloaddir"] + "/" + directory
  47. if not os.access(targetdir, os.F_OK):
  48. try:
  49. LOGGER.debug(_("Creating directory %s"), targetdir)
  50. os.makedirs(targetdir, 0o750)
  51. except os.error:
  52. raise stov_exceptions.DirectoryCreationFailedException()
  53. os.chdir(targetdir)
  54. if self.downloaded == 0:
  55. try:
  56. youtubedl_wrapper.download_video(url)
  57. except stov_exceptions.YoutubeDlCallFailed:
  58. self.failcnt = int(self.failcnt) + 1
  59. return False
  60. else:
  61. self.downloaded = 1
  62. return True
  63. return False
  64. def get_id(self):
  65. """Resturns the id attribute assigned to the object."""
  66. return self._id