youtube.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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, description, ytid, conf, downloaded, failcount=0,
  26. video_id=0):
  27. self._id = video_id
  28. self.title = title
  29. self.description = description
  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 os.access(targetdir, os.F_OK) is False:
  38. try:
  39. os.makedirs(targetdir, 0o750)
  40. except os.error:
  41. raise stov_exceptions.DirectoryCreationFailedException()
  42. os.chdir(targetdir)
  43. if self.downloaded == 0:
  44. try:
  45. if self.__conf.outputlevel == "default":
  46. subprocess.check_call(["youtube-dl", "-f %s/%s"
  47. % (itag_value, video_codec), "-t",
  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(["youtube-dl", "-f %s/%s"
  54. % (itag_value, video_codec), "-t",
  55. "http://www.youtube.com/watch?v=%s"
  56. % self.ytid],
  57. stderr=sys.stderr, stdout=sys.stdout)
  58. elif self.__conf.outputlevel == "quiet":
  59. subprocess.check_call(["youtube-dl", "-f %s/%s"
  60. % (itag_value, video_codec), "-t",
  61. "http://www.youtube.com/watch?v=%s"
  62. % self.ytid],
  63. stderr=open("/dev/null", "w"),
  64. stdout=open("/dev/null", "w"))
  65. except subprocess.CalledProcessError:
  66. self.failcnt = int(self.failcnt) + 1
  67. return False
  68. else:
  69. self.downloaded = 1
  70. return True
  71. def get_id(self):
  72. """Resturns the id attribute assigned to the object."""
  73. return self._id