# Path: prometapi/compat.py
# def zlib_encode(data):


from django.db import models
from prometapi.compat import urlopen, unicode_type
import xml.etree.ElementTree as ET
import datetime
import time
import sys
import foojson
import json
def fetch_xmls():
	main_xml = urlopen(URL_CARTO, timeout=10).read()
	json = _parse_main_xml(main_xml)
	
	for k, v in json.iteritems():
		sta_xml_data = urlopen(URL_STATION % k, timeout=10).read()
		sta_dict = _parse_station_xml(sta_xml_data)
		v['station'] = sta_dict
		v['station_valid'] = not int(sta_dict['total']) == 0
	
	now = datetime.datetime.now()
	resp = {
		'markers': json,
		'updated': time.mktime(now.timetuple())
		}
	return now, resp

def convert_citybikes(data):
	try:
		citybikes = json.loads(data)
	except ValueError as e:
		print >> sys.stderr, [data]
		raise
		
	markers = {}
	updateds = []
	
	for d in citybikes:
		st_id = d['number']
		u = datetime.datetime.strptime(d['timestamp'].rstrip('Z'), '%Y-%m-%dT%H:%M:%S.%f')

# Path: prometapi/compat.py
# def zlib_encode(data):


# Path: prometapi/bicikeljproxy/models.py
from django.db import models
from prometapi.compat import urlopen, unicode_type
import xml.etree.ElementTree as ET
import datetime
import time
import sys
import foojson
import json

def fetch_xmls():
	main_xml = urlopen(URL_CARTO, timeout=10).read()
	json = _parse_main_xml(main_xml)
	
	for k, v in json.iteritems():
		sta_xml_data = urlopen(URL_STATION % k, timeout=10).read()
		sta_dict = _parse_station_xml(sta_xml_data)
		v['station'] = sta_dict
		v['station_valid'] = not int(sta_dict['total']) == 0
	
	now = datetime.datetime.now()
	resp = {
		'markers': json,
		'updated': time.mktime(now.timetuple())
		}
	return now, resp

def convert_citybikes(data):
	try:
		citybikes = json.loads(data)
	except ValueError as e:
		print >> sys.stderr, [data]
		raise
		
	markers = {}
	updateds = []
	
	for d in citybikes:
		st_id = d['number']
		u = datetime.datetime.strptime(d['timestamp'].rstrip('Z'), '%Y-%m-%dT%H:%M:%S.%f')

		updated = int(time.mktime(u.timetuple()))