123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #
- # This file is part of stov, written by Helmut Pozimski 2012-2013.
- #
- # stov is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, version 2 of the License.
- #
- # stov is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with stov. If not, see <http://www.gnu.org/licenses/>.
- # -*- coding: utf8 -*-
- from __future__ import unicode_literals
- import os
- import sys
- import gettext
- import sqlite3
- import subprocess
- from outputhelper import printf
- class video(object):
- def __init__(self, title, description, ytid, conf, downloaded, failcount=0,
- id=0):
- self.__ID = id
- self.title = title
- self.description = description
- self.ytid = ytid
- self.__conf = conf
- self.downloaded = downloaded
- self.failcnt = int(failcount)
- def DownloadVideo(self, directory, itag_value):
- """Downloads the video by calling youtube-dl as an external process"""
- self.__targetdir = self.__conf.values["downloaddir"] + "/" + directory
- if os.access(self.__targetdir, os.F_OK) is False:
- try:
- printf(_("Creating directory %s") % self.__targetdir,
- outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
- os.makedirs(self.__targetdir, 0o750)
- except os.error:
- printf(_("Download directory does not exist \
- and can't be created. Please check your configuration and try again"),
- outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr")
- os.chdir(self.__targetdir)
- if self.downloaded == 0:
- try:
- printf(_('Downloading video "%s"') % self.title,
- outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
- if self.__conf.outputlevel == "default":
- subprocess.check_call(["youtube-dl", "--max-quality=%s"
- % itag_value, "-t", "http://www.youtube.com/watch?v=%s" % self.ytid],
- stderr=sys.stderr, stdout=open("/dev/null", "w"))
- elif self.__conf.outputlevel == "verbose":
- subprocess.check_call(["youtube-dl", "--max-quality=%s"
- % itag_value, "-t", "http://www.youtube.com/watch?v=%s" % self.ytid],
- stderr=sys.stderr, stdout=sys.stdout)
- elif self.__conf.outputlevel == "quiet":
- subprocess.check_call(["youtube-dl", "--max-quality=%s"
- % itag_value, "-t", "http://www.youtube.com/watch?v=%s" % self.ytid],
- stderr=open("/dev/null", "w"), stdout=open("/dev/null", "w"))
- try:
- self.__database = sqlite3.connect(self.__conf.dbpath)
- except sqlite3.OperationalError:
- printf(_("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,
- outputlevel="default", level=self.__conf.outputlevel,
- descriptor="stderr")
- else:
- self.__cursor = self.__database.cursor()
- printf(_('Downloaded video "%s", updating database accordingly.')
- % self.title, outputlevel="verbose", level=self.__conf.outputlevel,
- descriptor="stderr")
- self.__query = "UPDATE videos SET downloaded ='1' WHERE id = ?"
- self.__cursor.execute(self.__query, (self.__ID,))
- self.__database.commit()
- self.__database.close()
- except subprocess.CalledProcessError:
- printf(_("The video %s could not be downloaded "
- "due to some problem with youtube-dl. If this happens to more than one "
- "video, please check your youtube-dl version") % self.title,
- outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr")
- try:
- self.__database = sqlite3.connect(self.__conf.dbpath)
- except sqlite3.OperationalError:
- printf(_("The Video \"%s\" has failed "
- "downloading but the status could not be updated in the database. "
- "Please check what went wrong and correct it") % self.title,
- outputlevel="default", level=self.__conf.outputlevel,
- descriptor="stderr")
- else:
- self.__cursor = self.__database.cursor()
- self.failcnt = int(self.failcnt) + 1
- if self.failcnt >= int(self.__conf.values["maxfails"]):
- printf(_("The video \"%s\" has failed downloading too often, marking as"
- " failed") % self.title, outputlevel="default",
- level=self.__conf.outputlevel, descriptor="stderr")
- self.__statement = "UPDATE videos SET downloaded = -1 WHERE id = ?"
- self.__cursor.execute(self.__statement, (self.__ID,))
- self.__statement = "UPDATE videos SET failcnt = ? WHERE id = ?"
- self.__cursor.execute(self.__statement, (self.failcnt, self.__ID))
- self.__database.commit()
- self.__database.close()
- return False
- else:
- return True
- def AssignDBID(self, id):
- self.__ID = id
- def delete(self):
- try:
- self.__database = sqlite3.connect(self.__conf.dbpath)
- except sqlite3.OperationalError:
- printf(_("Could not connect to the database, please check settings and"
- " permissions and try again"), outputlevel="default",
- level=self.__conf.outputlevel, descriptor="stderr")
- return False
- else:
- self.__cursor = self.__database.cursor()
- self.__statement = "DELETE FROM videos where id = ?"
- self.__cursor.execute(self.__statement, (self.__ID,))
- self.__database.commit()
- self.__database.close()
- return True
|