-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
110 lines (88 loc) · 3.59 KB
/
scraper.py
File metadata and controls
110 lines (88 loc) · 3.59 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python
import fritzconnection as fc
import fritzscrapercargo as fsc
import time
FRITZ_IP_ADDRESS = '192.168.0.1'
FRITZ_TCP_PORT = 49000
class Scraper(object):
connection = None
last_bytes_sent = 0
last_bytes_received = 0
last_traffic_call = 0.0
fscargo = {}
def __init__(self, address=FRITZ_IP_ADDRESS, port=FRITZ_TCP_PORT):
connection = fc.FritzConnection(address=address, port=port)
print("Connected to FritzBox")
self.connection = connection
self.last_bytes_sent = self.bytes_sent
self.last_bytes_received = self.bytes_received
self.last_traffic_call = time.time()
@property
def modelname(self):
return self.connection.modelname
@property
def is_linked(self):
status = self.connection.call_action('WANCommonInterfaceConfig', 'GetCommonLinkProperties')
return status['NewPhysicalLinkStatus'] == 'Up'
@property
def is_connected(self):
status = self.connection.call_action('WANIPConnection', 'GetStatusInfo')
return status['NewConnectionStatus'] == 'Connected'
@property
def wan_access_type(self):
return self.connection.call_action('WANCommonInterfaceConfig', 'GetCommonLinkProperties')['NewWANAccessType']
@property
def external_ip(self):
return self.connection.call_action('WANIPConnection', 'GetExternalIPAddress')['NewExternalIPAddress']
@property
def uptime(self):
return self.connection.call_action('WANIPConnection', 'GetStatusInfo')['NewUptime']
@property
def bytes_received(self):
return self.connection.call_action('WANCommonInterfaceConfig', 'GetTotalBytesReceived')['NewTotalBytesReceived']
@property
def bytes_sent(self):
return self.connection.call_action('WANCommonInterfaceConfig', 'GetTotalBytesSent')['NewTotalBytesSent']
@property
def transmission_rate(self):
# Convert to bits
sent = self.bytes_sent * 8
received = self.bytes_received * 8
traffic_call = time.time()
time_delta = traffic_call - self.last_traffic_call
upstream = int(1.0 * (sent - self.last_bytes_sent)/time_delta)
downstream = int(1.0 * (received - self.last_bytes_received)/time_delta)
self.last_bytes_sent = sent
self.last_bytes_received = received
self.last_traffic_call = traffic_call
return upstream, downstream
@property
def max_bitrate(self):
status = self.connection.call_action('WANCommonInterfaceConfig', 'GetCommonLinkProperties')
downstream = status['NewLayer1DownstreamMaxBitRate']
upstream = status['NewLayer1UpstreamMaxBitRate']
return upstream, downstream
def get_cargo(self):
self.update_data()
return self.fscargo
def update_data(self):
cargo = {}
#cargo["model_name"] = self.modelname
cargo["is_linked"] = int(self.is_linked)
cargo["is_connected"] = int(self.is_connected)
#cargo["wan_access_type"] = self.wan_access_type
#cargo["external_ip"] = self.external_ip
cargo["uptime"] = self.uptime
cargo["bytes_received"] = self.bytes_received
cargo["bytes_sent"] = self.bytes_sent
transmission_rate_upstream, transmission_rate_downstream = self.transmission_rate
cargo["transmission_rate_upstream"] = transmission_rate_upstream
cargo["transmission_rate_downstream"] = transmission_rate_downstream
max_bitrate_upstream, max_bitrate_downstream = self.max_bitrate
cargo["max_bitrate_upstream"] = max_bitrate_upstream
cargo["max_bitrate_downstream"] = max_bitrate_downstream
self.fscargo = fsc.FritzScraperCargo(cargo)
def print_status(self):
print("time: " + str(self.fscargo.timestamp))
for name, value in self.fscargo.cargo.items():
print(name + ": " + str(value))