Advertisement
adamchilcott

adblock2polipo.py

Jan 4th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # convert adblock ruleset into polipo-forbidden format
  4.  
  5. if __name__ == "__main__":
  6.  
  7.     import os
  8.     import sys
  9.     import re
  10.  
  11.     if len(sys.argv) == 1:
  12.         sys.exit("Usage: %s <adblockrules>" % os.path.basename(sys.argv[0]))
  13.  
  14.     if not os.path.exists(sys.argv[1]):
  15.         sys.exit("The rules file (%s) doesn't exist" % sys.argv[1])
  16.  
  17.     fhandle = file(sys.argv[1])
  18.     lines = fhandle.readlines()
  19.     fhandle.close()
  20.  
  21.     dollar_re = re.compile("(.*?)\$.*")
  22.  
  23.     for line in lines:
  24.         if line:
  25.             if (line[0] in ("[", "!", "~", "#", "@") or
  26.                 line.startswith("/adverti") or
  27.                 "##" in line):
  28.                 continue
  29.             line = dollar_re.sub(r"\1", line)
  30. #           line = line.replace("|http://", "")
  31.             line = line.replace("|", "")
  32.             line = line.replace("||", "")
  33.             line = line.replace(".", r"\.")
  34.             line = line.replace("*", ".*")
  35.             line = line.replace("?", r"\?")
  36.             line = line.replace("^", r"[\/:\.=&\?\+\-\ ]+")
  37. #           line = line.replace("&", r"\&")
  38. #           line = line.replace("+", r"\+")
  39. #           line = line.replace("-", r"\-")
  40. #           line = line.replace(";", r"\;")
  41. #           line = line.replace("=", r"\=")
  42. #           line = line.replace("/", r"\/")
  43.             print(line.strip())
  44.     print("")
  45.  
  46. #############
  47. # START NOTES
  48. #############
  49.  
  50. ## Reference:
  51. ## <https://sites.google.com/site/monkeesage/files/>
  52.  
  53. ## Example:
  54. ## wget https://fanboy.co.nz/r/fanboy-ultimate.txt
  55. ## python2 adblock2polipo.py fanboy-ultimate.txt > fanboy-ultimate.polipoid
  56.  
  57. ###########
  58. # END NOTES
  59. ###########
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement