youtube.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 sys
  19. import subprocess
  20. from lib_stov import stov_exceptions
  21. class Video(object):
  22. """This class stores all the attributes of a single youtube video and is
  23. also able to download it using youtube-dl.
  24. """
  25. def __init__(self, title, ytid, conf, downloaded, failcount=0,
  26. video_id=0):
  27. self._id = video_id
  28. self.title = title
  29. self.ytid = ytid
  30. self.__conf = conf
  31. self.downloaded = downloaded
  32. self.failcnt = int(failcount)
  33. def download_video(self, directory, itag_value, video_codec):
  34. """Downloads the video by calling youtube-dl as an external process"""
  35. targetdir = self.__conf.values["downloaddir"] + "/" + directory
  36. if os.access(targetdir, os.F_OK) is False:
  37. try:
  38. os.makedirs(targetdir, 0o750)
  39. except os.error:
  40. raise stov_exceptions.DirectoryCreationFailedException()
  41. os.chdir(targetdir)
  42. if self.downloaded == 0:
  43. try:
  44. if self.__conf.outputlevel == "default":
  45. subprocess.check_call([self.__conf.values["youtube-dl"], "-f %s/%s"
  46. % (itag_value, video_codec), "-o",
  47. "%(title)s-%(id)s.%(ext)s",
  48. "http://www.youtube.com/watch?v=%s"
  49. % self.ytid],
  50. stderr=sys.stderr,
  51. stdout=open("/dev/null", "w"))
  52. elif self.__conf.outputlevel == "verbose":
  53. subprocess.check_call([self.__conf.values["youtube-dl"], "-f %s/%s"
  54. % (itag_value, video_codec), "-o",
  55. "%(title)s-%(id)s.%(ext)s",
  56. "http://www.youtube.com/watch?v=%s"
  57. % self.ytid],
  58. stderr=sys.stderr, stdout=sys.stdout)
  59. elif self.__conf.outputlevel == "quiet":
  60. subprocess.check_call([self.__conf.values["youtube-dl"], "-f %s/%s"
  61. % (itag_value, video_codec), "-o",
  62. "%(title)s-%(id)s.%(ext)s",
  63. "http://www.youtube.com/watch?v=%s"
  64. % self.ytid],
  65. stderr=open("/dev/null", "w"),
  66. stdout=open("/dev/null", "w"))
  67. except subprocess.CalledProcessError:
  68. self.failcnt = int(self.failcnt) + 1
  69. return False
  70. else:
  71. self.downloaded = 1
  72. return True
  73. def get_id(self):
  74. """Resturns the id attribute assigned to the object."""
  75. return self._id