123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- # This file is part of stov, written by Helmut Pozimski 2012-2014.
- #
- # 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
- from __future__ import print_function
- import os
- import subprocess
- import sys
- from lib_stov import stov_exceptions
- class conf(object):
- def __init__(self):
- """Constructor
- Constructs the conf object with some reasonable default values which
- should work on most systems, existence of a local mail server is
- assumed.
- """
- self.values = {
- "database": "stov.sqlite",
- "downloaddir": str(os.environ['HOME']) + "/stov",
- "maxvideos": "25",
- "mailhost": "localhost",
- "mailto": "root",
- "mailfrom": "stov@localhost",
- "smtpport": "25",
- "auth_needed": "no",
- "user_name": "",
- "password": "",
- "youtube-dl": "",
- "notify": "yes",
- "config_version": "7",
- "db_version": "3",
- "videocodec": "h264",
- "maxresolution": "1080p",
- "maxfails": "50",
- "check_title": "no"
- }
- self.__explanations = {
- "database": _("the name of your database file"),
- "downloaddir": _("the directory where downloaded videos are "
- "saved"),
- "maxvideos": _("the maximum number of videos to retrieve for each "
- "subscription"),
- "mailhost": _("the host of your mail server"),
- "mailto": _("the address used for notifications"),
- "mailfrom": _("the sender address of notification e-mails"),
- "smtpport": _("the port to use on your mail server"),
- "auth_needed": _("if your mail server requires authentication"),
- "user_name": _("the user name used to authenticate to your mail "
- "server"),
- "password": _("the password used to authenticate to your mail "
- "server"),
- "youtube-dl": _("the path to your youtube-dl installation"),
- "notify": _("if you want to be notified via e-mail about new "
- "videos"),
- "videocodec": _("which video codec you prefer (h264, webm or "
- "flv)"),
- "maxresolution": _("which resolution you prefer (360p, 480p, 720p "
- "or 1080p)"),
- "check_title": _("if you want to compare the title of a video "
- "with the subscription search string")
- }
- self.dbpath = str(os.environ['HOME']) + "/.stov/" + \
- self.values["database"]
- self.outputlevel = "default"
- def WriteConfig(self):
- """Writes the configuration from the dictionary into the configuration
- file for stov. The configuration is written into the home directory of
- the user by default.
- """
- try:
- configfile = open(str(os.environ['HOME']) +
- "/.stov/stov.config", "w")
- except IOError:
- raise stov_exceptions.ConfigFileWriteErrorException()
- else:
- for key, value in self.values.items():
- configfile.write(key.upper() + "=" + self.values[key] +
- "\n")
- configfile.close()
- def Initialize(self):
- """Creates the necessary directory for the stov configuration and
- calls the internal methods to create the database and the
- configuration file.
- """
- try:
- os.mkdir(str(os.environ['HOME']) + "/.stov", 0o750)
- except os.error:
- raise stov_exceptions.DirectoryCreationFailedException()
- else:
- process = subprocess.Popen(["which", "youtube-dl"],
- stdout=subprocess.PIPE)
- self.values["youtube-dl"] = process.communicate()[0].strip()
- self.WriteConfig()
- def ReadConfig(self):
- """Reads the existing configuration file and places the values in the
- dictionary. Existing values (such as default values) are overwritten.
- """
- try:
- configfile = open(str(os.environ['HOME']) +
- "/.stov/stov.config", "r")
- except IOError:
- raise stov_exceptions.ConfigFileReadErrorException()
- for lines in configfile:
- tmpline = lines.strip()
- tmplist = tmpline.split("=")
- self.values[tmplist[0].lower()] = tmplist[1]
- configfile.close()
- self.dbpath = str(os.environ['HOME']) + "/.stov/" + \
- self.values["database"]
- def CheckConfig(self):
- """Checks if the configuration is up-to-date with the running
- stov version.
- """
- try:
- currentversion = int(self.values["config_version"])
- except ValueError:
- raise stov_exceptions.InvalidConfigurationVersionException()
- self.values["config_version"] = "0"
- self.ReadConfig()
- if self.values["config_version"] == "0" \
- or int(self.values["config_version"]) < currentversion:
- self.values["config_version"] = str(currentversion)
- return False
- else:
- self.values["config_version"] = str(currentversion)
- return True
- def UpdateConfig(self):
- """Update the configuration to the latest version"""
- versionbuffer = self.values["config_version"]
- self.ReadConfig()
- self.values["config_version"] = versionbuffer
- self.WriteConfig()
- def CheckDB(self):
- """Checks the database if it is up-to-date"""
- currentdbversion = int(self.values["db_version"])
- self.values["db_version"] = "0"
- self.ReadConfig()
- if self.values["db_version"] == "0" or \
- int(self.values["db_version"]) <\
- int(currentdbversion):
- self.values["db_version"] = str(currentdbversion)
- return False
- else:
- self.values["db_version"] = str(currentdbversion)
- return True
- def GetYoutubeParameter(self):
- """Determines which itag value results from codec and resolution
- settings and returns it
- """
- itag_value = 0
- if self.values["videocodec"] == "flv":
- if self.values["maxresolution"] == "240p":
- itag_value = 5
- elif self.values["maxresolution"] == "270p":
- itag_value = 6
- elif self.values["maxresolution"] == "360p":
- itag_value = 34
- elif self.values["maxresolution"] == "480p":
- itag_value = 35
- elif self.values["videocodec"] == "webm":
- if self.values["maxresolution"] == "360p":
- itag_value = 43
- elif self.values["maxresolution"] == "480p":
- itag_value = 44
- elif self.values["maxresolution"] == "720p":
- itag_value = 45
- elif self.values["maxresolution"] == "1080p":
- itag_value = 46
- elif self.values["videocodec"] == "h264":
- if self.values["maxresolution"] == "360p":
- itag_value = 18
- elif self.values["maxresolution"] == "720p":
- itag_value = 22
- elif self.values["maxresolution"] == "1080p":
- itag_value = 37
- elif self.values["maxresolution"] == "3072p":
- itag_value = 38
- return itag_value
- def assist(self):
- """ Ask the user to set all required configuration parameters """
- print(_("This assistant will help you to perform the initial "
- "configuration of stov. \nThe default value will be "
- "displayed in brackets.\n"
- "Please specify now :\n"))
- for value in self.__explanations:
- print(self.__explanations[value] + " [" + self.values[value] +
- "]:" +
- " ")
- if sys.version_info >= (3, 0):
- user_input = input()
- else:
- user_input = raw_input()
- if user_input != "":
- self.values[value] = user_input
- self.dbpath = str(os.environ['HOME']) + "/.stov/" + \
- self.values["database"]
|