@@ -11,35 +11,39 @@ def fetch_url(url):
11
11
if not url :
12
12
return
13
13
14
- headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0' }
14
+ headers = {
15
+ "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0"
16
+ }
15
17
16
- print (' [i] Fetching:' , url )
18
+ print (" [i] Fetching:" , url )
17
19
18
20
try :
19
21
response = urlopen (Request (url , headers = headers ))
20
22
except HTTPError as e :
21
- print (' [E] HTTP Error:' , e .code , ' whilst fetching' , url )
23
+ print (" [E] HTTP Error:" , e .code , " whilst fetching" , url )
22
24
return
23
25
except URLError as e :
24
- print (' [E] URL Error:' , e .reason , ' whilst fetching' , url )
26
+ print (" [E] URL Error:" , e .reason , " whilst fetching" , url )
25
27
return
26
28
27
29
# Read and decode
28
- response = response .read ().decode (' UTF-8' ).replace (' \r \n ' , ' \n ' )
30
+ response = response .read ().decode (" UTF-8" ).replace (" \r \n " , " \n " )
29
31
30
32
# If there is data
31
33
if response :
32
34
# Strip leading and trailing whitespace
33
- response = ' \n ' .join (x for x in map (str .strip , response .splitlines ()))
35
+ response = " \n " .join (x for x in map (str .strip , response .splitlines ()))
34
36
35
37
# Return the hosts
36
38
return response
37
39
38
40
39
- url_regexps_remote = 'https://raw.githubusercontent.com/nickspaargaren/no-google/master/regex.list'
40
- install_comment = 'github.com/nickspaargaren/no-google'
41
+ url_regexps_remote = (
42
+ "https://raw.githubusercontent.com/nickspaargaren/no-google/master/regex.list"
43
+ )
44
+ install_comment = "github.com/nickspaargaren/no-google"
41
45
42
- cmd_restart = [' pihole' , ' restartdns' , ' reload' ]
46
+ cmd_restart = [" pihole" , " restartdns" , " reload" ]
43
47
44
48
db_exists = False
45
49
conn = None
@@ -61,73 +65,83 @@ def fetch_url(url):
61
65
62
66
# Check to see whether the default "pihole" docker container is active
63
67
try :
64
- docker_id = subprocess .run (['docker' , 'ps' , '--filter' , 'name=pihole' , '-q' ],
65
- stdout = subprocess .PIPE , universal_newlines = True ).stdout .strip ()
68
+ docker_id = subprocess .run (
69
+ ["docker" , "ps" , "--filter" , "name=pihole" , "-q" ],
70
+ stdout = subprocess .PIPE ,
71
+ universal_newlines = True ,
72
+ ).stdout .strip ()
66
73
# Exception for if docker is not installed
67
74
except FileNotFoundError :
68
75
pass
69
76
70
77
# If a pihole docker container was found, locate the first mount
71
78
if docker_id :
72
- docker_mnt = subprocess .run (['docker' , 'inspect' , '--format' , '{{ (json .Mounts) }}' , docker_id ],
73
- stdout = subprocess .PIPE , universal_newlines = True ).stdout .strip ()
79
+ docker_mnt = subprocess .run (
80
+ ["docker" , "inspect" , "--format" , "{{ (json .Mounts) }}" , docker_id ],
81
+ stdout = subprocess .PIPE ,
82
+ universal_newlines = True ,
83
+ ).stdout .strip ()
74
84
# Convert output to JSON and iterate through each dict
75
85
for json_dict in json .loads (docker_mnt ):
76
86
# If this mount's destination is /etc/pihole
77
- if json_dict [' Destination' ] == r' /etc/pihole' :
87
+ if json_dict [" Destination" ] == r" /etc/pihole" :
78
88
# Use the source path as our target
79
- docker_mnt_src = json_dict [' Source' ]
89
+ docker_mnt_src = json_dict [" Source" ]
80
90
break
81
91
82
92
# If we successfully found the mount
83
93
if docker_mnt_src :
84
- print (' [i] Running in docker installation mode' )
94
+ print (" [i] Running in docker installation mode" )
85
95
# Prepend restart commands
86
- cmd_restart [0 :0 ] = [' docker' , ' exec' , '-i' , ' pihole' ]
96
+ cmd_restart [0 :0 ] = [" docker" , " exec" , "-i" , " pihole" ]
87
97
else :
88
- print (' [i] Running in physical installation mode ' )
98
+ print (" [i] Running in physical installation mode " )
89
99
90
100
# Set paths
91
- path_pihole = docker_mnt_src if docker_mnt_src else r' /etc/pihole'
92
- path_legacy_regex = os .path .join (path_pihole , ' regex.list' )
93
- path_legacy_mmotti_regex = os .path .join (path_pihole , ' mmotti-regex.list' )
94
- path_pihole_db = os .path .join (path_pihole , ' gravity.db' )
101
+ path_pihole = docker_mnt_src if docker_mnt_src else r" /etc/pihole"
102
+ path_legacy_regex = os .path .join (path_pihole , " regex.list" )
103
+ path_legacy_mmotti_regex = os .path .join (path_pihole , " mmotti-regex.list" )
104
+ path_pihole_db = os .path .join (path_pihole , " gravity.db" )
95
105
96
106
# Check that pi-hole path exists
97
107
if os .path .exists (path_pihole ):
98
- print (' [i] Pi-hole path exists' )
108
+ print (" [i] Pi-hole path exists" )
99
109
else :
100
- print (f' [e] { path_pihole } was not found' )
110
+ print (f" [e] { path_pihole } was not found" )
101
111
exit (1 )
102
112
103
113
# Check for write access to /etc/pihole
104
114
if os .access (path_pihole , os .X_OK | os .W_OK ):
105
- print (f' [i] Write access to { path_pihole } verified' )
115
+ print (f" [i] Write access to { path_pihole } verified" )
106
116
else :
107
- print (f'[e] Write access is not available for { path_pihole } . Please run as root or other privileged user' )
117
+ print (
118
+ f"[e] Write access is not available for { path_pihole } . Please run as root or other privileged user"
119
+ )
108
120
exit (1 )
109
121
110
122
# Determine whether we are using DB or not
111
123
if os .path .isfile (path_pihole_db ) and os .path .getsize (path_pihole_db ) > 0 :
112
124
db_exists = True
113
- print (' [i] DB detected' )
125
+ print (" [i] DB detected" )
114
126
else :
115
- print (' [i] Legacy regex.list detected' )
127
+ print (" [i] Legacy regex.list detected" )
116
128
117
129
# Fetch the remote regexps
118
130
str_regexps_remote = fetch_url (url_regexps_remote )
119
131
120
132
# If regexps were fetched, remove any comments and add to set
121
133
if str_regexps_remote :
122
- regexps_remote .update (x for x in map (str .strip , str_regexps_remote .splitlines ()) if x and x [:1 ] != '#' )
123
- print (f'[i] { len (regexps_remote )} regexps collected from { url_regexps_remote } ' )
134
+ regexps_remote .update (
135
+ x for x in map (str .strip , str_regexps_remote .splitlines ()) if x and x [:1 ] != "#"
136
+ )
137
+ print (f"[i] { len (regexps_remote )} regexps collected from { url_regexps_remote } " )
124
138
else :
125
- print (' [i] No remote regexps were found.' )
139
+ print (" [i] No remote regexps were found." )
126
140
exit (1 )
127
141
128
142
if db_exists :
129
143
# Create a DB connection
130
- print (f' [i] Connecting to { path_pihole_db } ' )
144
+ print (f" [i] Connecting to { path_pihole_db } " )
131
145
132
146
try :
133
147
conn = sqlite3 .connect (path_pihole_db )
@@ -139,93 +153,107 @@ def fetch_url(url):
139
153
c = conn .cursor ()
140
154
141
155
# Add / update remote regexps
142
- print ('[i] Adding / updating regexps in the DB' )
143
-
144
- c .executemany ('INSERT OR IGNORE INTO domainlist (type, domain, enabled, comment) '
145
- 'VALUES (3, ?, 1, ?)' ,
146
- [(x , install_comment ) for x in sorted (regexps_remote )])
147
- c .executemany ('UPDATE domainlist '
148
- 'SET comment = ? WHERE domain in (?) AND comment != ?' ,
149
- [(install_comment , x , install_comment ) for x in sorted (regexps_remote )])
156
+ print ("[i] Adding / updating regexps in the DB" )
157
+
158
+ c .executemany (
159
+ "INSERT OR IGNORE INTO domainlist (type, domain, enabled, comment) "
160
+ "VALUES (3, ?, 1, ?)" ,
161
+ [(x , install_comment ) for x in sorted (regexps_remote )],
162
+ )
163
+ c .executemany (
164
+ "UPDATE domainlist " "SET comment = ? WHERE domain in (?) AND comment != ?" ,
165
+ [(install_comment , x , install_comment ) for x in sorted (regexps_remote )],
166
+ )
150
167
151
168
conn .commit ()
152
169
153
170
# Fetch all current mmotti regexps in the local db
154
- c .execute ('SELECT domain FROM domainlist WHERE type = 3 AND comment = ?' , (install_comment ,))
171
+ c .execute (
172
+ "SELECT domain FROM domainlist WHERE type = 3 AND comment = ?" ,
173
+ (install_comment ,),
174
+ )
155
175
regexps_mmotti_local_results = c .fetchall ()
156
176
regexps_mmotti_local .update ([x [0 ] for x in regexps_mmotti_local_results ])
157
177
158
178
# Remove any local entries that do not exist in the remote list
159
179
# (will only work for previous installs where we've set the comment field)
160
- print (' [i] Identifying obsolete regexps' )
180
+ print (" [i] Identifying obsolete regexps" )
161
181
regexps_remove = regexps_mmotti_local .difference (regexps_remote )
162
182
163
183
if regexps_remove :
164
- print ('[i] Removing obsolete regexps' )
165
- c .executemany ('DELETE FROM domainlist WHERE type = 3 AND domain in (?)' , [(x ,) for x in regexps_remove ])
184
+ print ("[i] Removing obsolete regexps" )
185
+ c .executemany (
186
+ "DELETE FROM domainlist WHERE type = 3 AND domain in (?)" ,
187
+ [(x ,) for x in regexps_remove ],
188
+ )
166
189
conn .commit ()
167
190
168
191
# Delete mmotti-regex.list as if we've migrated to the db, it's no longer needed
169
192
if os .path .exists (path_legacy_mmotti_regex ):
170
193
os .remove (path_legacy_mmotti_regex )
171
194
172
- print (' [i] Restarting Pi-hole' )
195
+ print (" [i] Restarting Pi-hole" )
173
196
subprocess .run (cmd_restart , stdout = subprocess .DEVNULL )
174
197
175
198
# Prepare final result
176
- print (' [i] Done - Please see your installed regexps below\n ' )
199
+ print (" [i] Done - Please see your installed regexps below\n " )
177
200
178
- c .execute (' Select domain FROM domainlist WHERE type = 3' )
201
+ c .execute (" Select domain FROM domainlist WHERE type = 3" )
179
202
final_results = c .fetchall ()
180
203
regexps_local .update (x [0 ] for x in final_results )
181
204
182
- print (* sorted (regexps_local ), sep = ' \n ' )
205
+ print (* sorted (regexps_local ), sep = " \n " )
183
206
184
207
conn .close ()
185
208
186
209
else :
187
210
# If regex.list exists and is not empty
188
211
# Read it and add to a set
189
212
if os .path .isfile (path_legacy_regex ) and os .path .getsize (path_legacy_regex ) > 0 :
190
- print (' [i] Collecting existing entries from regex.list' )
191
- with open (path_legacy_regex , 'r' ) as fRead :
192
- regexps_local .update (x for x in map (str .strip , fRead ) if x and x [:1 ] != '#' )
213
+ print (" [i] Collecting existing entries from regex.list" )
214
+ with open (path_legacy_regex , "r" ) as fRead :
215
+ regexps_local .update (x for x in map (str .strip , fRead ) if x and x [:1 ] != "#" )
193
216
194
217
# If the local regexp set is not empty
195
218
if regexps_local :
196
- print (f' [i] { len (regexps_local )} existing regexps identified' )
219
+ print (f" [i] { len (regexps_local )} existing regexps identified" )
197
220
# If we have a record of a previous legacy install
198
- if os .path .isfile (path_legacy_mmotti_regex ) and os .path .getsize (path_legacy_mmotti_regex ) > 0 :
199
- print ('[i] Existing mmotti-regex install identified' )
221
+ if (
222
+ os .path .isfile (path_legacy_mmotti_regex )
223
+ and os .path .getsize (path_legacy_mmotti_regex ) > 0
224
+ ):
225
+ print ("[i] Existing mmotti-regex install identified" )
200
226
# Read the previously installed regexps to a set
201
- with open (path_legacy_mmotti_regex , 'r' ) as fOpen :
202
- regexps_legacy_mmotti .update (x for x in map (str .strip , fOpen ) if x and x [:1 ] != '#' )
227
+ with open (path_legacy_mmotti_regex , "r" ) as fOpen :
228
+ regexps_legacy_mmotti .update (
229
+ x for x in map (str .strip , fOpen ) if x and x [:1 ] != "#"
230
+ )
203
231
204
232
if regexps_legacy_mmotti :
205
- print (' [i] Removing previously installed regexps' )
233
+ print (" [i] Removing previously installed regexps" )
206
234
regexps_local .difference_update (regexps_legacy_mmotti )
207
235
208
236
# Add remote regexps to local regexps
209
- print (f' [i] Syncing with { url_regexps_remote } ' )
237
+ print (f" [i] Syncing with { url_regexps_remote } " )
210
238
regexps_local .update (regexps_remote )
211
239
212
240
# Output to regex.list
213
- print (f' [i] Outputting { len (regexps_local )} regexps to { path_legacy_regex } ' )
214
- with open (path_legacy_regex , 'w' ) as fWrite :
241
+ print (f" [i] Outputting { len (regexps_local )} regexps to { path_legacy_regex } " )
242
+ with open (path_legacy_regex , "w" ) as fWrite :
215
243
for line in sorted (regexps_local ):
216
- fWrite .write (f' { line } \n ' )
244
+ fWrite .write (f" { line } \n " )
217
245
218
246
# Output mmotti remote regexps to mmotti-regex.list
219
247
# for future install / uninstall
220
- with open (path_legacy_mmotti_regex , 'w' ) as fWrite :
248
+ with open (path_legacy_mmotti_regex , "w" ) as fWrite :
221
249
for line in sorted (regexps_remote ):
222
- fWrite .write (f' { line } \n ' )
250
+ fWrite .write (f" { line } \n " )
223
251
224
- print (' [i] Restarting Pi-hole' )
252
+ print (" [i] Restarting Pi-hole" )
225
253
subprocess .run (cmd_restart , stdout = subprocess .DEVNULL )
226
254
227
255
# Prepare final result
228
- print (' [i] Done - Please see your installed regexps below\n ' )
229
- with open (path_legacy_regex , 'r' ) as fOpen :
256
+ print (" [i] Done - Please see your installed regexps below\n " )
257
+ with open (path_legacy_regex , "r" ) as fOpen :
230
258
for line in fOpen :
231
- print (line , end = '' )
259
+ print (line , end = "" )
0 commit comments