#except BaseException:
        #    print('failed ondata')
        #    time.sleep(2)

    def on_error(self, status_code, data):
        print(status_code)
        disconnect()


stream = MyStreamer(consumer_key, consumer_secret,access_token,access_token_secret)

#Biscaya
#[[[-3.4492757,42.9818774],[-3.4492757,43.4568551],[-2.4128145,43.4568551],[-2.4128145,42.9818774],[-3.4492757,42.9818774]]]
#stream.statuses.filter(locations=[-3.4492757,42.9818774,-2.4128145,43.4568551])
#All spain
box=BoundingBox()
box.lat_min=float(config["GEOLOCATION"]["SPAIN_MIN_LAT"])
box.lat_max=float(config["GEOLOCATION"]["SPAIN_MAX_LAT"])
box.lon_min=float(config["GEOLOCATION"]["SPAIN_MIN_LON"])
box.lon_max=float(config["GEOLOCATION"]["SPAIN_MAX_LON"])
stream.statuses.filter(locations=[box.lon_min, box.lat_min, box.lon_max, box.lat_max])
stream.statuses.filter(replies=all)
#Enable Count in IBM server
#stream.statuses.filter(count=50000)


"""
Rest API
from twython import Twython
twitter = Twython()
__author__ = 'root'

import configparser
config = configparser.ConfigParser()
config.read('../../app.conf')

from mongoengine.connection import connect,disconnect
connection=connect(config["MONGODB"]["DB_NAME"])
#Call to API:
import requests as rq
import json
from hotelapp.models import Hotel,BoundingBox,HotelProperty
import math

box=BoundingBox()
box.lat_min=float(config["GEOLOCATION"]["MIN_LAT"])
box.lat_max=float(config["GEOLOCATION"]["MAX_LAT"])
box.lon_min=float(config["GEOLOCATION"]["MIN_LON"])
box.lon_max=float(config["GEOLOCATION"]["MAX_LON"])
max_radius=box.get_Max_Radius()
#cities = ['bilbao', 'barakaldo', 'getxo', 'portugalete', 'santurtzi', 'basauri', 'leioa', 'sestao', 'galdakao', 'durango', 'erandio', 'amorebieta-etxano', 'bermeo', 'mungia', 'gernika-lumo']
#for city in cities:
r = rq.get('http://dev.api.ean.com/ean-services/rs/hotel/v3/list?apiKey=rdqjh2rkduun4tvh4kkhu6z5&cid=55505&customerIpAddress=89.140.177.130&customerUserAgent=Mozilla/5.0&locale=en_US&currencyCode=USD&countryCode=ESP&latitude='+str((box.lat_max+box.lat_min)/2)+'&longitude='+str((box.lon_max+box.lon_min)/2)+'&searchRadiusUnit=KM&searchRadius='+str(math.ceil(max_radius))) # Replace the id if not found.

#r = rq.get('http://dev.api.ean.com/ean-services/rs/hotel/v3/list?apiKey=rdqjh2rkduun4tvh4kkhu6z5&cid=55505&customerIpAddress=89.140.177.130&customerUserAgent=Mozilla/5.0&locale=en_US&currencyCode=USD&countryCode=ESP&city=amorebieta-etxano') # Replace the id if not found.
print(r.text)
hotels = json.loads(r.text)
hotels = hotels["HotelListResponse"]["HotelList"]["HotelSummary"]
#print(json.dumps(hotels, indent=2))

for hotel in hotels:
        #    print('failed ondata')
        #    time.sleep(2)

    def on_error(self, status_code, data):
        print(status_code)
        disconnect()


stream = MyStreamer(consumer_key, consumer_secret, access_token,
                    access_token_secret)

#Biscaya
#[[[-3.4492757,42.9818774],[-3.4492757,43.4568551],[-2.4128145,43.4568551],[-2.4128145,42.9818774],[-3.4492757,42.9818774]]]
#stream.statuses.filter(locations=[-3.4492757,42.9818774,-2.4128145,43.4568551])
#All spain
box = BoundingBox()
box.lat_min = float(config["GEOLOCATION"]["SPAIN_MIN_LAT"])
box.lat_max = float(config["GEOLOCATION"]["SPAIN_MAX_LAT"])
box.lon_min = float(config["GEOLOCATION"]["SPAIN_MIN_LON"])
box.lon_max = float(config["GEOLOCATION"]["SPAIN_MAX_LON"])
stream.statuses.filter(
    locations=[box.lon_min, box.lat_min, box.lon_max, box.lat_max])
stream.statuses.filter(replies=all)
#Enable Count in IBM server
#stream.statuses.filter(count=50000)
"""
Rest API
from twython import Twython
twitter = Twython()

api_url = 'https://api.twitter.com/1.1/search/tweets.json'
__author__ = 'root'

import configparser
config = configparser.ConfigParser()
config.read('../../app.conf')
from mongoengine.connection import connect,disconnect
connection=connect(config["MONGODB"]["DB_NAME"])
#Call to API:
import requests as rq
import json
from hotelapp.models import Hotel,BoundingBox, Room
import math
box=BoundingBox()
box.lat_min=float(config["GEOLOCATION"]["MIN_LAT"])
box.lat_max=float(config["GEOLOCATION"]["MAX_LAT"])
box.lon_min=float(config["GEOLOCATION"]["MIN_LON"])
box.lon_max=float(config["GEOLOCATION"]["MAX_LON"])
max_radius=box.get_Max_Radius()

#url = []
for i in range(14,30): #Loop through dates of March
    for hotel in Hotel.objects: #Loop through hotels
        #print(str(hotel.hotelID))
        url = 'http://dev.api.ean.com/ean-services/rs/hotel/v3/avail?minorRev=28&apiKey=rdqjh2rkduun4tvh4kkhu6z5&cid=55505&customerIpAddress=89.140.177.130&customerUserAgent=Mozilla/5.0&locale=en_US&currencyCode=USD&hotelId='+str(hotel.hotelID )+'&arrivalDate=3/'+str(i)+'/2015&departureDate=3/'+str(i+1)+'/2015&includeDetails=true&includeRoomImages=false&room1=2'

        r = rq.get(url) # Replace the id if not found.
        room = json.loads(r.text)
        room = room["HotelRoomAvailabilityResponse"]
        #print(json.dumps(room, indent=2))

        h = Hotel()