youtube.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. from __future__ import unicode_literals
  17. import os
  18. import sys
  19. import subprocess
  20. from lib_stov import stov_exceptions
  21. class video(object):
  22. def __init__(self, title, description, ytid, conf, downloaded, failcount=0,
  23. id=0):
  24. self.__ID = id
  25. self.title = title
  26. self.description = description
  27. self.ytid = ytid
  28. self.__conf = conf
  29. self.downloaded = downloaded
  30. self.failcnt = int(failcount)
  31. def DownloadVideo(self, directory, itag_value, video_codec):
  32. """Downloads the video by calling youtube-dl as an external process"""
  33. targetdir = self.__conf.values["downloaddir"] + "/" + directory
  34. if os.access(targetdir, os.F_OK) is False:
  35. try:
  36. os.makedirs(targetdir, 0o750)
  37. except os.error:
  38. raise stov_exceptions.DirectoryCreationFailedException()
  39. os.chdir(targetdir)
  40. if self.downloaded == 0:
  41. try:
  42. if self.__conf.outputlevel == "default":
  43. subprocess.check_call(["youtube-dl", "-f %s/%s"
  44. % (itag_value, video_codec), "-t",
  45. "http://www.youtube.com/watch?v=%s"
  46. % self.ytid],
  47. stderr=sys.stderr,
  48. stdout=open("/dev/null", "w"))
  49. elif self.__conf.outputlevel == "verbose":
  50. subprocess.check_call(["youtube-dl", "-f %s/%s"
  51. % (itag_value, video_codec), "-t",
  52. "http://www.youtube.com/watch?v=%s"
  53. % self.ytid],
  54. stderr=sys.stderr, stdout=sys.stdout)
  55. elif self.__conf.outputlevel == "quiet":
  56. subprocess.check_call(["youtube-dl", "-f %s/%s"
  57. % (itag_value, video_codec), "-t",
  58. "http://www.youtube.com/watch?v=%s"
  59. % self.ytid],
  60. stderr=open("/dev/null", "w"),
  61. stdout=open("/dev/null", "w"))
  62. except subprocess.CalledProcessError:
  63. self.failcnt = int(self.failcnt) + 1
  64. return False
  65. else:
  66. self.downloaded = 1
  67. return True
  68. def AssignDBID(self, id):
  69. self.__ID = id
  70. def GetID(self):
  71. return self.__ID