1
0

generic_video.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. from lib_stov import configuration
  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, itag_value, 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 itag_value: quality specifier
  40. :type itag_value: int
  41. :param video_codec: video codec to download
  42. :type video_codec: str
  43. :param url: url to the video
  44. :type url: str
  45. :return: boolean value
  46. :rtype: bool
  47. """
  48. targetdir = self._conf.values["downloaddir"] + "/" + directory
  49. if not os.access(targetdir, os.F_OK):
  50. try:
  51. LOGGER.debug(_("Creating directory %s"), targetdir)
  52. os.makedirs(targetdir, 0o750)
  53. except os.error:
  54. raise stov_exceptions.DirectoryCreationFailedException()
  55. os.chdir(targetdir)
  56. if self.downloaded == 0:
  57. try:
  58. youtubedl_wrapper.download_video(url, itag_value)
  59. except stov_exceptions.YoutubeDlCallFailed:
  60. self.failcnt = int(self.failcnt) + 1
  61. return False
  62. else:
  63. self.downloaded = 1
  64. return True
  65. return False
  66. def get_id(self):
  67. """Resturns the id attribute assigned to the object."""
  68. return self._id