-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpolicies_snapshot_api.py
247 lines (187 loc) · 6.48 KB
/
policies_snapshot_api.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from abc import abstractmethod
from collections import defaultdict, namedtuple
from pathlib import Path
import os.path
import json
import logging
import zipfile
import tarfile
import requests
import tqdm
def _get_latest_release():
"""Get link of the latest policies snapshot."""
github_api = "https://api.github.com/repos/cliqz-oss/privacy-bot/releases/latest"
response = requests.get(github_api)
release = response.json()
return {
"tag": release["tag_name"],
"name": release["name"],
"url": release["zipball_url"],
"id": release["id"]
}
Policy = namedtuple('Policy', [
'name',
'url',
'html',
'text',
'domain',
'lang',
'tld'
])
class SnapshotBase:
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
@abstractmethod
def open(self, path):
raise NotImplementedError()
@abstractmethod
def close(self):
raise NotImplementedError()
class ZipSnapshot(SnapshotBase):
def __init__(self, path):
self.zipfile = zipfile.ZipFile(path)
# Mapping from filename to zip archive filepath
self.file_index = {}
files = list(self.zipfile.namelist())
prefix = os.path.commonprefix(files)
for filename in self.zipfile.namelist():
self.file_index[filename[len(prefix):]] = filename
def open(self, path):
return self.zipfile.open(self.file_index[path])
def close(self):
self.zipfile.close()
class LocalSnapshot(SnapshotBase):
def __init__(self, path):
self.path = Path(path)
def open(self, path):
return (self.path / path).open()
def close(self):
pass
def load_from_snapshot_abstraction(snapshot):
policies = defaultdict(list)
domains = set()
languages = set()
tlds = set()
seen_policies = set()
# Load index file
with snapshot.open('index.json') as index_file:
index = json.load(index_file)
# Iterate on all available domains
for domain, policy_pages in tqdm.tqdm(index.items(), total=len(index),
dynamic_ncols=True, unit='domain',
desc='Loading policies'):
domains.add(domain)
for policy in policy_pages:
base_path = policy['path']
content = {}
for extension in ['txt', 'html']:
with snapshot.open(base_path + '.' + extension) as content_file:
content[extension] = content_file.read()
tlds.add(policy['tld'])
languages.add(policy['lang'])
policy = Policy(
domain=domain,
url=policy['url'],
name=policy['name'],
html=content['html'],
text=content['txt'],
tld=policy['tld'],
lang=policy['lang']
)
if policy not in seen_policies:
seen_policies.add(policy)
policies[domain].append(policy)
return Policies(
policies=policies,
domains=domains,
tlds=tlds,
languages=languages
)
class Policies:
def __init__(self, policies, tlds, domains, languages):
self.policies = policies
self.domains = domains
self.languages = languages
self.tlds = tlds
@staticmethod
def from_remote():
print('Fetch information about latest release...')
latest_release = _get_latest_release()
print("Get Latest Release")
print("-" * 80)
print("Name:", latest_release["name"])
print("Tag:", latest_release["tag"])
print("Url:", latest_release["url"])
print("-" * 80)
# Check if there is a cached version already
cached_path = Path('/tmp', 'privacy-bot_' + str(latest_release["id"]))
# Fetch and cache content locally
if not cached_path.exists():
# Fetch the content of the archive
response = requests.get(
latest_release["url"],
stream=False,
allow_redirects=True,
)
content = response.content
# Cache fetched content
with cached_path.open('wb') as output:
output.write(content)
with ZipSnapshot(str(cached_path)) as snapshot:
return load_from_snapshot_abstraction(snapshot)
@staticmethod
def from_tar(path):
assert path.endswith('.tar.bz2'), 'from_tar excepts a tar.bz2 archive'
cached_path = Path('/tmp', 'privacy-bot_policies_' + path.split('.', 1)[0])
# Extract archive is necessary
if not cached_path.exists():
with tarfile.open(path, mode='r:bz2') as archive:
print('Extract archive into', cached_path)
archive.extractall(str(cached_path))
# Load policies from extracted archive
return Policies.from_path(cached_path)
@staticmethod
def from_zip(path):
with ZipSnapshot(path) as snapshot:
return load_from_snapshot_abstraction(snapshot)
@staticmethod
def from_path(path):
print("Load privacy policies from", path)
with LocalSnapshot(path) as snapshot:
return load_from_snapshot_abstraction(snapshot)
def __iter__(self):
for domain, policies in self.policies.items():
for policy in policies:
yield policy
def query(self, domain=None, lang=None, tld=None):
for policy in self:
# Filter on domain
if domain and policy.domain != domain:
continue
# Filter on language
if lang and policy.lang != lang:
continue
# Filter on tld
if tld and policy.tld != tld:
continue
yield policy
def example():
logging.basicConfig(level=logging.INFO)
policies = Policies.from_remote()
# Iterate on all policies
for policy in policies:
print(policy)
print('Size', len(list(policies)))
# Check available domains, tlds, languages
print(policies.domains)
print(policies.tlds)
print(policies.languages)
# Query policy by: tld, domain, lang
for policy in policies.query(lang='de'):
print(policy.domain)
if __name__ == "__main__":
example()