Repository navigation
Expand file tree
/
Copy pathpyGuifiAPI.py
More file actions
767 lines (622 loc) · 28.7 KB
/
Copy pathpyGuifiAPI.py
File metadata and controls
767 lines (622 loc) · 28.7 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# guifiAPI.py - Guifi.net API handler
# Copyright (C) 2012 Pablo Castellano <[email protected]>
#
# 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 <http://www.gnu.org/licenses/>.
from urllib2 import Request, urlopen, URLError
# Warning: HTTPS requests do not do any verification of the server’s certificate.
# Currently urllib2 does not support fetching of https locations through a proxy. This can be a problem.
# http://www.voidspace.org.uk/python/articles/urllib2.shtml
import urllib
import json
ANSWER_GOOD = 200
ANSWER_BAD = 201
CODE_ERROR_INVALID_TOKEN = 502
CODE_ERROR_INVALID_TOKEN_STR = 'The given Auth token is invalid'
class GuifiApiError(Exception):
def __init__(self, reason, code=0, extra=None):
self.reason = reason
self.code = int(code)
self.extra = extra
def __str__(self):
return self.reason
class GuifiAPI(object):
def __init__(self, username=None, passwd=None, host='test.guifi.net',
secure=True, retry_count=0, retry_delay=0, retry_errors=None,
authToken=None):
self.setHost(host)
self.secure = secure
self.username = username
self.passwd = passwd
self.retry_count = retry_count
self.retry_delay = retry_delay
self.retry_errors = retry_errors
self.base_url = 'https://' if self.secure else 'http://'
self.base_url += self.host
self.base_api_url = self.base_url + '/api?'
self.base_cnml_url = self.base_url + '/%s/guifi/cnml/%d/%s'
self.base_gml_url = self.base_url + '/%s/guifi/gml/%d/%s'
self.base_device_url = self.base_url + '/%s/guifi/device/%d/view'
user_agent = 'pyGuifiAPI/0.7'
self.headers = {'User-Agent': user_agent}
if authToken:
self.headers['Authorization'] = 'GuifiLogin auth=' + authToken
self.authToken = authToken
def getHost(self):
return self.host
def setUsername(self, username):
if username != self.username:
#invalidate auth token
self.authToken = None
self.username = username
def setPassword(self, password):
self.passwd = password
def getAuthToken(self):
return self.authToken
def setAuthToken(self, authToken):
self.authToken = authToken
def setHost(self, host):
"""Checks for a valid host and set it as attribute"""
if host.endswith('/'):
host = host[:-1]
self.host = host
def sendRequest(self, data):
""" Sends API request and returns json result"""
url = self.base_api_url + data
#print '<<<sendRequest>>>', url, len(url)
#print 'Headers:', self.headers
req = Request(url, headers=self.headers)
try:
response = urlopen(req)
#j = json.load(response)
r = response.read()
j = json.loads(r)
print r
except URLError, e:
#caza tambien HTTPError
print e.reason
raise
except ValueError, e:
print e.reason
raise
return self._parseResponse(j)
def _parseResponse(self, jsondata):
""" Parses json response.
Returns a tuple with the code and the result of the request (it can be an errorenous response)
Raises exception if any error code is unknown """
print '<<<parseResponse>>>'
codenum = jsondata.get('code')['code']
codestr = jsondata.get('code')['str']
print 'Got code %d: %s' % (codenum, codestr)
if codenum == ANSWER_GOOD:
responses = jsondata.get('responses')
if isinstance(responses, list):
if len(responses) == 1:
responses = responses[0]
print 'Just one response'
else:
print 'There are several responses'
return (codenum, responses)
elif codenum == ANSWER_BAD:
errors = jsondata.get('errors')
if len(errors) == 1:
errors = errors[0]
print 'Just one response (error)'
print 'Error', errors['code'], ':', errors['str']
if 'extra' in errors:
print 'Extra information:', errors['extra']
else:
print 'There are several responses (errors)'
for e in errors:
print 'Error', e['code'], ':', e['str']
if 'extra' in e:
print 'Extra information:', e['extra']
return (codenum, errors)
else:
raise GuifiApiError('Unexpected return code: ' + codenum)
def auth(self):
""" Authenticate user and get the Authorization Token """
if self.username is None or self.passwd is None:
raise GuifiApiError('You need to set username and password first')
self.authToken = None
if 'Authorization' in self.headers:
del self.headers['Authorization']
# XXX: There's some kind of bug when you submit the Authorization header to this command
# Then, you get 502 error if command is the last parameters specified
# http://test.guifi.net/api?username=user1&password=pass1&command=guifi.auth.login
# {"command":"","code":{"code":201,"str":"Request could not be completed, errors found"},"errors":[{"code":502,"str":"The given Auth token is invalid"}]}
# Note 1: the command is not specified in the response (!!)
# Note 2: it works well in firefox
# A workaround fix is also using the method parameter which is added to the end in the dict structure
#data = urllib.urlencode({'command':'guifi.auth.login', 'username':self.username, 'password':self.passwd})
data = urllib.urlencode({'command': 'guifi.auth.login', 'username': self.username, 'password': self.passwd, 'method': 'password'})
try:
(codenum, response) = self.sendRequest(data)
print 'auth:', codenum
print response
if codenum == ANSWER_GOOD:
self.authToken = response.get('authToken')
self.headers['Authorization'] = 'GuifiLogin auth=' + self.authToken
else:
# Expect just one error
errorcode = response['code']
if errorcode == CODE_ERROR_INVALID_TOKEN: # Nosense (:?)
#{"errors":[{"code":403,"str":"Request is not valid: some input data is incorrect","extra":"Either the supplied username or password are not correct"}]}
raise GuifiApiError('Error during authentication: ' + str(errorcode) + ': ' + response['str'])
else:
raise GuifiApiError('Unexpected return code: ' + str(errorcode))
except URLError: # Not connected to the Internets
raise
def addNode(self, title, zone_id, lat, lon, nick=None, body=None,
zone_desc=None, notification=None, elevation=None,
stable='Yes', graph_server=None, status='Planned'):
if not self.is_authenticated():
raise GuifiApiError('You have to be authenticated to run this action')
data = {'command': 'guifi.node.add', 'title': title, 'zone_id': zone_id, 'lat': lat, 'lon': lon}
if nick is not None:
data['nick'] = nick
if body is not None:
data['body'] = body
if zone_desc is not None:
data['zone_description'] = zone_desc
if notification is not None:
data['notification'] = notification
if elevation is not None:
data['elevation'] = elevation
if stable != 'Yes':
data['stable'] = 'No'
if graph_server is not None:
data['graph_server'] = graph_server
if status is not 'Planned':
data['status'] = status
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
node_id = int(response.get('node_id'))
print 'Node succesfully created', node_id
print self.urlForNode(node_id)
else:
# Everybody can create nodes
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
return node_id
def urlForNode(self, nid):
lang = 'es'
return '%s/%s/node/%d' % (self.base_url, lang, nid)
def urlForZone(self, zid):
lang = 'es'
return '%s/%s/node/%d' % (self.base_url, lang, zid)
def urlForDevice(self, did):
lang = 'es'
return self.base_device_url % (lang, did)
def updateNode(self, nid, title=None, nick=None, body=None, zone_id=None,
zone_description=None, notification=None, lat=None,
lon=None, elevation=None, stable=None, graph_server=None,
status=None):
if not self.is_authenticated():
raise GuifiApiError('You have to be authenticated to run this action')
data = {'command': 'guifi.node.update', 'node_id': nid}
if title is not None:
data['title'] = title
if nick is not None:
data['nick'] = nick
if body is not None:
data['body'] = body
if zone_id is not None:
data['zone_id'] = zone_id
if zone_description is not None:
data['zone_description'] = zone_description
if notification is not None:
data['notification'] = notification
if lat is not None:
data['lat'] = lat
if lon is not None:
data['lon'] = lon
if elevation is not None:
data['elevation'] = elevation
if stable is not None:
data['stable'] = stable
if status is not None:
data['status_flag'] = status
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
node_id = response['node']['node_id']
print 'Node succesfully updated', node_id
print '%s/node/%s' % (self.base_url, node_id)
else:
# [{"code":500,"str":"Request could not be completed. The object was not found","extra":"zone_id = is not a guifi node"}]}
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
return node_id
def removeNode(self, nid):
if not self.is_authenticated():
raise GuifiApiError('You have to be authenticated to run this action')
data = {'command': 'guifi.node.remove', 'node_id': nid}
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
print 'Node %s succesfully removed' % nid
else:
# [{"code":500,"str":"Request could not be completed. The object was not found","extra":"node_id = 49836"}]}
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
def addZone(self, title, master, minx, miny, maxx, maxy, nick=None,
mode='infrastructure', body=None, timezone='+01 2 2',
graph_server=None, proxy_server=None, dns_servers=None,
ntp_servers=None, ospf_zone=None, homepage=None,
notification=None):
if not self.is_authenticated():
raise GuifiApiError('You have to be authenticated to run this action')
data = {'command': 'guifi.zone.add', 'title': title, 'master': master,
'minx': minx, 'miny': miny, 'maxx': maxx, 'maxy': maxy}
if nick is not None:
data['nick'] = nick
if mode != 'infrastructure':
data['mode'] = mode
if body is not None:
data['body'] = body
if timezone != '+01 2 2':
data['timezone'] = timezone
if graph_server is not None:
data['graph_server'] = graph_server
if proxy_server is not None:
data['proxy_server'] = proxy_server
if dns_servers is not None:
data['dns_servers'] = dns_servers
if ntp_servers is not None:
data['ntp_servers'] = ntp_servers
if ospf_zone is not None:
data['ospf_zone'] = ospf_zone
if homepage is not None:
data['homepage'] = homepage
if notification is not None:
data['notification'] = notification
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
zone_id = int(response.get('zone_id'))
print 'Zone succesfully created', zone_id
print self.urlForZone(zone_id)
else:
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
return zone_id
def updateZone(self, zid, title=None, nick=None, mode='infrastructure',
body=None, timezone='+01 2 2', graph_server=None,
proxy_server=None, dns_servers=None, ntp_servers=None,
ospf_zone=None, homepage=None, notification=None,
master=None, minx=None, miny=None, maxx=None, maxy=None):
if not self.is_authenticated():
raise GuifiApiError('You have to be authenticated to run this action')
data = {'command': 'guifi.zone.update', 'zone_id': zid}
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
pass
else:
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
def removeZone(self, zid):
if not self.is_authenticated():
raise GuifiApiError('You have to be authenticated to run this action')
data = {'command': 'guifi.zone.remove', 'zone_id': zid}
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
pass
else:
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
def addDevice(self, nid, rtype, mac, nick=None, notification=None,
comment=None, status=None, graph_server=None,
model_id=None, firmware=None, download=None, upload=None, mrtg_index=None):
if not self.is_authenticated():
raise GuifiApiError('You have to be authenticated to run this action')
data = {'command': 'guifi.device.add', 'node_id': nid, 'type': rtype, 'mac': mac}
if nick is not None:
data['nick'] = nick
if notification is not None:
data['notification'] = notification
if comment is not None:
data['comment'] = comment
if status is not None:
data['status'] = status
if graph_server is not None:
data['graph_server'] = graph_server
if rtype == 'radio':
if model_id is None or firmware is None:
raise ValueError
data['model_id'] = model_id
data['firmware'] = firmware
elif rtype == 'adsl':
if download is None or upload is None or mrtg_index is None:
raise ValueError
data['download'] = download
data['upload'] = upload
data['mrtg_index'] = mrtg_index
elif rtype == 'generic':
if mrtg_index is None:
raise ValueError
data['mrtg_index'] = mrtg_index
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
device_id = int(response.get('device_id'))
print 'Device succesfully created', device_id
print self.urlForDevice(device_id)
else:
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
return device_id
def updateDevice(self, did, nid=None, nick=None, notification=None,
mac=None, comment=None, status=None, graph_server=None,
model_id=None, firmware=None, download=None, upload=None,
mrtg_index=None):
if not self.is_authenticated():
raise GuifiApiError('You have to be authenticated to run this action')
data = {'command': 'guifi.device.update', 'device_id': did}
# TODO: update nid. Is it safe to change it directly in the database?
if nick is not None:
data['nick'] = nick
if notification is not None:
data['notification'] = notification
if mac is not None:
data['mac'] = mac
if comment is not None:
data['comment'] = comment
if status is not None:
data['flag'] = status
if graph_server is not None:
data['graph_server'] = graph_server
if model_id is not None:
data['model_id'] = model_id
if firmware is not None:
data['firmware'] = firmware
if download is not None:
data['download'] = download
if upload is not None:
data['upload'] = upload
if mrtg_index is not None:
data['mrtg_index'] = mrtg_index
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
pass
else:
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
def removeDevice(self, did):
if not self.is_authenticated():
raise GuifiApiError('You have to be authenticated to run this action')
data = {'command': 'guifi.device.remove', 'device_id': did}
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
print 'Device %s succesfully removed' % did
else:
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
def addRadio(self, mode, did, mac, angle=None, gain=None, azimuth=None,
amode=None, ssid=None, protocol=None, channel=None, clients='Yes'):
if not self.is_authenticated():
raise GuifiApiError('You have to be authenticated to run this action')
data = {'command': 'guifi.radio.add', 'mode': mode, 'device_id': did, 'mac': mac}
if angle is not None:
data['antenna_angle'] = angle
if gain is not None:
data['antenna_gain'] = gain
if azimuth is not None:
data['antenna_azimuth'] = azimuth
if amode is not None:
data['antenna_mode'] = amode
if mode == 'ap':
if ssid is None or protocol is None or channel is None or clients is None:
raise ValueError
data['ssid'] = ssid
data['protocol'] = protocol
data['channel'] = channel
data['clients_accepted'] = clients
elif mode == 'ad-hoc':
if ssid is None or protocol is None or channel is None:
raise ValueError
data['ssid'] = ssid
data['protocol'] = protocol
data['channel'] = channel
else:
print mode
raise NotImplementedError
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
# TODO: qué devolver? array tb?
radiodev_counter = int(response.get('radiodev_counter'))
interfaces = response.get('interfaces')
print 'Radio succesfully created', radiodev_counter
print self.urlForDevice(int(did))
else:
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
return (radiodev_counter, interfaces)
def updateRadio(self, did, radiodev, angle=None, gain=None,
azimuth=None, amode=None):
if not self.is_authenticated():
raise GuifiApiError('You have to be authenticated to run this action')
data = {'command': 'guifi.radio.update', 'device_id': did, 'radiodev_counter': radiodev}
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
pass
else:
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
def removeRadio(self, did, radiodev):
if not self.is_authenticated():
raise GuifiApiError('You have to be authenticated to run this action')
data = {'command': 'guifi.radio.remove', 'device_id': did, 'radiodev_counter': radiodev}
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
print 'Radio %s from device %s succesfully removed' % (radiodev, did)
else:
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
def addInterface(self, did, radiodev):
if not self.is_authenticated():
raise GuifiApiError('You have to be authenticated to run this action')
data = {'command': 'guifi.interface.add', 'device_id': did, 'radiodev_counter': radiodev}
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
# "responses":{"ipv4":[{"ipv4_type":"1","ipv4":"10.64.3.33","netmask":"255.255.255.224"}],"interface_id":76140}
if codenum == ANSWER_GOOD:
iid = int(response.get('interface_id'))
ipv4 = response.get('ipv4')
print 'Interface succesfully created', iid
print self.urlForDevice(int(did))
else:
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
return (iid, ipv4)
def removeInterface(self, iid):
if not self.is_authenticated():
raise GuifiApiError('You have to be authenticated to run this action')
data = {'command': 'guifi.interface.remove', 'interface_id': iid}
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
print 'Interface %s succesfully removed' % iid
else:
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
def addLink(self, fromdid, fromradiodev, todid, toradiodev,
ipv4=None, status='Working'):
if not self.is_authenticated():
raise GuifiApiError('You have to be authenticated to run this action')
data = {'command': 'guifi.link.add', 'from_device_id': fromdid,
'from_radiodev_counter': fromradiodev, 'to_device_id': todid,
'to_radiodev_counter': toradiodev}
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
lid = int(response.get('link_id'))
ipv4 = response.get('ipv4')
print 'Link succesfully created', lid
print self.urlForDevice(int(fromdid))
else:
# FIXME: Crashes when there are several errors and extra is a list (no has_key())
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
return (lid, ipv4)
def updateLink(self, lid, ipv4=None, status=None, routing=None):
if not self.is_authenticated():
raise GuifiApiError('You have to be authenticated to run this action')
data = {'command': 'guifi.link.update', 'link_id': lid}
if ipv4 is not None:
data['ipv4'] = ipv4
if status is not None:
data['status'] = status
if routing is not None:
data['routing'] = routing
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
pass
else:
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
def removeLink(self, lid):
if not self.is_authenticated():
raise GuifiApiError('You have to be authenticated to run this action')
data = {'command': 'guifi.link.remove', 'link_id': lid}
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
print 'Link %s succesfully removed' % lid
else:
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
def getModels(self, type=None, fid=None, supported=None):
data = {'command': 'guifi.misc.model'}
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
return response['models']
else:
raise GuifiApiError(response['str'], response['code'], response['extra'])
def getManufacturers(self):
data = {'command': 'guifi.misc.manufacturer'}
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
return response['manufacturers']
else:
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
def getFirmwares(self, model_id=None):
data = {'command': 'guifi.misc.firmware'}
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
return response['firmwares']
else:
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
def getProtocols(self):
data = {'command': 'guifi.misc.protocol'}
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
return response['protocols']
else:
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
def getChannels(self, protocol):
data = {'command': 'guifi.misc.channel', 'protocol': protocol}
params = urllib.urlencode(data)
(codenum, response) = self.sendRequest(params)
if codenum == ANSWER_GOOD:
return response['channels']
else:
extra = response['extra'] if 'extra' in response else None
raise GuifiApiError(response['str'], response['code'], extra)
def is_authenticated(self):
return self.authToken is not None
#http://test.guifi.net/es/guifi/cnml/3671/zones
#returns file descriptor to be read
# It may take some seconds depending on the server load...
def downloadCNML(self, zid, ctype='nodes'):
if ctype not in ['zones', 'nodes', 'detail']:
raise ValueError
lang = 'es'
url = self.base_cnml_url % (lang, zid, ctype)
print 'Downloading CNML:', url
req = Request(url, headers=self.headers)
response = urlopen(req)
return response
# http://guifi.net/es/guifi/gml/21629/links
def downloadGML(self, zid, ctype='nodes'):
if ctype not in ['nodes', 'links']:
raise ValueError
lang = 'es'
url = self.base_gml_url % (lang, zid, ctype)
print 'Downloading GML:', url
req = Request(url, headers=self.headers)
response = urlopen(req)
return response
"""
guifi.zone.nearest
guifi.radio.nearest
"""