1
0

youtube.py 5.4 KB

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