mirror of
https://github.com/StevenBlack/hosts.git
synced 2024-07-07 13:02:27 +02:00
Fixed: Exclude domains in the whitelist
It still can be improved but now it's working as it should. Not tested with Python 3. Working with Python 2.7.10
This commit is contained in:
parent
58e5277b25
commit
6a07a22b61
@ -78,16 +78,11 @@ README_TEMPLATE = os.path.join(BASEDIR_PATH, 'readme_template.md')
|
|||||||
README_FILE = os.path.join(BASEDIR_PATH, 'readme.md')
|
README_FILE = os.path.join(BASEDIR_PATH, 'readme.md')
|
||||||
TARGET_HOST = '0.0.0.0'
|
TARGET_HOST = '0.0.0.0'
|
||||||
WHITELIST_FILE = os.path.join(BASEDIR_PATH, 'whitelist')
|
WHITELIST_FILE = os.path.join(BASEDIR_PATH, 'whitelist')
|
||||||
|
HOSTS_FILE = os.path.join(BASEDIR_PATH, 'hosts')
|
||||||
|
|
||||||
# Exclusions
|
# Exclusions
|
||||||
EXCLUSION_PATTERN = '([a-zA-Z\d-]+\.){0,}' #append domain the end
|
EXCLUSION_PATTERN = '([a-zA-Z\d-]+\.){0,}' #append domain the end
|
||||||
|
|
||||||
# Exclutions from whitelist file
|
|
||||||
EXCLUSIONS = []
|
EXCLUSIONS = []
|
||||||
if os.path.isfile(WHITELIST_FILE):
|
|
||||||
with open(WHITELIST_FILE, "r") as ins:
|
|
||||||
for line in ins:
|
|
||||||
EXCLUSIONS.append(line)
|
|
||||||
|
|
||||||
# Common domains to exclude
|
# Common domains to exclude
|
||||||
COMMON_EXCLUSIONS = ['hulu.com']
|
COMMON_EXCLUSIONS = ['hulu.com']
|
||||||
@ -98,11 +93,11 @@ numberOfRules = 0
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
promptForUpdate()
|
promptForUpdate()
|
||||||
excludeFromFile()
|
|
||||||
promptForExclusions()
|
promptForExclusions()
|
||||||
mergeFile = createInitialFile()
|
mergeFile = createInitialFile()
|
||||||
finalFile = removeDups(mergeFile)
|
finalFile = removeDups(mergeFile)
|
||||||
finalizeFile(finalFile)
|
finalizeFile(finalFile)
|
||||||
|
excludeFromFile()
|
||||||
updateReadme(numberOfRules)
|
updateReadme(numberOfRules)
|
||||||
printSuccess('Success! Your shiny new hosts file has been prepared.\nIt contains ' + "{:,}".format( numberOfRules ) + ' unique entries.')
|
printSuccess('Success! Your shiny new hosts file has been prepared.\nIt contains ' + "{:,}".format( numberOfRules ) + ' unique entries.')
|
||||||
|
|
||||||
@ -110,11 +105,37 @@ def main():
|
|||||||
|
|
||||||
# Exclusion from file
|
# Exclusion from file
|
||||||
def excludeFromFile():
|
def excludeFromFile():
|
||||||
for domain in EXCLUSIONS:
|
global numberOfRules
|
||||||
if (domain != '' and not domain.startswith("#")):
|
if os.path.isfile(WHITELIST_FILE):
|
||||||
domainRegex = re.compile("www\d{0,3}[.]|https?")
|
with open(WHITELIST_FILE, "r") as ins:
|
||||||
if not (domainRegex.match(domain)):
|
for line in ins:
|
||||||
excludeDomain(domain)
|
EXCLUSIONS.append(line)
|
||||||
|
f = open(HOSTS_FILE)
|
||||||
|
output = []
|
||||||
|
for line in f:
|
||||||
|
write = 'true'
|
||||||
|
for domain in EXCLUSIONS:
|
||||||
|
if domain in line:
|
||||||
|
write = 'false'
|
||||||
|
numberOfRules -= 1
|
||||||
|
break
|
||||||
|
if (write == 'true'):
|
||||||
|
output.append(line)
|
||||||
|
f.close()
|
||||||
|
f = open(HOSTS_FILE, 'w')
|
||||||
|
f.writelines(output)
|
||||||
|
f.close()
|
||||||
|
f = open(HOSTS_FILE)
|
||||||
|
output = []
|
||||||
|
for line in f:
|
||||||
|
if 'unique entries' not in line:
|
||||||
|
output.append(line)
|
||||||
|
else:
|
||||||
|
output.append('# Merging these sources produced ' + "{:,}".format( numberOfRules ) + ' unique entries\n')
|
||||||
|
f.close()
|
||||||
|
f = open(HOSTS_FILE, 'w')
|
||||||
|
f.writelines(output)
|
||||||
|
f.close()
|
||||||
|
|
||||||
# Prompt the User
|
# Prompt the User
|
||||||
def promptForUpdate():
|
def promptForUpdate():
|
||||||
@ -131,7 +152,7 @@ def promptForExclusions():
|
|||||||
if (response == "yes"):
|
if (response == "yes"):
|
||||||
displayExclusionOptions()
|
displayExclusionOptions()
|
||||||
else:
|
else:
|
||||||
print ('OK, we won\'t exclude any domains.')
|
print ('OK, we\'ll only exclude domains in the whitelist.')
|
||||||
|
|
||||||
def promptForMoreCustomExclusions():
|
def promptForMoreCustomExclusions():
|
||||||
response = query_yes_no("Do you have more domains you want to enter?")
|
response = query_yes_no("Do you have more domains you want to enter?")
|
||||||
|
Loading…
Reference in New Issue
Block a user