configuration.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # This file is part of stov, written by Helmut Pozimski 2012-2014.
  2. #
  3. # stov is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, version 2 of the License.
  6. #
  7. # stov is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with stov. If not, see <http://www.gnu.org/licenses/>.
  14. # -*- coding: utf8 -*-
  15. from __future__ import unicode_literals
  16. from __future__ import print_function
  17. import os
  18. import subprocess
  19. from lib_stov import stov_exceptions
  20. class conf(object):
  21. def __init__(self):
  22. """Constructor
  23. Constructs the conf object with some reasonable default values which should
  24. work on most systems, existence of a local mail server is assumed.
  25. """
  26. self.values = {
  27. "database": "stov.sqlite",
  28. "downloaddir": str(os.environ['HOME']) + "/stov",
  29. "maxvideos": "25",
  30. "mailhost": "localhost",
  31. "mailto": "root",
  32. "mailfrom": "stov@localhost",
  33. "smtpport": "25",
  34. "auth_needed": "no",
  35. "user_name": "",
  36. "password": "",
  37. "youtube-dl": "",
  38. "notify": "yes",
  39. "config_version": "7",
  40. "db_version": "3",
  41. "videocodec": "h264",
  42. "maxresolution": "1080p",
  43. "maxfails": "50",
  44. "check_title": "no"
  45. }
  46. self.__explanations = {
  47. "database": _("the name of your database file"),
  48. "downloaddir": _("the directory where downloaded videos are saved"),
  49. "maxvideos": _("the maximum number of videos to retrieve for each "
  50. "subscription"),
  51. "mailhost": _("the host of your mail server"),
  52. "mailto": _("the address used for notifications"),
  53. "mailfrom": _("the sender address of notification e-mails"),
  54. "smtpport": _("the port to use on your mail server"),
  55. "auth_needed": _("if your mail server requires authentication"),
  56. "user_name": _("the user name used to authenticate to your mail server"),
  57. "password": _("the password used to authenticate to your mail server"),
  58. "youtube-dl": _("the path to your youtube-dl installation"),
  59. "notify": _("if you want to be notified via e-mail about new videos"),
  60. "videocodec": _("which video codec you prefer (h264, webm or flv)"),
  61. "maxresolution": _("which resolution you prefer (360p, 480p, 720p "
  62. "or 1080p)"),
  63. "check_title": _("if you want to compare the title of a video with the "
  64. "subscription search string")
  65. }
  66. self.dbpath = str(os.environ['HOME']) + "/.stov/" + self.values["database"]
  67. self.outputlevel = "default"
  68. def WriteConfig(self):
  69. """Writes the configuration from the dictionary into the configuration file
  70. for stov. The configuration is written into the home directory of the user
  71. by default.
  72. """
  73. try:
  74. self.configfile = open(str(os.environ['HOME']) + "/.stov/stov.config", "w")
  75. except IOError:
  76. raise stov_exceptions.ConfigFileWriteErrorException()
  77. else:
  78. for key in self.values.iterkeys():
  79. self.configfile.write(key.upper() + "=" + self.values[key] + "\n")
  80. self.configfile.close()
  81. def Initialize(self):
  82. """Creates the necessary directory for the stov configuration and calls the
  83. internal methods to create the database and the configuration file.
  84. """
  85. try:
  86. os.mkdir(str(os.environ['HOME']) + "/.stov", 0o750)
  87. except os.error:
  88. raise stov_exceptions.DirectoryCreationFailedException()
  89. else:
  90. self.values["youtube-dl"] = subprocess.Popen(["which", "youtube-dl"],
  91. stdout=subprocess.PIPE).communicate()[0].strip()
  92. self.WriteConfig()
  93. def ReadConfig(self):
  94. """Reads the existing configuration file and places the values in the
  95. dictionary. Existing values (such as default values) are overwritten.
  96. """
  97. try:
  98. self.configfile = open(str(os.environ['HOME']) + "/.stov/stov.config", "r")
  99. except IOError:
  100. raise stov_exceptions.ConfigFileReadErrorException()
  101. for lines in self.configfile:
  102. self.tmpline = lines.strip()
  103. self.tmplist = self.tmpline.split("=")
  104. self.values[self.tmplist[0].lower()] = self.tmplist[1]
  105. self.configfile.close()
  106. self.dbpath = str(os.environ['HOME']) + "/.stov/" + self.values["database"]
  107. def CheckConfig(self):
  108. """Checks if the configuration is up-to-date with the running
  109. stov version.
  110. """
  111. try:
  112. self.__currentversion = int(self.values["config_version"])
  113. except ValueError:
  114. raise stov_exceptions.InvalidConfigurationVersionException()
  115. self.values["config_version"] = "0"
  116. self.__currentdbversion = self.values["db_version"]
  117. self.ReadConfig()
  118. if self.values["config_version"] == "0" \
  119. or int(self.values["config_version"]) < self.__currentversion:
  120. self.values["config_version"] = str(self.__currentversion)
  121. return False
  122. else:
  123. self.values["config_version"] = str(self.__currentversion)
  124. return True
  125. def UpdateConfig(self):
  126. """Update the configuration to the latest version"""
  127. self.__versionbuffer = self.values["config_version"]
  128. self.ReadConfig()
  129. self.values["config_version"] = self.__versionbuffer
  130. self.WriteConfig()
  131. def CheckDB(self):
  132. """Checks the database if it is up-to-date"""
  133. self.values["db_version"] = "0"
  134. self.ReadConfig()
  135. if self.values["db_version"] == "0" or \
  136. int(self.values["db_version"]) < int(self.__currentdbversion):
  137. self.values["db_version"] = str(self.__currentversion)
  138. return False
  139. else:
  140. self.values["db_version"] = str(self.__currentversion)
  141. return True
  142. def GetYoutubeParameter(self):
  143. """Determines which itag value results from codec and resolution settings
  144. and returns it
  145. """
  146. itag_value = 0
  147. if self.values["videocodec"] == "flv":
  148. if self.values["maxresolution"] == "240p":
  149. itag_value = 5
  150. elif self.values["maxresolution"] == "270p":
  151. itag_value = 6
  152. elif self.values["maxresolution"] == "360p":
  153. itag_value = 34
  154. elif self.values["maxresolution"] == "480p":
  155. itag_value = 35
  156. elif self.values["videocodec"] == "webm":
  157. if self.values["maxresolution"] == "360p":
  158. itag_value = 43
  159. elif self.values["maxresolution"] == "480p":
  160. itag_value = 44
  161. elif self.values["maxresolution"] == "720p":
  162. itag_value = 45
  163. elif self.values["maxresolution"] == "1080p":
  164. itag_value = 46
  165. elif self.values["videocodec"] == "h264":
  166. if self.values["maxresolution"] == "360p":
  167. itag_value = 18
  168. elif self.values["maxresolution"] == "720p":
  169. itag_value = 22
  170. elif self.values["maxresolution"] == "1080p":
  171. itag_value = 37
  172. elif self.values["maxresolution"] == "3072p":
  173. itag_value = 38
  174. return itag_value
  175. def assist(self):
  176. """ Ask the user to set all required configuration parameters """
  177. print(_("This assistant will help you to perform the initial configuration"
  178. " of stov. \nThe default value will be displayed in brackets.\n"
  179. "Please specify now :\n"))
  180. for value in self.__explanations:
  181. print(self.__explanations[value] + " [" + self.values[value] + "]: ")
  182. self.__user_input = raw_input()
  183. if self.__user_input != "":
  184. self.values[value] = self.__user_input
  185. self.dbpath = str(os.environ['HOME']) + "/.stov/" + self.values["database"]