Voici un Module pour l'API OpenWeatherMap
http://openweathermap.org/api
EDIT: Mis à jour le 04/09/2015
Code : Tout sélectionner
;=======================================================
;== Module OpenWeatherMap
;=======================================================
;== Compiler: PureBasic 5.31 (x64)
;== Tested on: Linux XUbuntu
;== Author: flype, 2015
;== Version: 1.3 (2015-09-04)
;== Source: GitHub link (soon)
;=======================================================
; http://openweathermap.org/api
; http://openweathermap.org/api_station
; http://openweathermap.org/current
; http://openweathermap.org/forecast5
; http://openweathermap.org/forecast16
; http://openweathermap.org/weather-data
; http://openweathermap.org/weather-conditions
; http://openweathermap.org/img/w/{weather.icon}.png
;=======================================================
;== Module Http
;=======================================================
DeclareModule Http
;----------------------------------------
;-- Functions
;----------------------------------------
Declare GetData (url.s, bufSize.i = 65536)
Declare GetImage (url.s, bufSize.i = 65536)
Declare.s GetString(url.s, bufSize.i = 65536)
EndDeclareModule
Module Http
EnableExplicit
;----------------------------------------
;-- Publics
;----------------------------------------
Procedure GetData(url.s, bufSize = 65536)
Protected connection, exit, count, total, *buf1, *buf2
url = URLEncoder(url)
connection = OpenNetworkConnection(GetURLPart(url, #PB_URL_Site), 80, #PB_Network_TCP)
If connection
If SendNetworkString(connection, "GET " + url + #CRLF$, #PB_UTF8)
Repeat
Select NetworkClientEvent(connection)
Case #PB_NetworkEvent_None: Delay(10)
Case #PB_NetworkEvent_Disconnect: exit = #True
Case #PB_NetworkEvent_Data
*buf1 = AllocateMemory(bufSize)
If *buf1 <> #Null
*buf2 = *buf1
Repeat
count = ReceiveNetworkData(connection, *buf2, 1024)
If count > 0
*buf2 + count : total + count
EndIf
Until count = 0
*buf1 = ReAllocateMemory(*buf1, total)
EndIf
exit = #True
EndSelect
Until exit
EndIf
CloseNetworkConnection(connection)
EndIf
ProcedureReturn *buf1
EndProcedure
Procedure GetImage(url.s, bufSize = 65536)
Protected result, *buf = Http::GetData(url, bufSize)
If *buf
result = CatchImage(#PB_Any, *buf, MemorySize(*buf))
FreeMemory(*buf)
EndIf
ProcedureReturn result
EndProcedure
Procedure.s GetString(url.s, bufSize = 65536)
Protected result.s, *buf = Http::GetData(url, bufSize)
If *buf
result = PeekS(*buf, -1, #PB_UTF8)
FreeMemory(*buf)
EndIf
ProcedureReturn result
EndProcedure
EndModule
;=======================================================
;== Module OpenWeatherMap
;=======================================================
DeclareModule OpenWeatherMap
;----------------------------------------
;-- Constants
;----------------------------------------
#OWM_API_ICON = "http://openweathermap.org/img/w/"
#OWM_API_ROOT = "http://api.openweathermap.org/data/2.5/"
#OWM_API_FIND = "http://api.openweathermap.org/data/2.5/find"
#OWM_API_GROUP = "http://api.openweathermap.org/data/2.5/group"
#OWM_API_WEATHER = "http://api.openweathermap.org/data/2.5/weather"
#OWM_API_BOX = "http://api.openweathermap.org/data/2.5/box/city"
#OWM_MODE_HTML = "html"
#OWM_MODE_JSON = "json"
#OWM_MODE_XML = "xml"
#OWM_TYPE_ACCURATE = "accurate"
#OWM_TYPE_LIKE = "like"
#OWM_UNIT_Imperial = "imperial"
#OWM_UNIT_Metric = "metric"
#OWM_LANG_Bulgarian = "bg"
#OWM_LANG_Catalan = "ca"
#OWM_LANG_Chinese_Traditional = "zh_tw"
#OWM_LANG_Chinese_Simplified = "zh" ; or "zh_cn"
#OWM_LANG_Croatian = "hr"
#OWM_LANG_Dutch = "nl"
#OWM_LANG_English = "en"
#OWM_LANG_Finnish = "fi"
#OWM_LANG_French = "fr"
#OWM_LANG_German = "de"
#OWM_LANG_Italian = "it"
#OWM_LANG_Polish = "pl"
#OWM_LANG_Portuguese = "pt"
#OWM_LANG_Romanian = "ro"
#OWM_LANG_Russian = "ru"
#OWM_LANG_Spanish = "es" ; or "sp"
#OWM_LANG_Swedish = "sv" ; or "se"
#OWM_LANG_Turkish = "tr"
#OWM_LANG_Ukrainian = "uk" ; or "ua"
;----------------------------------------
;-- Structures
;----------------------------------------
Structure OWM_Clouds
all.l ; Cloudiness, %
EndStructure
Structure OWM_Coord
lon.d ; City geo location, latitude
lat.d ; City geo location, longitude
EndStructure
Structure OWM_Error
cod.s ; Error code
message.s ; Error message
EndStructure
Structure OWM_Main
temp.d ; Temperature. Unit Default: Kelvin, Metric: Celsius, Imperial: Fahrenheit.
pressure.l ; Atmospheric pressure (on the sea level, if there is no sea_level or grnd_level data), hPa
humidity.l ; Humidity, %
temp_min.d ; Minimum temperature at the moment.
; This is deviation from current temp that is possible for large cities
; and megalopolises geographically expanded (use these parameter optionally).
; Unit Default: Kelvin, Metric: Celsius, Imperial: Fahrenheit.
temp_max.d ; Maximum temperature at the moment.
; This is deviation from current temp that is possible for large cities
; and megalopolises geographically expanded (use these parameter optionally).
; Unit Default: Kelvin, Metric: Celsius, Imperial: Fahrenheit.
sea_level.l ; Atmospheric pressure on the sea level, hPa
grnd_level.l ; Atmospheric pressure on the ground level, hPa
EndStructure
Structure OWM_Rain
h3.l ; Rain volume for the last 3 hours
; Remark: original property is '3h'
EndStructure
Structure OWM_Snow
h3.l ; Snow volume for the last 3 hours
; Remark: original property is '3h'
EndStructure
Structure OWM_Sys
type.l ; Internal parameter
id.l ; Internal parameter
message.d ; Internal parameter
country.s ; Country code (GB, JP etc.)
sunrise.l ; Sunrise time, unix, UTC
sunset.l ; Sunset time, unix, UTC
EndStructure
Structure OWM_Weather
id.l ; Weather condition id
main.s ; Group of weather parameters (Rain, Snow, Extreme etc.)
description.s ; Weather condition within the group
icon.s ; Weather icon id
EndStructure
Structure OWM_Wind
speed.d ; Wind speed. Unit Default: meter/sec, Metric: meter/sec, Imperial: miles/hour.
deg.l ; Wind direction, degrees (meteorological)
EndStructure
Structure OWM_Item
cod.l ; Internal parameter
base.s ; Internal parameter
id.l ; City ID
name.s ; City name
visibility.l ; Visibility
dt.l ; Time of data calculation, unix, UTC
coord.OWM_Coord ; Coord object
clouds.OWM_Clouds ; Clouds object
error.OWM_Error ; Error object
main.OWM_Main ; Main object
rain.OWM_Rain ; Rain object
sys.OWM_Sys ; Sys object
snow.OWM_Snow ; Snow object
wind.OWM_Wind ; Wind object
List weather.OWM_Weather() ; Weather objects
EndStructure
Structure OWM_Group
message.s ; Message
cod.s ; Code
calctime.d ; Processing server time
count.l ; Number of items
List items.OWM_Item() ; Items list
error.OWM_Error ; Error object
EndStructure
;----------------------------------------
;-- Functions
;----------------------------------------
Declare OWM_GetCitiesInIDs(*object.OWM_Group, cityIDs.s)
Declare OWM_GetCitiesInCircle(*object.OWM_Group, lat.d, lon.d, count.i = 10)
Declare OWM_GetCitiesInRect(*object.OWM_Group, lat1.d, lon1.d, lat2.d, lon2.d, mapZoom.d)
Declare OWM_GetWeatherByGeoCoord(*object.OWM_Item, lat.d, lon.d)
Declare OWM_GetWeatherByCityID(*object.OWM_Item, cityID.s)
Declare OWM_GetWeatherByCityName(*object.OWM_Item, cityName.s)
Declare OWM_GetWeatherByZipCode(*object.OWM_Item, zip.s)
Declare OWM_GetWeatherIcon(*object.OWM_Weather)
Declare OWM_SetDefaultAPIKey(key.s)
Declare OWM_SetDefaultLang(lang.s)
Declare OWM_SetDefaultUnits(units.s)
EndDeclareModule
Module OpenWeatherMap
EnableExplicit
;-----------------------------------------------------
;-- Privates
;-----------------------------------------------------
Enumeration
#OWM_FIND
#OWM_ITEM
#OWM_GROUP
EndEnumeration
Global defaultAPIKey.s
Global defaultLang.s
Global defaultUnits.s
Procedure.s PrepareJSON(json.s)
; Rename "3h" member of OWM_Rain and OWM_Snow objects to "h3"
json = ReplaceString(json, #DQUOTE$ + "3h" + #DQUOTE$, #DQUOTE$ + "h3" + #DQUOTE$)
; Rename "list" member of OWM_Group object to "items"
json = ReplaceString(json, #DQUOTE$ + "list" + #DQUOTE$, #DQUOTE$ + "items" + #DQUOTE$)
; Rename "cnt" member of OWM_Group object to "count" (for standardized use)
json = ReplaceString(json, #DQUOTE$ + "cnt" + #DQUOTE$, #DQUOTE$ + "count" + #DQUOTE$)
ProcedureReturn json
EndProcedure
Procedure GetJSON(*object, service.s, params.s)
Protected id, success, json.s
params + "&mode=" + #OWM_MODE_JSON
If defaultUnits <> #NULL$
params + "&units=" + defaultUnits
EndIf
If defaultLang <> #NULL$
params + "&lang=" + defaultLang
EndIf
If defaultAPIKey <> #NULL$
params + "&APPID=" + defaultAPIKey
EndIf
json = Http::GetString(#OWM_API_ROOT + service + params)
;Debug "OpenWeatherMap::GetJSON()"
;Debug #OWM_API_ROOT + service + params
;Debug json
If json <> #NULL$
id = ParseJSON(#PB_Any, PrepareJSON(json))
If id
Select service
Case "weather"
Protected *item.OWM_Item = *object
ExtractJSONStructure(JSONValue(id), *item, OWM_Item)
ExtractJSONStructure(JSONValue(id), *item\error, OWM_Error)
success = #True
Case "box/city", "find", "group"
Protected *group.OWM_Group = *object
ExtractJSONStructure(JSONValue(id), *group, OWM_Group)
ExtractJSONStructure(JSONValue(id), *group\error, OWM_Error)
success = #True
EndSelect
FreeJSON(id)
EndIf
EndIf
ProcedureReturn success
EndProcedure
;-----------------------------------------------------
;-- Publics Getters
;-----------------------------------------------------
Procedure OWM_GetCitiesInIDs(*object.OWM_Group, cityIDs.s)
ProcedureReturn GetJSON(*object, "group", "?id=" + cityIDs)
EndProcedure
Procedure OWM_GetCitiesInCircle(*object.OWM_Group, lat.d, lon.d, count.i = 10)
ProcedureReturn GetJSON(*object, "find", "?lat=" + lat + "&lon=" + lon + "&cnt=" + count)
EndProcedure
Procedure OWM_GetCitiesInRect(*object.OWM_Group, lat1.d, lon1.d, lat2.d, lon2.d, mapZoom.d)
ProcedureReturn GetJSON(*object, "box/city", "?bbox=" + lat1.d + "," + lon1.d + "," + lat2.d + "," + lon2 + "," + mapZoom)
EndProcedure
Procedure OWM_GetWeatherByGeoCoord(*object.OWM_Item, lat.d, lon.d)
ProcedureReturn GetJSON(*object, "weather", "?lat=" + lat + "&lon=" + lon)
EndProcedure
Procedure OWM_GetWeatherByCityID(*object.OWM_Item, cityID.s)
ProcedureReturn GetJSON(*object, "weather", "?id=" + cityID)
EndProcedure
Procedure OWM_GetWeatherByCityName(*object.OWM_Item, cityName.s)
ProcedureReturn GetJSON(*object, "weather", "?q=" + cityName)
EndProcedure
Procedure OWM_GetWeatherByZipCode(*object.OWM_Item, zipCode.s)
ProcedureReturn GetJSON(*object, "weather", "?zip=" + zipCode)
EndProcedure
Procedure OWM_GetWeatherIcon(*object.OWM_Weather)
ProcedureReturn Http::GetImage(#OWM_API_ICON + *object\icon + ".png")
EndProcedure
;-----------------------------------------------------
;-- Publics Setters
;-----------------------------------------------------
Procedure OWM_SetDefaultAPIKey(key.s)
defaultAPIKey = key
EndProcedure
Procedure OWM_SetDefaultLang(lang.s)
defaultLang = lang
EndProcedure
Procedure OWM_SetDefaultUnits(units.s)
defaultUnits = units
EndProcedure
EndModule
;=======================================================
;== End of include
;=======================================================