aboutsummaryrefslogtreecommitdiff
path: root/tor-metrics/relays.py
blob: 425e6717f562116f8c03c10ddc8622cfa1918399 (plain)
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
import os, json, requests, time
import config

abs_path = os.path.dirname(os.path.abspath(__file__))

class Relays:
    def __init__(self):
        self.statuscode = None
        self.url = config.CONFIG['onionoo_url']
        self.ts_file = os.path.join(abs_path, "timestamp")
        self.json = self.fetch()
        self.timestamp = self.timestamp()

    def fetch(self):
        if os.path.isfile(self.ts_file):
            with open(self.ts_file, 'r') as ts_file:
                prev_timestamp = ts_file.read()
            try:
                api_response = requests.get(self.url,
                        headers={"If-Modified-Since": prev_timestamp})
            except requests.exceptions.RequestException:
                return(None)
        else:
            try:
                api_response = requests.get(self.url)
            except requests.exceptions.RequestException:
                return(None)
        
        self.statuscode = api_response.status_code
        if self.statuscode != 200:
            return(None)
        
        json_data = api_response.json()
        sorted_json = self.sort_by_bandwidth(json_data)
        trimmed_json = self.trim_platform(sorted_json)
        return(trimmed_json)

    def trim_platform(self, json):
        for relay in json['relays']:
            relay['platform'] = relay['platform'].split(' on ', 1)[1].split(' ')[0]
        return(json)

    def sort_by_bandwidth(self, json):
        json['relays'].sort(key=lambda x: x['observed_bandwidth'], reverse=True)
        return(json)

    def timestamp(self):
        timestamp = time.time()
        f_timestamp = time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(timestamp))
        if self.json is not None:
            with open(self.ts_file, 'w', encoding='utf8') as ts_file:
                    ts_file.write(f_timestamp)
        return(f_timestamp)