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