Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# -*- coding: utf-8 -*-
import os
import json
import urllib2
from datetime import datetime, timedelta
from sh import git, rm, cd
DATE_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'
API_HOST = 'http://api.openwifimap.net/view_nodes_spatial'
API_BBOX = '13.179473876953125,52.45308034523523,13.647079467773438,52.59241215943279'
GIT_NAME = 'berlin.freifunk.net'
GIT_URL = 'git@github.com:freifunk-berlin/berlin.freifunk.net.git'
GIT_AUTHOR = 'Freifunk Berlin <berlin@berlin.freifunk.net>'
FF_PATH = 'www/static/berlin.json'
THRESHOLD = 10
now = datetime.now()
def valid_date(data):
if 'mtime' in data:
date = datetime.strptime(data['mtime'], DATE_FORMAT)
return (now - date) <= timedelta(days=7)
return False
print("Loading nodes...\t"),
req = urllib2.urlopen(API_HOST + '?bbox=' + API_BBOX)
data = json.loads(req.read())
nodes = len([n for n in data['rows'] if valid_date(n['value'])])
print("%d nodes in our database." % nodes)
print("\nClone git repository: %s" % GIT_URL)
rm('-rf', GIT_NAME)
git.clone(GIT_URL, GIT_NAME)
cd(GIT_NAME)
with open(FF_PATH, 'r') as f:
ff_api_data = json.load(f)
# do we have a new value for nodes?
api_nodes = int(ff_api_data['state']['nodes'])
if abs(nodes - api_nodes) > THRESHOLD:
print("JSON API file is outdated (%d != %d)" % (api_nodes, nodes))
ff_api_data['state']['nodes'] = nodes
ff_api_data['state']['lastchange'] = now.strftime(DATE_FORMAT)
# save new json api file
with open(FF_PATH, 'w') as f_new:
json.dump(ff_api_data, f_new, sort_keys=True, indent=4,
separators=(',',':'))
# commit it
msg = 'berlin.json - node count updated to %d' % nodes
git.add(FF_PATH)
git.commit('-m', msg, '--author="%s"' % GIT_AUTHOR)
print('Committed: %s' % msg)
git.push()
print('Pushed changes!')
cd('..')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment