1
0

configuration.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. import sys
  20. from lib_stov import stov_exceptions
  21. class conf(object):
  22. def __init__(self):
  23. """Constructor
  24. Constructs the conf object with some reasonable default values which
  25. should work on most systems, existence of a local mail server is
  26. assumed.
  27. """
  28. self.values = {
  29. "database": "stov.sqlite",
  30. "downloaddir": str(os.environ['HOME']) + "/stov",
  31. "maxvideos": "25",
  32. "mailhost": "localhost",
  33. "mailto": "root",
  34. "mailfrom": "stov@localhost",
  35. "smtpport": "25",
  36. "auth_needed": "no",
  37. "user_name": "",
  38. "password": "",
  39. "youtube-dl": "",
  40. "notify": "yes",
  41. "config_version": "7",
  42. "db_version": "3",
  43. "videocodec": "h264",
  44. "maxresolution": "1080p",
  45. "maxfails": "50",
  46. "check_title": "no"
  47. }
  48. self.__explanations = {
  49. "database": _("the name of your database file"),
  50. "downloaddir": _("the directory where downloaded videos are "
  51. "saved"),
  52. "maxvideos": _("the maximum number of videos to retrieve for each "
  53. "subscription"),
  54. "mailhost": _("the host of your mail server"),
  55. "mailto": _("the address used for notifications"),
  56. "mailfrom": _("the sender address of notification e-mails"),
  57. "smtpport": _("the port to use on your mail server"),
  58. "auth_needed": _("if your mail server requires authentication"),
  59. "user_name": _("the user name used to authenticate to your mail "
  60. "server"),
  61. "password": _("the password used to authenticate to your mail "
  62. "server"),
  63. "youtube-dl": _("the path to your youtube-dl installation"),
  64. "notify": _("if you want to be notified via e-mail about new "
  65. "videos"),
  66. "videocodec": _("which video codec you prefer (h264, webm or "
  67. "flv)"),
  68. "maxresolution": _("which resolution you prefer (360p, 480p, 720p "
  69. "or 1080p)"),
  70. "check_title": _("if you want to compare the title of a video "
  71. "with the subscription search string")
  72. }
  73. self.dbpath = str(os.environ['HOME']) + "/.stov/" + \
  74. self.values["database"]
  75. self.outputlevel = "default"
  76. def WriteConfig(self):
  77. """Writes the configuration from the dictionary into the configuration
  78. file for stov. The configuration is written into the home directory of
  79. the user by default.
  80. """
  81. try:
  82. configfile = open(str(os.environ['HOME']) +
  83. "/.stov/stov.config", "w")
  84. except IOError:
  85. raise stov_exceptions.ConfigFileWriteErrorException()
  86. else:
  87. for key, value in self.values.items():
  88. configfile.write(key.upper() + "=" + self.values[key] +
  89. "\n")
  90. configfile.close()
  91. def Initialize(self):
  92. """Creates the necessary directory for the stov configuration and
  93. calls the internal methods to create the database and the
  94. configuration file.
  95. """
  96. try:
  97. os.mkdir(str(os.environ['HOME']) + "/.stov", 0o750)
  98. except os.error:
  99. raise stov_exceptions.DirectoryCreationFailedException()
  100. else:
  101. process = subprocess.Popen(["which", "youtube-dl"],
  102. stdout=subprocess.PIPE)
  103. self.values["youtube-dl"] = process.communicate()[0].strip()
  104. self.WriteConfig()
  105. def ReadConfig(self):
  106. """Reads the existing configuration file and places the values in the
  107. dictionary. Existing values (such as default values) are overwritten.
  108. """
  109. try:
  110. configfile = open(str(os.environ['HOME']) +
  111. "/.stov/stov.config", "r")
  112. except IOError:
  113. raise stov_exceptions.ConfigFileReadErrorException()
  114. for lines in configfile:
  115. tmpline = lines.strip()
  116. tmplist = tmpline.split("=")
  117. self.values[tmplist[0].lower()] = tmplist[1]
  118. configfile.close()
  119. self.dbpath = str(os.environ['HOME']) + "/.stov/" + \
  120. self.values["database"]
  121. def CheckConfig(self):
  122. """Checks if the configuration is up-to-date with the running
  123. stov version.
  124. """
  125. try:
  126. currentversion = int(self.values["config_version"])
  127. except ValueError:
  128. raise stov_exceptions.InvalidConfigurationVersionException()
  129. self.values["config_version"] = "0"
  130. self.ReadConfig()
  131. if self.values["config_version"] == "0" \
  132. or int(self.values["config_version"]) < currentversion:
  133. self.values["config_version"] = str(currentversion)
  134. return False
  135. else:
  136. self.values["config_version"] = str(currentversion)
  137. return True
  138. def UpdateConfig(self):
  139. """Update the configuration to the latest version"""
  140. versionbuffer = self.values["config_version"]
  141. self.ReadConfig()
  142. self.values["config_version"] = versionbuffer
  143. self.WriteConfig()
  144. def CheckDB(self):
  145. """Checks the database if it is up-to-date"""
  146. currentdbversion = int(self.values["db_version"])
  147. self.values["db_version"] = "0"
  148. self.ReadConfig()
  149. if self.values["db_version"] == "0" or \
  150. int(self.values["db_version"]) <\
  151. int(currentdbversion):
  152. self.values["db_version"] = str(currentdbversion)
  153. return False
  154. else:
  155. self.values["db_version"] = str(currentdbversion)
  156. return True
  157. def GetYoutubeParameter(self):
  158. """Determines which itag value results from codec and resolution
  159. settings and returns it
  160. """
  161. itag_value = 0
  162. if self.values["videocodec"] == "flv":
  163. if self.values["maxresolution"] == "240p":
  164. itag_value = 5
  165. elif self.values["maxresolution"] == "270p":
  166. itag_value = 6
  167. elif self.values["maxresolution"] == "360p":
  168. itag_value = 34
  169. elif self.values["maxresolution"] == "480p":
  170. itag_value = 35
  171. elif self.values["videocodec"] == "webm":
  172. if self.values["maxresolution"] == "360p":
  173. itag_value = 43
  174. elif self.values["maxresolution"] == "480p":
  175. itag_value = 44
  176. elif self.values["maxresolution"] == "720p":
  177. itag_value = 45
  178. elif self.values["maxresolution"] == "1080p":
  179. itag_value = 46
  180. elif self.values["videocodec"] == "h264":
  181. if self.values["maxresolution"] == "360p":
  182. itag_value = 18
  183. elif self.values["maxresolution"] == "720p":
  184. itag_value = 22
  185. elif self.values["maxresolution"] == "1080p":
  186. itag_value = 37
  187. elif self.values["maxresolution"] == "3072p":
  188. itag_value = 38
  189. return itag_value
  190. def assist(self):
  191. """ Ask the user to set all required configuration parameters """
  192. print(_("This assistant will help you to perform the initial "
  193. "configuration of stov. \nThe default value will be "
  194. "displayed in brackets.\n"
  195. "Please specify now :\n"))
  196. for value in self.__explanations:
  197. print(self.__explanations[value] + " [" + self.values[value] +
  198. "]:" +
  199. " ")
  200. if sys.version_info >= (3, 0):
  201. user_input = input()
  202. else:
  203. user_input = raw_input()
  204. if user_input != "":
  205. self.values[value] = user_input
  206. self.dbpath = str(os.environ['HOME']) + "/.stov/" + \
  207. self.values["database"]