generic_video.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #
  2. # This file is part of stov, written by Helmut Pozimski 2012-2021.
  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:
  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 url: url to the video
  40. :type url: str
  41. :return: boolean value
  42. :rtype: bool
  43. """
  44. targetdir = self._conf.values["downloaddir"] + "/" + directory
  45. if not os.access(targetdir, os.F_OK):
  46. try:
  47. LOGGER.debug(_("Creating directory %s"), targetdir)
  48. os.makedirs(targetdir, 0o750)
  49. except os.error as exc:
  50. raise stov_exceptions.DirectoryCreationFailedException() \
  51. from exc
  52. os.chdir(targetdir)
  53. if self.downloaded == 0:
  54. try:
  55. youtubedl_wrapper.download_video(url)
  56. except stov_exceptions.YoutubeDlCallFailed:
  57. self.failcnt = int(self.failcnt) + 1
  58. return False
  59. else:
  60. self.downloaded = 1
  61. return True
  62. return False
  63. def get_id(self):
  64. """Resturns the id attribute assigned to the object."""
  65. return self._id