Skip to content

Instantly share code, notes, and snippets.

@base-jump-org
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save base-jump-org/91ded10b85d9507339be to your computer and use it in GitHub Desktop.
Save base-jump-org/91ded10b85d9507339be to your computer and use it in GitHub Desktop.
Create JSON from FFVL http://www.balisemeteo.com datas
#! /usr/bin/env python
#
# Parse balisemeteo.com
# create JSONs
#
# jm@base-jump.org
# 2015
####################
import codecs, sys, os
import time
import xml.dom
import xml.dom.minidom
import urllib
#######################
## Init things
#######################
# get balise num from command line
# or use default
try:
numBalise = sys.argv[1]
except:
numBalise = 100 # default balise number. 100 = Col du Joly
# URL wap page
balise_url = "http://www.balisemeteo.com/wap/bd_balise.php?idBalise=" + str(numBalise)
## JSON files
balise_file = "[Full path to JSON dir]/balise_" + str(numBalise) + ".json"
tmp_ext = ".tmp"
## return first value
## (clean up values)
def val( str):
temp = str.split(" ")
return temp[0]
# get HTML data
try:
xmldoc = xml.dom.minidom.parse(urllib.urlopen(balise_url))
do_balise = True
except:
print "Error loading HTML file " + balise_url
do_balise = False
# If HTML loaded
if do_balise:
#######################
## LET'S GO !
#######################
print " Create " + balise_file
paragraphs = xmldoc.getElementsByTagName('p')
keys = {}
## open UTF8 temp file
with codecs.open(balise_file + tmp_ext, mode="w", encoding="utf-8") as file:
# start json
file.write(u"{\"BALISE\":\"" + str(numBalise) + u"\",\n")
first = True
# parse each <p> in wap page
for paragraph in paragraphs:
# parse text in current paragraph
for text in paragraph.childNodes:
line = str(text.nodeValue).strip()
# if line not empty and <> 'None'
# start parsing key / values
if line != "None" and len(line)>0:
## first line is date and time
## no key
if first:
col = line.split("-")
key = col[0].strip()
file.write(u"\"DATE\":\"" + key + u"\",\n")
file.write(u"\"TIME\":\"" + col[1].strip() + u"\",\n")
first = False
# parse key / value
# or key [value1, value2]
else:
col = line.split(":")
# check if key does'nt already exist in array of keys
# if already exist add index, for example:
# vitesse = average wind speed
# vitesse1 = max wind speed
# vitesse2 = min wind speed
key = col[0].strip()
try:
i = keys[key]
keys[key] += 1
key = key + str(keys[key])
except:
keys[key] = 0
file.write(u"\"" + key + u"\":")
try :
# chek if one value or two separate by an hyphen
args = col[1].find("-")
if args > 0:
sub = col[1].split("-")
# check if hyphen is not a minus ! (temperature)
if sub[0].strip():
file.write(u"[\"" + val(sub[0].strip()) + u"\",\"" + val(sub[1].strip()) + u"\"],\n")
else:
file.write(u"\"" + val(col[1].strip()) + u"\",\n")
else:
file.write(u"\"" + val(col[1].strip()) + u"\",\n")
except IndexError:
# do nothing ...
t=0
# end JSON, write date of JSON file creation
file.write(u"\"DATEJSON\":\"" + time.strftime("%Y-%m-%d %H:%M") + u"\"}")
## rename temp file
os.rename(balise_file + tmp_ext, balise_file)
{"BALISE":"100",
"DATE":"20/03/2015",
"TIME":"16:17",
"Direction":["NO","315"],
"Vitesse":"0",
"Direction1":["NNO","337"],
"Vitesse1":"7",
"Vitesse2":"0",
"Temp":"10",
"DATEJSON":"2015-03-20 16:30"}
@base-jump-org
Copy link
Author

Bug fix :
check if the hyphen is not a minus (temperature).
If first arg after splitting is empty -> the hyphen is in fact a minus (temperature)
if first arg not empty -> make an array with 2 values (wind speed, direction, ...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment