youtube.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #
  2. # This file is part of stov, written by Helmut Pozimski in 2012.
  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 gettext
  19. import sqlite3
  20. import subprocess
  21. from outputhelper import printf
  22. class video(object):
  23. def __init__(self, title, description, ytid, conf, downloaded, failcount = 0, 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):
  32. """Downloads the video by calling youtube-dl as an external process"""
  33. self.__targetdir = self.__conf.values["downloaddir"] + "/" + directory
  34. if os.access(self.__targetdir, os.F_OK) is False:
  35. try:
  36. printf(_("Creating directory %s" % self.__targetdir),
  37. outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
  38. os.makedirs(self.__targetdir, 0750)
  39. except os.error:
  40. printf(_("Download directory does not exist \
  41. and can't be created. Please check your configuration and try again"),
  42. outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr")
  43. os.chdir(self.__targetdir)
  44. if self.downloaded == 0:
  45. try:
  46. printf(_('Downloading video "%s"' % self.title),
  47. outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
  48. if self.__conf.outputlevel == "default":
  49. subprocess.check_call(["youtube-dl", "--max-quality=%s" % itag_value, "-t",
  50. "http://www.youtube.com/watch?v=%s" % self.ytid],
  51. stderr=sys.stderr, stdout=open("/dev/null", "w"))
  52. elif self.__conf.outputlevel == "verbose":
  53. subprocess.check_call(["youtube-dl", "--max-quality=%s" % itag_value, "-t",
  54. "http://www.youtube.com/watch?v=%s" % self.ytid],
  55. stderr=sys.stderr, stdout=sys.stdout)
  56. elif self.__conf.outputlevel == "quiet":
  57. subprocess.check_call(["youtube-dl", "--max-quality=%s" % itag_value, "-t",
  58. "http://www.youtube.com/watch?v=%s" % self.ytid],
  59. stderr=open("/dev/null", "w"), stdout=open("/dev/null", "w"))
  60. try:
  61. self.__database = sqlite3.connect(self.__conf.dbpath)
  62. except sqlite3.OperationalError:
  63. printf(_("The Video \"%s\" has been "
  64. "downloaded but the status could not be updated in the database. "
  65. "Please check what went wrong and correct it" % self.title),
  66. outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr")
  67. else:
  68. self.__cursor = self.__database.cursor()
  69. printf(_('Downloaded video "%s", updating database accordingly.'
  70. % self.title), outputlevel="verbose", level=self.__conf.outputlevel,
  71. descriptor="stderr")
  72. self.__query = "UPDATE videos SET downloaded ='1' WHERE id = ?"
  73. self.__cursor.execute(self.__query, (self.__ID,))
  74. self.__database.commit()
  75. self.__database.close()
  76. except subprocess.CalledProcessError:
  77. printf(_("The video %s could not be downloaded "
  78. "due to some problem with youtube-dl. If this happens to more than one "
  79. "video, please check your youtube-dl version" % self.title),
  80. outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr")
  81. try:
  82. self.__database = sqlite3.connect(self.__conf.dbpath)
  83. except sqlite3.OperationalError:
  84. printf(_("The Video \"%s\" has failed "
  85. "downloading but the status could not be updated in the database. "
  86. "Please check what went wrong and correct it" % self.title),
  87. outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr")
  88. else:
  89. self.__cursor = self.__database.cursor()
  90. self.failcnt = int(self.failcnt) + 1
  91. if self.failcnt >= int(self.__conf.values["maxfails"]):
  92. printf(_("The video \"%s\" has failed downloading too often, marking as"
  93. " failed" % self.title), outputlevel="default", level=self.__conf.outputlevel,
  94. descriptor="stderr")
  95. self.__statement = "UPDATE videos SET downloaded = -1 WHERE id = ?"
  96. self.__cursor.execute(self.__statement, (self.__ID,))
  97. self.__statement = "UPDATE videos SET failcnt = ? WHERE id = ?"
  98. self.__cursor.execute(self.__statement, (self.failcnt, self.__ID))
  99. self.__database.commit()
  100. self.__database.close()
  101. return False
  102. else:
  103. return True
  104. def AssignDBID(self, id):
  105. self.__ID = id
  106. def delete(self):
  107. try:
  108. self.__database = sqlite3.connect(self.__conf.dbpath)
  109. except sqlite3.OperationalError:
  110. printf(_("Could not connect to the database, please check settings and"
  111. "permissions and try again"), outputlevel="default",
  112. level=self.__conf.outputlevel, descriptor="stderr")
  113. return False
  114. else:
  115. self.__cursor = self.__database.cursor()
  116. self.__statement = "DELETE FROM videos where id = ?"
  117. self.__cursor.execute(self.__statement, (self.__ID,))
  118. self.__database.commit()
  119. self.__database.close()
  120. return True