黑暗天空API包装
darkskylib的Python项目详细描述
这个库为Dark Sky API提供了对 来自世界各地的天气信息。
快速启动
在开始使用这个库之前,需要获取API密钥 here。
API调用
函数forecast处理所有请求参数并返回 Forecast对象。
>>>fromdarkskyimportforecast>>>boston=forecast(key,42.3601,-71.0589)>>>
前3个位置参数与所需的3个相同 API调用的参数可选的查询参数必须是 作为关键字参数提供
使用time参数将获得时间机器调用。 使用timeout参数将设置默认值request timeout
>>>BOSTON=key,42.3601,-71.0589>>>fromdatetimeimportdatetimeasdt>>>t=dt(2013,5,6,12).isoformat()>>>boston=forecast(*BOSTON,time=t)>>>boston.time1367866800
数据点和数据块
值以及DataPoint和DataBlock对象是 使用实例属性或字典键访问。您可以访问 直接当前值,而不经过currently数据点
>>>boston['currently']['temperature']60.72>>>boston.temperature60.72
数据块可通过其data值进行索引和iteable。
>>>len(boston.hourly)24>>>>>>boston.hourly[1].temperature59.49>>>>>># list temperatures for next 10 hours...[hour.temperatureforhourinboston.hourly[:10]][60.83,59.49,58.93,57.95,56.01,53.95,51.21,49.21,47.95,46.31]
不存在的属性将引发^ {TT9}$和字典键 KeyError如你所料。
原始数据
要获取原始数据字典,您可以通过 实例属性或通过字典键导航到它,相同的 你浏览字典的方式
>>>boston.hourly[2]{'ozone':290.06,'temperature':58.93,'pressure':1017.8,'windBearing':274,'dewPoint':52.58,'cloudCover':0.29,'apparentTemperature':58.93,'windSpeed':7.96,'summary':'Partly Cloudy','icon':'partly-cloudy-night','humidity':0.79,'precipProbability':0,'precipIntensity':0,'visibility':8.67,'time':1476410400}>>>>>>boston['hourly']['data'][2]{'ozone':290.06,'temperature':58.93,'pressure':1017.8,'windBearing':274,'dewPoint':52.58,'cloudCover':0.29,'apparentTemperature':58.93,'windSpeed':7.96,'summary':'Partly Cloudy','icon':'partly-cloudy-night','humidity':0.79,'precipProbability':0,'precipIntensity':0,'visibility':8.67,'time':1476410400}
标志和警报
标记的属性名中的所有破折号-都替换为 下划线_。这不会影响字典键
>>># instead of 'boston.flags.isd-stations'...boston.flags.isd_stations['383340-99999','383390-99999','383410-99999','384620-99999','384710-99999']>>>>>>boston.flags['isd-stations']['383340-99999','383390-99999','383410-99999','384620-99999','384710-99999']
即使警报由列表表示,数据可访问性 对于列表中的警报,将保留“通过实例”属性。
>>>boston.alerts[0].title'Freeze Watch for Norfolk, MA'
更新数据
使用refresh()方法更新Forecast对象的数据。这个 refresh()方法接受可选查询(包括time,使 它是一个time machine对象)作为关键字参数。打电话 refresh()如果没有任何参数,则会将所有查询设置为默认值 价值观。使用timeout参数设置请求超时。
>>>boston.refresh()>>>(boston.time,boston.temperature,len(boston.hourly))(1476403500,60.72,49)>>>>>>boston.refresh(units='si',extend='hourly')>>>(boston.time,boston.temperature,len(boston.hourly))(1476404205,15.81,169)>>>>>>boston.refresh(units='us')>>>(boston.time,boston.temperature,len(boston.hourly))(1476404489,60.57,49)
对于开发者
响应头存储在^{tt19}下的字典中$ 属性。
>>>boston.response_headers['X-response-Time']'146.035ms'
示例脚本
fromdarkskyimportforecastfromdatetimeimportdate,timedeltaBOSTON=42.3601,71.0589weekday=date.today()withforecast('API_KEY',*BOSTON)asboston:print(boston.daily.summary,end='\n---\n')fordayinboston.daily:day=dict(day=date.strftime(weekday,'%a'),sum=day.summary,tempMin=day.temperatureMin,tempMax=day.temperatureMax)print('{day}: {sum} Temp range: {tempMin} - {tempMax}'.format(**day))weekday+=timedelta(days=1)
输出:
Light rain on Friday and Saturday, with temperatures bottoming out at 48°F on Tuesday. --- Sun: Partly cloudy in the morning. Temp range: 44.86 - 57.26°F Mon: Mostly cloudy in the morning. Temp range: 44.26 - 55.28°F Tue: Clear throughout the day. Temp range: 36.85 - 47.9°F Wed: Partly cloudy starting in the afternoon, continuing until evening. Temp range: 33.23 - 47.93°F Thu: Light rain overnight. Temp range: 35.75 - 49.71°F Fri: Light rain in the morning and afternoon. Temp range: 45.47 - 57.11°F Sat: Drizzle in the morning. Temp range: 43.3 - 62.08°F Sun: Clear throughout the day. Temp range: 39.81 - 60.84°F