Skip to content

Commit 88f0d37

Browse files
committed
add: weather api
1 parent 00a53e6 commit 88f0d37

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/agents/weather.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { BaseAgent } from './base';
2+
3+
/*
4+
5+
message WeatherResult {
6+
string country = 1;
7+
string region = 2;
8+
string localtime = 3;
9+
float temperature = 4;
10+
repeated string weather_descriptions = 5;
11+
float humidity = 6;
12+
float wind_speed = 7;
13+
float wind_dir = 8;
14+
float cloudcover = 9;
15+
float feelslike = 10;
16+
float uv_index = 11;
17+
}
18+
*/
19+
20+
interface WeatherResult {
21+
country: string;
22+
region: string;
23+
localtime: string;
24+
temperature: number;
25+
weather_descriptions: string[];
26+
humidity: number;
27+
wind_speed: number;
28+
wind_dir: number;
29+
cloudcover: number;
30+
feelslike: number;
31+
uv_index: number;
32+
}
33+
34+
export class WeatherAgent extends BaseAgent {
35+
constructor() {
36+
super();
37+
}
38+
39+
async getWeather(city: string, country: string) {
40+
const weatherApiKey = "a766256befc0904d3ff5edb4f8e5b3a9"
41+
const response = await fetch(`https://api.weatherstack.com/current?access_key=${weatherApiKey}&query=${city},${country}`);
42+
const data = await response.json();
43+
44+
const result: WeatherResult = {
45+
country: data.location.country,
46+
region: data.location.region,
47+
localtime: data.location.localtime,
48+
temperature: data.current.temperature,
49+
weather_descriptions: data.current.weather_descriptions,
50+
humidity: data.current.humidity,
51+
wind_speed: data.current.wind_speed,
52+
wind_dir: data.current.wind_dir,
53+
cloudcover: data.current.cloudcover,
54+
feelslike: data.current.feelslike,
55+
uv_index: data.current.uv_index,
56+
};
57+
return result;
58+
}
59+
}

src/app.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { joinSuggestions, PaperScoreResultSchema } from './agents/paper-score.sc
1212
import { z } from 'zod';
1313
import { asyncHandler } from './middleware/error-handler';
1414
import { Server } from 'http';
15+
import { WeatherAgent } from './agents/weather';
1516

1617
export const app = express();
1718
const port = process.env['PORT'] || 8000;
@@ -30,6 +31,7 @@ const paperClassifier = new PaperClassificationAgent();
3031
const paperScoreAgent = new PaperScoreAgent();
3132
const paperMatchWeaknessSectionsAgent = new PaperMatchWeaknessSectionsAgent();
3233
const paperScoreSummaryAgent = new PaperReviewSummaryWeaknessAgent();
34+
const weatherAgent = new WeatherAgent();
3335

3436
app.get('/', (_: Request, res: Response) => {
3537
res.send('Hello, TypeScript Express!');
@@ -71,6 +73,11 @@ const ScorePaperRequestSchema = z.object({
7173
] as const),
7274
});
7375

76+
const WeatherRequestSchema = z.object({
77+
city: z.string().min(1, 'city is required'),
78+
country: z.string().min(1, 'country is required'),
79+
});
80+
7481
app.post(
7582
'/paper-score-comments',
7683
asyncHandler(async (req: Request, res: Response) => {
@@ -144,6 +151,16 @@ app.post(
144151
}),
145152
);
146153

154+
// 获得天气
155+
app.get(
156+
'/weather',
157+
asyncHandler(async (req: Request, res: Response) => {
158+
const { city, country } = WeatherRequestSchema.parse(req.query);
159+
const result = await weatherAgent.getWeather(city, country);
160+
res.send(result);
161+
}),
162+
);
163+
147164
// Global error handler
148165
app.use((err: Error, _: Request, res: Response, __: express.NextFunction) => {
149166
console.error('Unhandled error:', err);

0 commit comments

Comments
 (0)