-
Notifications
You must be signed in to change notification settings - Fork 30
/
UltimateBlockList.py
67 lines (56 loc) · 2.01 KB
/
UltimateBlockList.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
#!/usr/bin/env python
#Written for Pyhon 2.7.5
#Written by: Adam Walsh
#Written on 7/6/14
#Maintained @ https://github.com/walshie4/Ultimate-Blocklist
import requests
import urllib
from bs4 import BeautifulSoup as mksoup
import gzip
import os
token = os.getenv('DROPBOX_ACCESS_TOKEN')
if token:
from dropbox.client import DropboxClient
db_client = DropboxClient(token)
BASE = "https://www.iblocklist.com"
def get_value_from(url):
soup = mksoup(requests.get(BASE + url).text)
return str(soup.find_all("input")[-1]).split("\"")[-2]
def process(url):
try:
handle = urllib.urlopen(url)
except Exception as e:
print("URL open failed! Exception following:")
print(e)
return
with open('ultBlockList.tmp.gz', 'wb') as out:
while True:
data = handle.read(1024)
if len(data) == 0: break
out.write(data)
with gzip.open('ultBlockList.tmp.gz') as contents:
with open("blocklist.txt", "a+") as f:
for line in contents:
f.write(line)
os.remove('ultBlockList.tmp.gz')
if __name__=="__main__":
print("Getting list page")
soup = mksoup(requests.get("https://www.iblocklist.com/lists.php").text)
links = {}#dict of name of list -> its url
for row in soup.find_all("tr")[1:]:#for each table row
section = str(list(row.children)[0])
pieces = section.split("\"")
links[pieces[4].split("<")[0][1:]] = pieces[3]
for link in links:#download and combine files
print "Downloading " + link + " blocklist."
value = get_value_from(links[link])
if value == "subscription":
print "Blocklist is not available for free download D:"
elif value == "unavailable":
print "URL is unavailable"
else:#download and add this sucker
process(value)
if token:
file = open('blocklist.txt', 'rb')
response = db_client.put_file('/blocklist.txt', file, overwrite=True)
print 'Uploaded to Dropbox!'