Skip to content

Commit a5c1d79

Browse files
committed
更新了 24 小时天气预报 API 调用功能,库调用方法略微改动。
1 parent a55eea8 commit a5c1d79

File tree

12 files changed

+378
-49
lines changed

12 files changed

+378
-49
lines changed

src/ExampleApp/ExampleConsoleApp/ExampleConsoleApp.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<TargetFramework>net6.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8+
<Version>1.1.0</Version>
9+
<AssemblyVersion>1.1.0.*</AssemblyVersion>
10+
<FileVersion>1.1.0.*</FileVersion>
11+
<Deterministic>False</Deterministic>
812
</PropertyGroup>
913

1014
<ItemGroup>

src/ExampleApp/ExampleConsoleApp/Program.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,32 @@
33
class Program
44
{
55
static void Main()
6+
{
7+
GetWeatherAsync().Wait();
8+
}
9+
10+
private static async Task GetWeatherAsync()
611
{
712
Console.Write("请输入 API 密钥:");
813
string key = Console.ReadLine();
914
Console.Write("请输入地点名称:");
1015
string location = Console.ReadLine();
1116
try
1217
{
13-
var locationInfo = QWeatherAPI.QWeather.GetGeoAsync(location, key).Result.Locations[0];
14-
var weatherInfo = QWeatherAPI.QWeather.GetRealTimeWeatherAsync(locationInfo.Lon, locationInfo.Lat, "af71f6c5c1e94ec4abf618febf35ca68").Result;
15-
Console.WriteLine(@$"{locationInfo.Name}的天气
16-
当前温度 {weatherInfo.Now.Temp}°C
17-
体感温度 {weatherInfo.Now.FeelsLike}°C
18-
{weatherInfo.Now.Text}
19-
{weatherInfo.Now.WindDir}");
18+
var locationInfo = (await QWeatherAPI.GeoAPI.GetGeoAsync(location, key)).Locations[0];
19+
var realTimeWeatherInfo = await QWeatherAPI.RealTimeWeatherAPI.GetRealTimeWeatherAsync(locationInfo.Lon, locationInfo.Lat, key);
20+
var forecastWeatherInfo = await QWeatherAPI.WeatherHourlyForecastAPI.GetHourlyForecastWeatherAsync(locationInfo.Lon, locationInfo.Lat, key);
21+
Console.WriteLine(@$"{locationInfo.Name} 的天气
22+
当前温度 {realTimeWeatherInfo.Now.Temp}°C
23+
体感温度 {realTimeWeatherInfo.Now.FeelsLike}°C
24+
{realTimeWeatherInfo.Now.Text}
25+
{realTimeWeatherInfo.Now.WindDir}
26+
27+
明天 {forecastWeatherInfo.Hourly[23].FxTime} 的天气
28+
温度 {forecastWeatherInfo.Hourly[23].Temp}
29+
{forecastWeatherInfo.Hourly[23].Text}
30+
{forecastWeatherInfo.Hourly[23].WindDir}
31+
");
2032
}
2133
catch (ArgumentException ex)
2234
{

src/QWeather/GeoAPI.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using QWeatherAPI.Result.GeoAPI.CityLookup;
2+
using QWeatherAPI.Tools.Net.Http;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace QWeatherAPI
10+
{
11+
public class GeoAPI
12+
{
13+
#region 获取城市地理信息方法
14+
/// <summary>
15+
/// 通过地区名称获取 LocationID 和 经纬度。
16+
/// </summary>
17+
/// <param name="location">地区名称,支持绝大多数语言。</param>
18+
/// <param name="key">和风天气 API 密钥。</param>
19+
/// <param name="adm">城市的上级行政区划。</param>
20+
/// <param name="range">搜索限定范围。国家代码需使用 ISO 3166 所定义的国家代码,默认为 10。详情请参考:https://www.iso.org/obp/ui/</param>
21+
/// <param name="limit">地理位置 API 返回数据限制个数。</param>
22+
/// <param name="lang">返回数据语言,默认采用中文。详情请参考:https://dev.qweather.com/docs/resource/language/</param>
23+
/// <returns>城市信息 API 返回数据。</returns>
24+
public static async Task<GeoResult> GetGeoAsync(string location, string key, string adm, string range = "world", int limit = 10, string lang = "zh")
25+
{
26+
range = range.ToLower();
27+
string jsonData = await WebRequests.GetRequestAsync($"https://geoapi.qweather.com/v2/city/lookup?location={location}&number={limit}&adm={adm}&range={range}&lang={lang}&key={key}");
28+
return new GeoResult(jsonData);
29+
}
30+
31+
/// <summary>
32+
/// 通过地区名称获取 LocationID 和 经纬度。
33+
/// </summary>
34+
/// <param name="location">地区名称,支持绝大多数语言。</param>
35+
/// <param name="key">和风天气 API 密钥。</param>
36+
/// <param name="range">搜索限定范围。国家代码需使用 ISO 3166 所定义的国家代码,默认为 10。详情请参考:https://www.iso.org/obp/ui/</param>
37+
/// <param name="limit">地理位置 API 返回数据限制个数。</param>
38+
/// <param name="lang">返回数据语言,默认采用中文。详情请参考:https://dev.qweather.com/docs/resource/language/</param>
39+
/// <returns>城市信息 API 返回数据。</returns>
40+
public static async Task<GeoResult> GetGeoAsync(string location, string key, string range = "world", int limit = 10, string lang = "zh")
41+
{
42+
range = range.ToLower();
43+
string jsonData = await WebRequests.GetRequestAsync($"https://geoapi.qweather.com/v2/city/lookup?location={location}&number={limit}&range={range}&lang={lang}&key={key}");
44+
return new GeoResult(jsonData);
45+
}
46+
#endregion
47+
}
48+
}

src/QWeather/QWeatherAPI.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
<PackageIcon>天气 (3).png</PackageIcon>
1313
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
1414
<PackageLicenseExpression>MIT</PackageLicenseExpression>
15-
<AssemblyVersion>1.0.1.0</AssemblyVersion>
16-
<FileVersion>1.0.1.0</FileVersion>
15+
<AssemblyVersion>1.1.0.*</AssemblyVersion>
16+
<FileVersion>1.1.0.*</FileVersion>
1717
<PlatformTarget>x64</PlatformTarget>
18-
<Version>1.0.1</Version>
18+
<Version>1.1.0</Version>
19+
<Deterministic>False</Deterministic>
1920
</PropertyGroup>
2021

2122
<ItemGroup>

src/QWeather/RealTimeWeatherAPI.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using QWeatherAPI.Result.RealTimeWeather;
2+
using QWeatherAPI.Tools;
3+
using QWeatherAPI.Tools.Net.Http;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace QWeatherAPI
11+
{
12+
public static class RealTimeWeatherAPI
13+
{
14+
#region 获取实时天气方法
15+
/// <summary>
16+
/// 通过经度,纬度获取实时天气 API 数据。
17+
/// </summary>
18+
/// <param name="lon">地区经度。</param>
19+
/// <param name="lat">地区纬度。</param>
20+
/// <param name="key">和风天气 API 密钥。</param>
21+
/// <param name="unit">测量单位,Tools.Units.Metric 为公制,Tools.Units.Inch 为英制,默认采用 公制。</param>
22+
/// <param name="lang">返回数据语言,默认采用中文。详情请参考:https://dev.qweather.com/docs/resource/language/</param>
23+
/// <returns>实时天气 API 返回数据。</returns>
24+
public static async Task<WeatherResult> GetRealTimeWeatherAsync(double lon, double lat, string key, Units unit = Units.Metric, string lang = "zh")
25+
{
26+
string _unit;
27+
switch (unit)
28+
{
29+
case Units.Metric:
30+
_unit = "m";
31+
break;
32+
case Units.Inch:
33+
_unit = "i";
34+
break;
35+
default:
36+
goto case Units.Metric;
37+
}
38+
string jsonData = await WebRequests.GetRequestAsync($"https://devapi.qweather.com/v7/weather/now?location={lon},{lat}&lang={lang}&unit={_unit}&key={key}");
39+
var weather = new WeatherResult(jsonData);
40+
return weather;
41+
}
42+
43+
/// <summary>
44+
/// 通过 LocationID 获取和风天气实时天气 API 数据。
45+
/// </summary>
46+
/// <param name="id">地区 LocationID,可通过 GeoAPI.GetGeoAsync() 来获取。</param>
47+
/// <param name="key">和风天气 API 密钥。</param>
48+
/// <param name="unit">测量单位,Tools.Units.Metric 为公制,Tools.Units.Inch 为英制,默认采用 公制。</param>
49+
/// <param name="lang">返回数据语言,默认采用中文。详情请参考:https://dev.qweather.com/docs/resource/language/</param>
50+
/// <returns>实时天气 API 返回数据。</returns>
51+
public static async Task<WeatherResult> GetRealTimeWeatherAsync(string id, string key, Units unit = Units.Metric, string lang = "zh")
52+
{
53+
string _unit;
54+
switch (unit)
55+
{
56+
case Units.Metric:
57+
_unit = "m";
58+
break;
59+
case Units.Inch:
60+
_unit = "i";
61+
break;
62+
default:
63+
goto case Units.Metric;
64+
}
65+
string jsonData = await WebRequests.GetRequestAsync($"https://devapi.qweather.com/v7/weather/now?location={id}&lang={lang}&unit={_unit}&key={key}");
66+
var weather = new WeatherResult(jsonData);
67+
return weather;
68+
}
69+
#endregion
70+
}
71+
}

src/QWeather/Result/GeoAPI/CityLookup/GeoResult.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class GeoResult
1717
/// <summary>
1818
/// 位置搜索结果
1919
/// </summary>
20-
public List<Location> Locations = new List<Location>();
20+
public Location[] Locations = Array.Empty<Location>();
2121
#endregion
2222

2323
#region 构造方法
@@ -33,7 +33,9 @@ public GeoResult(string jsonString)
3333
if (this.Code != "200") { throw new ArgumentException(this.Code); }
3434
foreach (JToken location in jsonData.SelectToken("location"))
3535
{
36-
this.Locations.Add(new Location(location));
36+
var locationList = this.Locations.ToList();
37+
locationList.Add(new Location(location));
38+
this.Locations = locationList.ToArray();
3739
}
3840
}
3941
#endregion

src/QWeather/Result/RealTimeWeather/Now.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,8 @@ public Now(JToken token)
8888
this.Precip = token.Value<string>("precip");
8989
this.Pressure = token.Value<string>("pressure");
9090
this.Vis = token.Value<string>("vis");
91-
#nullable enable
9291
this.Cloud = token.Value<string?>("cloud");
9392
this.Dew = token.Value<string?>("dew");
94-
#nullable restore
9593
}
9694
#endregion
9795
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using Newtonsoft.Json.Linq;
2+
3+
namespace QWeatherAPI.Result.WeatherHourlyForecast
4+
{
5+
public class Hourly
6+
{
7+
#region 声明字段
8+
/// <summary>
9+
/// 更新时间
10+
/// </summary>
11+
public string FxTime;
12+
/// <summary>
13+
/// 温度
14+
/// </summary>
15+
public string Temp;
16+
/// <summary>
17+
/// 图标代码
18+
/// </summary>
19+
public string Icon;
20+
/// <summary>
21+
/// 天气情况文字介绍
22+
/// </summary>
23+
public string Text;
24+
/// <summary>
25+
/// 风向角度
26+
/// </summary>
27+
public string Wind360;
28+
/// <summary>
29+
/// 风向
30+
/// </summary>
31+
public string WindDir;
32+
/// <summary>
33+
/// 风力等级
34+
/// </summary>
35+
public string WindScale;
36+
/// <summary>
37+
/// 风速,公里/小时
38+
/// </summary>
39+
public string WindSpeed;
40+
/// <summary>
41+
/// 相对湿度,百分比数值
42+
/// </summary>
43+
public string Humidity;
44+
/// <summary>
45+
/// 逐小时预报降水概率,百分比数值(可能为空)
46+
/// </summary>
47+
public string Pop;
48+
/// <summary>
49+
/// 每小时降雨量(默认单位:毫米)
50+
/// </summary>
51+
public string Precip;
52+
/// <summary>
53+
/// 大气压强(默认单位:百帕)
54+
/// </summary>
55+
public string Pressure;
56+
/// <summary>
57+
/// 云量,百分比数值(可能为空)
58+
/// </summary>
59+
public string Cloud;
60+
/// <summary>
61+
/// 露点温度(可能为空)
62+
/// </summary>
63+
public string Dew;
64+
#endregion
65+
66+
#region 构造方法
67+
public Hourly(JToken token)
68+
{
69+
this.FxTime = token.Value<string>("fxTime");
70+
this.Temp = token.Value<string>("temp");
71+
this.Icon = token.Value<string>("icon");
72+
this.Text = token.Value<string>("text");
73+
this.Wind360 = token.Value<string>("wind360");
74+
this.WindDir = token.Value<string>("windDir");
75+
this.WindScale = token.Value<string>("windScale");
76+
this.WindSpeed = token.Value<string>("windSpeed");
77+
this.Humidity = token.Value<string>("humidity");
78+
this.Pop = token.Value<string>("pop");
79+
this.Precip = token.Value<string>("precip");
80+
this.Pressure = token.Value<string>("pressure");
81+
this.Cloud = token.Value<string?>("cloud");
82+
this.Dew = token.Value<string?>("dew");
83+
}
84+
#endregion
85+
}
86+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Newtonsoft.Json.Linq;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace QWeatherAPI.Result.WeatherHourlyForecast
9+
{
10+
public class WeatherResult
11+
{
12+
#region 声明字段
13+
/// <summary>
14+
/// API 状态码
15+
/// </summary>
16+
public string Code;
17+
/// <summary>
18+
/// API 最近更新时间
19+
/// </summary>
20+
public string UpdateTime;
21+
/// <summary>
22+
/// 当前数据的响应式页面,便于嵌入网站或应用
23+
/// </summary>
24+
public string FxLink;
25+
/// <summary>
26+
/// 24 小时预报结果
27+
/// </summary>
28+
public Hourly[] Hourly = Array.Empty<Hourly>();
29+
#endregion
30+
31+
#region 构造方法
32+
/// <summary>
33+
/// 构造 24 天气预报 API 返回结果
34+
/// </summary>
35+
/// <param name="jsonString"></param>
36+
/// <exception cref="ArgumentException"></exception>
37+
public WeatherResult(string jsonString)
38+
{
39+
JObject jsonData = JObject.Parse(jsonString);
40+
this.Code = jsonData.Value<string>("code");
41+
if (this.Code != "200") { throw new ArgumentException(this.Code); }
42+
this.UpdateTime = jsonData.Value<string>("updateTime");
43+
this.FxLink = jsonData.Value<string>("fxLink");
44+
foreach (var item in jsonData.SelectToken("hourly"))
45+
{
46+
var hourlyList = this.Hourly.ToList();
47+
hourlyList.Add(new Hourly(item));
48+
this.Hourly = hourlyList.ToArray();
49+
}
50+
}
51+
#endregion
52+
}
53+
}

src/QWeather/Tools/Units.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace QWeatherAPI.Tools
8+
{
9+
#region 单位枚举
10+
public enum Units
11+
{
12+
Metric,
13+
Inch
14+
}
15+
#endregion
16+
}

0 commit comments

Comments
 (0)