1
0

configuration.py 9.5 KB

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