Fixed: Exclude domains in the whitelist
authorFrancoGag <redacted>
Sat, 7 Nov 2015 20:01:55 +0000 (17:01 -0300)
committerFrancoGag <redacted>
Sat, 7 Nov 2015 20:01:55 +0000 (17:01 -0300)
It still can be improved but now it's working as it should.
Not tested with Python 3. Working with Python 2.7.10

updateHostsFile.py

index fc460aa1fb16fc3739a3b171c7bd9ef8724b0441..f151df34008279593a9fc15ef6720d924db777fa 100755 (executable)
@@ -78,16 +78,11 @@ README_TEMPLATE = os.path.join(BASEDIR_PATH, 'readme_template.md')
 README_FILE = os.path.join(BASEDIR_PATH, 'readme.md')
 TARGET_HOST = '0.0.0.0'
 WHITELIST_FILE = os.path.join(BASEDIR_PATH, 'whitelist')
+HOSTS_FILE = os.path.join(BASEDIR_PATH, 'hosts')
 
 # Exclusions
 EXCLUSION_PATTERN = '([a-zA-Z\d-]+\.){0,}' #append domain the end
-
-# Exclutions from whitelist file
 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_EXCLUSIONS = ['hulu.com']
@@ -98,11 +93,11 @@ numberOfRules = 0
 
 def main():
        promptForUpdate()
-       excludeFromFile()
        promptForExclusions()
        mergeFile = createInitialFile()
        finalFile = removeDups(mergeFile)
        finalizeFile(finalFile)
+       excludeFromFile()
        updateReadme(numberOfRules)
        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
 def excludeFromFile():
-       for domain in EXCLUSIONS:
-               if (domain != '' and not domain.startswith("#")):
-                       domainRegex = re.compile("www\d{0,3}[.]|https?")
-                       if not (domainRegex.match(domain)):
-                               excludeDomain(domain)
+    global numberOfRules
+    if os.path.isfile(WHITELIST_FILE):
+       with open(WHITELIST_FILE, "r") as ins:
+               for line in ins:
+                       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
 def promptForUpdate():
@@ -131,7 +152,7 @@ def promptForExclusions():
        if (response == "yes"):
                displayExclusionOptions()
        else:
-               print ('OK, we won\'t exclude any domains.')
+               print ('OK, we\'ll only exclude domains in the whitelist.')
 
 def promptForMoreCustomExclusions():
        response = query_yes_no("Do you have more domains you want to enter?")
git clone https://git.99rst.org/PROJECT