1
0

youtube.py 5.3 KB

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