1
0

youtube.py 4.2 KB

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