-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstlist.py
More file actions
executable file
·65 lines (54 loc) · 1.84 KB
/
stlist.py
File metadata and controls
executable file
·65 lines (54 loc) · 1.84 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
#!/usr/bin/env python
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
# Requires the following modules
# apt-get install python-requests python-bs4
# pip install requests beautifulsoup4 netaddr
from __future__ import print_function
import sys
import requests
import os
import time
from bs4 import BeautifulSoup
from optparse import OptionParser
# http://stackoverflow.com/a/14981125
def warning(*objs):
print("WARNING: ", *objs, file=sys.stderr)
# http://stackoverflow.com/a/16695277
def DownloadFile(url, local_filename):
warning("Downloading %s to %s" % (url, local_filename))
r = requests.get(url)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
return
def is_st(tag):
if tag.name == "device" and len(tag.find_all("radio")) > 1:
return True
else:
return False
def main():
usage = "usage: %prog zone"
parser = OptionParser(usage)
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("incorrect number of arguments")
zone = args[0]
cnml_file = "/tmp/%s.cnml" % zone
cnml_url = "http://guifi.net/ca/guifi/cnml/%s/detail" % zone
try:
age = time.time() - os.path.getmtime(cnml_file)
if age > 3600*4:
warning("File age %s" % age)
warning("Too old, redownload")
DownloadFile(cnml_url, cnml_file)
except OSError:
warning("%s does not exist" % cnml_file)
DownloadFile(cnml_url, cnml_file)
with open(cnml_file, 'r') as f:
cnml = BeautifulSoup(f.read())
for st in cnml.find_all(is_st):
print("%6s %30s http://guifi.net/ca/guifi/device/%s"
% (st["id"], st["title"], st["id"]))
if __name__ == "__main__":
main()