forked from drego85/FibraClickScanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibraclick.py
executable file
·161 lines (126 loc) · 5.78 KB
/
fibraclick.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
#!/usr/bin/python2
# This file is part of FibraClick Scanner.
#
# Copyright(c) 2017 Andrea Draghetti
# https://www.andreadraghetti.it
#
# This file may be licensed under the terms of of the
# GNU General Public License Version 3 (the ``GPL'').
#
# Software distributed under the License is distributed
# on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
# express or implied. See the GPL for the specific language
# governing rights and limitations.
#
# You should have received a copy of the GPL along with this
# program. If not, go to http://www.gnu.org/licenses/gpl.html
# or write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import os
import json
import requests
from pushover import init, Client
try:
from configparser import ConfigParser
except ImportError:
from ConfigParser import ConfigParser # ver. < 3.0
header = {"User-Agent": "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)",
"Accept-Language": "it"}
timeoutconnection = 60
def check_clli_and_onus(sede_clli, onu_ids, statusonus, api_token, user_key):
onu_status = []
notfound = True
n = len(statusonus)
for i, onu_id in enumerate(onu_ids):
if i < n:
onu_status.append({"onu_id": onu_id, "status": statusonus[i]})
else:
onu_status.append({"onu_id": onu_id, "status": ""})
url = "https://fibra.click/api/data/copertura/armadi/?sede=%s" % sede_clli
page = requests.get(url, headers=header, timeout=timeoutconnection)
data = json.loads(page.text)
for onu in onu_status:
for each in data["data"]:
if each["onu_id"] == onu['onu_id']:
currentstatus = str(each["attivazione"])
if currentstatus != onu['status']:
print "Variato lo stato dell'ONU: " + onu['onu_id'] + " in: " + currentstatus
# Invio la notifica Push
if api_token:
Client(user_key).send_message(
"Variato lo stato dell'ONU: " + onu['onu_id'] + " in: " + currentstatus,
title="FibraClick Scanner", url="https://fibra.click",
url_title="Tutti i dettagli su FibraClick")
onu['status'] = currentstatus
else:
print "Nessuna variazione dell'ONU: " + onu['onu_id'] + ", " + currentstatus
notfound = False
break
if notfound:
print "ONU: " + onu[
"onu_id"] + " e Sede Clli: " + sede_clli + " non attualmente pianificati o errore nell'inserimento dei dati."
# Aggiorno il file di configurazione con il nuovo valore ottenuto
new_status = ""
for onu in onu_status:
new_status += onu['status'] + "|"
cfgfile = open("config.txt", "w")
config.set("stati", "statusonus", new_status)
config.write(cfgfile)
cfgfile.close()
def check_comune(comune, statuscomune, api_token, user_key):
url = "https://fibra.click/api/data/comuni/?search=%s" % comune
page = requests.get(url, headers=header, timeout=timeoutconnection)
data = json.loads(page.text)
# Identifico il codice del comune indicato
codicecomune = "0" + data["data"][0]["regione"]["codice"] + data["data"][0]["codice"]
url = "https://fibra.click/api/data/copertura/centrali/%s" % codicecomune
page = requests.get(url, headers=header, timeout=timeoutconnection)
data = json.loads(page.text)
# Se il codice comune ha uno stato (pianificato, attivo, ecc) lo analizzo
if "stato" in str(data):
currentstatus = str(data["data"][0]["stato"])
if currentstatus != statuscomune:
print "Variato lo stato del comune: %s in: %s" % (comune, currentstatus)
# Invio la notifica Push
if api_token:
Client(user_key).send_message(
"Variato lo stato del comune: " + comune + " in: " + currentstatus,
title="FibraClick Scanner", url="https://fibra.click",
url_title="Tutti i dettagli su FibraClick")
cfgfile = open("config.txt", "w")
config.set("stati", "statuscomune", currentstatus)
config.write(cfgfile)
cfgfile.close()
else:
print "Nessuna variazione dell comune: " + comune + ", " + currentstatus
else:
print "Il comune %s non risulta attualmente pianificato per l\'attivazione della Fibra (VDSL)." % comune
def main():
# Leggo i parametri di configurazione
if os.path.exists("config.txt") is True:
global config
config = ConfigParser()
config.read("config.txt")
comune = config.get("fibraclick", "comune")
sede_clli = config.get("fibraclick", "sede_clli")
onu_ids = config.get("fibraclick", "onu_ids").split(',')
api_token = config.get("pushover", "api_token")
user_key = config.get("pushover", "user_key")
statusonus = config.get("stati", "statusonus").split('|')
statuscomune = config.get("stati", "statuscomune")
else:
print "Impossibile leggere il file di configurazione."
print "Editare i parametri del file config.sample.txt e rinominarlo in config.txt!"
exit()
# Inizializzo PushOver
if api_token:
init(api_token)
else:
print "Parametri Pushover assenti, impossibile inviare eventuale notifica push."
if sede_clli and onu_ids:
check_clli_and_onus(sede_clli, onu_ids, statusonus, api_token, user_key)
# Se viene indicato il comune e non la sede Clli o ONU effettuo verifiche sullo stato di attivazione del coume
if comune and not (sede_clli and onu_ids):
check_comune(comune, statuscomune, api_token, user_key)
if __name__ == "__main__":
main()