Knihovny pro snadné použití API
Pro snadnou implementaci dat s počasím jsme pro Vás připravili několik pomocných knihoven v různých jazycích, které vám výrazně usnadní práci. Uvítáme případné náměty, doplnění či upozornění na chyby, nicméně k samotné implementaci neposkytujeme podporu.
Dostupné knihovny:Python API knihovna
K základní funkčnost potřebuje tato knihovna pouze balíčky requests
and pytz
. Knihovnu lze instalovat pomocí nástroje pip3
.
Python knihovna na GitHubu pro použití API počasí
Příklad použití:
from datetime import datetime, timedelta
from pymeteosource.api import Meteosource
from pymeteosource.types import tiers, sections, langs, units
# Change this to your actual API key
YOUR_API_KEY = 'abcdefghijklmnopqrstuvwxyz0123456789ABCD'
# Change this to your actual tier
YOUR_TIER = tiers.FLEXI
# Initialize the main Meteosource object
meteosource = Meteosource(YOUR_API_KEY, YOUR_TIER)
# Get the forecast for given point
forecast = meteosource.get_point_forecast(
lat=37.7775, # Latitude of the point
lon=-122.416389, # Longitude of the point
place_id=None, # You can specify place_id instead of lat+lon
sections=[sections.CURRENT, sections.HOURLY], # Defaults to '("current", "hourly")'
tz='US/Pacific', # Defaults to 'UTC', regardless of the point location
lang=langs.ENGLISH, # Defaults to 'en'
units=units.US # Defaults to 'auto'
)
# Forecast for lat: 37.7775, lon: -122.416389
print(forecast)
JavacScript knihovna
Pro použití JavaScript knihovny nainstalujte meteosource_js
příkazem npm install meteosource
, případně ke svému kódu připojte metesource.js
a datetime luxon
knihovny.
JavaScript počasí API knihovna na GitHubu
Příklad použití:
<script src="https://www.meteosource.com/js/libs/meteosource.js">
<script src="https://cdn.jsdelivr.net/npm/luxon@2.4.0/build/global/luxon.min.js">
// Change this to your actual API key
let apiKey = 'YOU_API_KEY'
// Change this to your actual tier
let tier = 'flexi'
let m = new meteosource.Meteosource(apiKey, tier)
let forecast = await m.getPointForecast({
lat: 37.7775, // Latitude of the point
lon: -122.416389, // Longitude of the point
placeId: null, // You can specify place_id instead of lat+lon
sections: ["current", "hourly"], // is converted to "current,hourly"
tz: 'US/Pacific',
lang: 'en',
units: 'us', // Defaults to 'auto'
})
// NOTE: The command above works in an async/wait context only
// (async function, Node.JS REPL)
// elsewhere you run:
//m.getPointForecast({...}).then(forecast => console.log(forecast))
PHP knihovna
Nainstalujte meteosource_php
knihovnu příkazem composer require meteosource/meteosource_php
.
PHP knihovna na GitHubu pro použití API počasí
Příklad použití:
<?php
// Change this to your actual API key
const YOUR_API_KEY = 'abcdefghijklmnopqrstuvwxyz0123456789ABCD';
// Change this to your actual tier
const YOUR_TIER = 'flexi';
// Initialize the main Meteosource object
$meteosource = new Meteosource\Meteosource(YOUR_API_KEY, YOUR_TIER);
// Get the forecast for given point
$forecast = $meteosource->getPointForecast(
null, // You can specify place_id instead of lat+lon
37.7775, // Latitude of the point
-122.416389, // Longitude of the point
['current', 'hourly'], // Defaults to '("current", "hourly")'
'US/Pacific', // Defaults to 'UTC', regardless of the point location
'en', // Defaults to 'en'
'auto' // Defaults to 'auto'
);
echo $forecast; // <?Forecast for lat: 37.7775, lon: -122.416389>
C++ knihovna
Pro využití meteosource_cpp
je třeba zkopírovat všechny *.cpp
and *.h
soubory z adresáře src
do vašeho projektu. Poté připojte knihovnu užitím #include "Meteosource.h"
a získejte vámi potřebné informace o aktuálním i historickém počasí.
C++ knihovna na GitHubu pro použití API počasí
Příklad použití:
#include <iostream>
#include <jsoncpp/json/json.h>
#include <memory>
#include "Meteosource.h"
int main()
{
// Change this to your actual API key
const std::string api_key = "YOUR-API-KEY";
// Change this to your actual tier
const std::string tier = "free";
Meteosource m = Meteosource(api_key, tier);
const std::string place_id = "london";
const std::string sections = "all";
const std::string timezone = "UTC";
const std::string language = "en";
const std::string units = "auto";
auto res = m.get_point_forecast(place_id, sections, timezone, language, units);
if (!res)
{
return -1;
}
if (res->current)
{
std::cout << "Current weather: " << res->current->summary << std::endl << std::endl;
}
if (res->minutely)
{
std::cout << "Minutely summary: " << res->minutely->summary << std::endl << std::endl;
std::cout << "Precipitation for next 5 minutes: " << res->minutely->summary << std::endl << std::endl;
for (int i = 0; i < 5; ++i)
std::cout << " " << res->minutely->data[i]->date << ": precipitation " << res->minutely->data[i]->precipitation << std::endl;
std::cout << std::endl;
}
if (res->hourly.size() > 0)
{
std::cout << "Weather for next 5 hours:" << std::endl;
for (int i = 0; i < 5; ++i)
std::cout << " " << res->hourly[i]->date << ": temperature " << res->hourly[i]->temperature << ", wind speed: " << res->hourly[i]->wind_speed << std::endl;
std::cout << std::endl;
}
if (res->daily.size() > 0)
{
std::cout << "Daily Weather for next 5 days:" << std::endl;
for (int i = 0; i < 5; ++i)
std::cout << " " << res->daily[i]->day << ": all day weather: '" << res->daily[i]->all_day->weather << "', sunrise: " << res->daily[i]->astro->sun_rise << std::endl;
std::cout << std::endl;
}
return 0;
}
Postman Collection
Použijte Postman kolekci přímo, klikem na Run in Postman
tlačítko, nebo navštivte GitHub repozitář pro více informací.