-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathooniapi.py
More file actions
95 lines (77 loc) · 3.29 KB
/
Copy pathooniapi.py
File metadata and controls
95 lines (77 loc) · 3.29 KB
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
"""
TransitMeasure
Copyright (C) 2024 Atheesh Thirumalairajan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
import json
import config
import requests
from tqdm import tqdm
from cli import ANSIEscape
from caching import load_cache, save_cache
from datetime import datetime, timezone
# Fetch measurements with caching, timeline, and vantage point filtering
def fetch_measurements_with_cache(probe_cc, start_date, end_date, probe_asn=None, confirmed=True):
cache = load_cache()
cache_key = f"{probe_cc}_{start_date}_{end_date}_{probe_asn}_{confirmed}"
if cache_key in cache:
print("Using cached results for:", cache_key)
return cache[cache_key]
params = {
'test_name': 'web_connectivity',
'confirmed': confirmed,
'probe_cc': probe_cc,
'since': start_date,
'until': end_date
}
if probe_asn:
params['probe_asn'] = probe_asn # Add ASN filter if provided
print(f"Downloading OONI Measurements for {cache_key}...")
response = requests.get(config.OONI_MEASUREMENTS_URL, params=params)
response.raise_for_status()
results = response.json()['results']
# Save to cache
cache[cache_key] = results
save_cache(cache)
# Highly Likely that Preprocess Dump is Expired
if os.path.exists(config.PREPROCESS_DUMP_FILE):
os.rename(
config.PREPROCESS_DUMP_FILE,
f"{config.PREPROCESS_DUMP_FILE.replace('.json', '')}" +
f"_bkp_{datetime.now(timezone.utc)}.json"
)
return results
# Preprocess data: filter relevant blockpages
def preprocess_data(measurements, usedump=False):
if usedump and os.path.exists(config.PREPROCESS_DUMP_FILE):
print("Using Pre-process Dump File for further analysis...")
with open(config.PREPROCESS_DUMP_FILE, 'r') as file:
return json.load(file)
raw_reports = []
total_measurements = len(measurements)
# Initialize tqdm progress bar
with tqdm(total=total_measurements, desc="Fetching Raw Reports", unit="measurement") as pbar:
for measurement in measurements:
# Fetch Raw Report from the OONI Raw Measurement URL
raw_report_res = requests.get(measurement['measurement_url'])
raw_report_res.raise_for_status()
raw_reports.append(raw_report_res.json())
# Update progress bar
pbar.update(1)
pbar.set_postfix(current=len(raw_reports))
# If Dump Requested, save the results
if usedump:
with open(config.PREPROCESS_DUMP_FILE, "w") as file:
json.dump(raw_reports, file, indent=4)
print(f"Downloaded {len(raw_reports)} Raw Reports for Analysis")
return raw_reports