-
Notifications
You must be signed in to change notification settings - Fork 5
/
privbloxy.py
94 lines (79 loc) · 3.73 KB
/
privbloxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
# -----------------------------------------------------------
# grabs list of ad servers from pgl.yoyo.org and
# sets up privoxy to block the enumerated domains
# *note*: be sure to update variables down there ;]
#
# usage: python privbloxy.py
# -----------------------------------------------------------
import urllib2
# -----------------------------------------------------------
# re-write privoxy config, but add in the ads actionfile
# -----------------------------------------------------------
def add_ads_action(dir, lines, user_line):
line_num = 0
config = open(dir + "/config.txt", "w")
for line in lines:
config.write(line)
if line_num == user_line:
config.write("actionsfile ads.action # Blocking ad servers enumerated on pgl.yoyo.org\n")
line_num += 1
config.close()
# -----------------------------------------------------------
# determine whether ads actionfile entry exists or update
# -----------------------------------------------------------
def check_config(dir):
curr_line = 0
user_line = 0
config_lines = open(privoxy_dir + "/config.txt", "r").readlines()
for line in config_lines:
# -----------------------------------------------------------
# check for ads action file; bail if present
# -----------------------------------------------------------
if line.startswith("actionsfile ads.action"):
break
# -----------------------------------------------------------
# line after user actions was not ads actions
# -----------------------------------------------------------
if user_line > 0:
add_ads_action(privoxy_dir, config_lines, user_line)
break
# -----------------------------------------------------------
# note location of user actions
# -----------------------------------------------------------
if line.startswith("actionsfile user.action"):
user_line = curr_line
curr_line += 1
# -----------------------------------------------------------
# parse and set actionfile contents
# -----------------------------------------------------------
def parse_adfile(adfile):
adlist = "{+block{Ad domains from pgl.yoyo.org} +handle-as-empty-document}\n"
lines = adfile.readlines()
for line in lines:
# -----------------------------------------------------------
# skip comment lines starting with '#' and blank lines
# -----------------------------------------------------------
if line.startswith("#") or not line.strip():
continue
# -----------------------------------------------------------
# domains are separated by commas
# -----------------------------------------------------------
adlist = adlist + line.replace(",", "\n")
return adlist
# -----------------------------------------------------------
# download latest ads list, parse, and write to file
# -----------------------------------------------------------
def set_adlist(dir, url):
adfile = urllib2.urlopen(url)
adlist = parse_adfile(adfile)
actionfile = open(dir + "/ads.action", "w")
actionfile.write(adlist)
actionfile.close()
# -----------------------------------------------------------
# configuration stuffs, adjust accordingly
# -----------------------------------------------------------
privoxy_dir = "C:/Program Files/Privoxy"
adlist_url = "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=one-line&showintro=0&mimetype=plaintext"
check_config(privoxy_dir)
set_adlist(privoxy_dir, adlist_url)