1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
import os
import sqlite3
import logging
from configparser import ConfigParser
from get_argv import config_dir
# Check if config_dir exist
if os.path.exists(config_dir) is False:
# Create config_dir directory tree
try:
os.mkdir(os.path.join(config_dir))
logging.debug("Created data directory")
except OSError:
logging.exception("The configuration directory doesn't exist and Bazarr cannot create it (permission issue?).")
exit(2)
if os.path.exists(os.path.join(config_dir, 'config')) is False:
os.mkdir(os.path.join(config_dir, 'config'))
logging.debug("Created config folder")
if os.path.exists(os.path.join(config_dir, 'db')) is False:
os.mkdir(os.path.join(config_dir, 'db'))
logging.debug("Created db folder")
if os.path.exists(os.path.join(config_dir, 'log')) is False:
os.mkdir(os.path.join(config_dir, 'log'))
logging.debug("Created log folder")
config_file = os.path.normpath(os.path.join(config_dir, 'config/config.ini'))
# if os.path.exists(os.path.join(config_dir, 'db/bazarr.db')) is True and os.path.exists(config_file) is False:
try:
# Open database connection
db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30)
c = db.cursor()
# Get general settings from database table
c.execute("SELECT * FROM table_settings_general")
general_settings = c.fetchone()
c.execute('''SELECT * FROM table_settings_auth''')
auth_settings = c.fetchone()
c.execute('''SELECT * FROM table_settings_sonarr''')
sonarr_settings = c.fetchone()
c.execute('''SELECT * FROM table_settings_radarr''')
radarr_settings = c.fetchone()
c.execute("DROP TABLE table_settings_general")
c.execute("DROP TABLE table_settings_auth")
c.execute("DROP TABLE table_settings_sonarr")
c.execute("DROP TABLE table_settings_radarr")
# Close database connection
db.close()
cfg = ConfigParser()
section = 'general'
if not cfg.has_section(section):
cfg.add_section(section)
cfg.set(section, 'ip', str(general_settings[0]))
cfg.set(section, 'port', str(general_settings[1]))
cfg.set(section, 'base_url', general_settings[2])
cfg.set(section, 'path_mappings', general_settings[3])
cfg.set(section, 'log_level', general_settings[4])
cfg.set(section, 'branch', general_settings[5])
cfg.set(section, 'auto_update', general_settings[6])
cfg.set(section, 'single_language', general_settings[9])
cfg.set(section, 'minimum_score', general_settings[10])
cfg.set(section, 'use_scenename', general_settings[11])
cfg.set(section, 'use_postprocessing', general_settings[12])
cfg.set(section, 'postprocessing_cmd', general_settings[13])
cfg.set(section, 'use_sonarr', general_settings[14])
cfg.set(section, 'use_radarr', general_settings[15])
cfg.set(section, 'path_mappings_movie', general_settings[16])
cfg.set(section, 'serie_default_enabled', general_settings[17])
cfg.set(section, 'serie_default_language', general_settings[18])
cfg.set(section, 'serie_default_hi', general_settings[19])
cfg.set(section, 'movie_default_enabled', general_settings[20])
cfg.set(section, 'movie_default_language', general_settings[21])
cfg.set(section, 'movie_default_hi', general_settings[22])
cfg.set(section, 'page_size', general_settings[23])
cfg.set(section, 'minimum_score_movie', general_settings[24])
cfg.set(section, 'use_embedded_subs', general_settings[25])
cfg.set(section, 'only_monitored', general_settings[26])
section = 'auth'
if not cfg.has_section(section):
cfg.add_section(section)
cfg.set(section, 'enabled', auth_settings[0])
cfg.set(section, 'username', auth_settings[1])
cfg.set(section, 'password', auth_settings[2])
section = 'sonarr'
if not cfg.has_section(section):
cfg.add_section(section)
cfg.set(section, 'ip', sonarr_settings[0])
cfg.set(section, 'port', str(sonarr_settings[1]))
cfg.set(section, 'base_url', sonarr_settings[2])
cfg.set(section, 'ssl', sonarr_settings[3])
cfg.set(section, 'apikey', sonarr_settings[4])
cfg.set(section, 'full_update', sonarr_settings[5])
section = 'radarr'
if not cfg.has_section(section):
cfg.add_section(section)
cfg.set(section, 'ip', radarr_settings[0])
cfg.set(section, 'port', str(radarr_settings[1]))
cfg.set(section, 'base_url', radarr_settings[2])
cfg.set(section, 'ssl', radarr_settings[3])
cfg.set(section, 'apikey', radarr_settings[4])
cfg.set(section, 'full_update', radarr_settings[5])
with open(config_file, 'w+') as configfile:
cfg.write(configfile)
logging.info('Config file succesfully migrated from database')
except sqlite3.OperationalError:
# if not os.path.exists(config_file):
if os.path.exists(config_file) is False:
cfg = ConfigParser()
section = 'general'
if not cfg.has_section(section):
cfg.add_section(section)
cfg.set(section, 'ip', "0.0.0.0")
cfg.set(section, 'port', "6767")
cfg.set(section, 'base_url', "/")
cfg.set(section, 'path_mappings', '[]')
cfg.set(section, 'log_level', "INFO")
cfg.set(section, 'branch', "master")
cfg.set(section, 'auto_update', "True")
cfg.set(section, 'single_language', "False")
cfg.set(section, 'minimum_score', "100")
cfg.set(section, 'use_scenename', "False")
cfg.set(section, 'use_postprocessing', "False")
cfg.set(section, 'postprocessing_cmd', "False")
cfg.set(section, 'use_sonarr', "False")
cfg.set(section, 'use_radarr', "False")
cfg.set(section, 'path_mappings_movie', '[]')
cfg.set(section, 'serie_default_enabled', "False")
cfg.set(section, 'serie_default_language', '')
cfg.set(section, 'serie_default_hi', "False")
cfg.set(section, 'movie_default_enabled', "False")
cfg.set(section, 'movie_default_language', '')
cfg.set(section, 'movie_default_hi', "False")
cfg.set(section, 'page_size', "25")
cfg.set(section, 'minimum_score_movie', "100")
cfg.set(section, 'use_embedded_subs', "False")
cfg.set(section, 'only_monitored', "False")
section = 'auth'
if not cfg.has_section(section):
cfg.add_section(section)
cfg.set(section, 'enabled', "False")
cfg.set(section, 'username', "")
cfg.set(section, 'password', "")
section = 'sonarr'
if not cfg.has_section(section):
cfg.add_section(section)
cfg.set(section, 'ip', "127.0.0.1")
cfg.set(section, 'port', "8989")
cfg.set(section, 'base_url', "/")
cfg.set(section, 'ssl', "False")
cfg.set(section, 'apikey', "")
cfg.set(section, 'full_update', "Daily")
section = 'radarr'
if not cfg.has_section(section):
cfg.add_section(section)
cfg.set(section, 'ip', "127.0.0.1")
cfg.set(section, 'port', "7878")
cfg.set(section, 'base_url', "/")
cfg.set(section, 'ssl', "False")
cfg.set(section, 'apikey', "")
cfg.set(section, 'full_update', "Daily")
with open(config_file, 'w+') as configfile:
cfg.write(configfile)
logging.info('Config file created successfully')
try:
# Get SQL script from file
fd = open(os.path.join(os.path.dirname(__file__), 'create_db.sql'), 'r')
script = fd.read()
# Close SQL script file
fd.close()
# Open database connection
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
c = db.cursor()
# Execute script and commit change to database
c.executescript(script)
# Close database connection
db.close()
logging.info('Database created successfully')
except:
pass
|