Last active
January 9, 2023 04:35
-
-
Save andreacioni/8b4662f551ce3e2b052905977bb7ea30 to your computer and use it in GitHub Desktop.
Update /etc/pihole/adlist.list of Pi-Hole automatically using the list provided by the WaLLy3K’s blocklist compilation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
''' | |
Copyright 2018 Andrea Cioni | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the 'Software'), | |
to deal in the Software without restriction, including without limitation the | |
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
sell copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included | |
in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR | |
THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
This Python 3 script is used to update /etc/pihole/adlist.list of Pi-Hole automatically | |
using the list provided by the WaLLy3K’s blocklist compilation. | |
Dependencies: | |
- urllib3 | |
- certifi | |
Minimum Python version: 3.4 | |
Enjoy! | |
''' | |
##### PARAMETERS ##### | |
PIHOLE_BIN_PATH= '/usr/local/bin/pihole' #Only needed if you want to update adlist using gravity | |
UPDATE_SITES = ['https://v.firebog.net/hosts/lists.php?type=tick'] | |
ADLIST_FILE_PATH ='/etc/pihole/adlists.list' | |
###################### | |
import argparse | |
import urllib3 | |
import certifi | |
import subprocess | |
from io import SEEK_END | |
def load_adlist(): | |
with open(ADLIST_FILE_PATH, 'r', encoding='utf-8') as fp: | |
ad_list = fp.read() | |
print('Current adlist.list file content: {}'.format(ad_list)) | |
return ad_list | |
def insert_single_line(new_url, old_adlist): | |
print('Going to add {} to /etc/pihole/adlist.list'.format(ad_block_list_url)) | |
if new_url in old_adlist: | |
print('URL already in adlist') | |
else: | |
with open(ADLIST_FILE_PATH, 'a', encoding='utf-8') as fp: | |
fp.seek(SEEK_END) | |
fp.write('\n' + new_url) | |
old_adlist += '\n' + new_url | |
print('URL inserted') | |
print('Done\n') | |
def gravity_update(): | |
print('Executing: pihole -g') | |
if subprocess.call([PIHOLE_BIN_PATH, '-g']) != 0: | |
print('There was an error on gravity update') | |
else: | |
print('Gravity update done') | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-g', '--gravity', help='update list using gravity after update', action='store_true') | |
args = parser.parse_args() | |
print('Start updating Pi-Hole ad blocklists') | |
print('Loading current adlist') | |
current_adlist = load_adlist() | |
http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where()) | |
for up_site in UPDATE_SITES: | |
print('Downloding list from: {}'.format(up_site)) | |
r = http.request('GET', up_site) | |
if r.status == 200: | |
str_data = r.data.decode('utf-8') | |
print('List downloaded:\n{}'.format(str_data)) | |
print('Extracting data') | |
splited_data = [s for s in str_data.split('\n') if s.startswith('http')] | |
print('New ad-block lists entries: {}'.format(len(splited_data))) | |
for ad_block_list_url in splited_data: | |
insert_single_line(ad_block_list_url, current_adlist) | |
else: | |
print('There was an error while downloading list. (Code:{})'.format(r.status)) | |
if args.gravity: | |
print('Updating using gravity') | |
gravity_update() | |
else: | |
print('Skipping update') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment