youtube.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # -*- coding: utf8 -*-
  2. import os
  3. import sys
  4. import gettext
  5. import sqlite3
  6. class video(object):
  7. def __init__(self, title, description, ytid, conf, downloaded, id=0):
  8. self.__ID = id
  9. self.title = title
  10. self.description = description
  11. self.ytid = ytid
  12. self.__conf = conf
  13. self.__downloaded = downloaded
  14. def DownloadVideo(self, directory):
  15. self.__targetdir = self.__conf.values["downloaddir"] + "/" + directory
  16. if os.access(self.__targetdir, os.F_OK) == False:
  17. try:
  18. os.makedirs(self.__targetdir, 0750)
  19. except os.error:
  20. print >> sys.stderr, _("Download directory does not exist and can't be created. Please check your configuration and try again")
  21. os.chdir(self.__targetdir)
  22. if self.__downloaded == 0:
  23. if os.system("youtube-dl --max-quality=22 -t %s > /dev/null" %self.ytid) == 0:
  24. try:
  25. self.__database = sqlite3.connect(self.conf.dbpath)
  26. self.__cursor = self.__database.cursor()
  27. self.__query = "UPDATE videos SET downloaded ='1' WHERE id = ?"
  28. self.__cursor.execute(self.__query, (self.__ID,))
  29. self.__database.commit()
  30. self.__database.close()
  31. except sqlite3.OperationalError:
  32. print >> sys.stderr, _("""The Video \"%s\" has been downloaded but the status could not be updated in the database. Please check what went wrong and correct it""" %self.title)
  33. return True
  34. else:
  35. return False
  36. def AssignDBID(self, id):
  37. self.__ID = id