Skip to content

Commit f71fbda

Browse files
feat: Changed Weather API
1 parent 9a9cd7b commit f71fbda

File tree

4 files changed

+23
-28
lines changed

4 files changed

+23
-28
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ WAKATIME_URL="https://wakatime.com/share"
2020
WAKATIME_GLOBAL="/@AndcoolSystems/c20041f4-a965-47c3-ac36-7234e622a980.json" // WakaTime Global stats
2121
WAKATIME_LANGS="/@AndcoolSystems/eaa20c39-4e68-49d3-8760-93e93fbf1ff5.json" // WakaTime Langs stats
2222

23-
WEATHER_API="https://weather.andcool.ru/api" // Path for weather API
24-
WEATHER_QUERY="/?place=andcool&language=en&json=true" // Query for weather API
23+
WEATHER_TOKEN="61d202e168925f843260a7f646f65118" // OpenWeatherMap Token
24+
WEATHER_LAT="xxx" // Location Latitude
25+
WEATHER_LON="xxx" // Location Longitude
2526

2627
ACTIVITY_API="https://activity.andcool.ru/" // Path for activity API
2728
ACTIVITY_ID="t9mdtk" // ID of activity
@@ -34,7 +35,6 @@ DATETIME_TIMEZONE="Etc/GMT-3" // Yours timezone
3435
- `DESCRIPTION` – Описание виджета. Будет добавлено ключу `description` в json.
3536
- `WAKATIME_GLOBAL`, `WAKATIME_LANGS` – Статистика WakaTime. Можно получить [тут](https://wakatime.com/share/embed). Сгенерируйте типы чартов `Coding Activity` и `Languages` соответственно в формате JSON и вставьте их в соответствующие ключи в конфиге (исключая `https://wakatime.com/share`).
3637

37-
- `WEATHER_API`, `WEATHER_QUERY` – URL и запрос для API погоды. Используется проект [weather.andcool.ru](https://weather.andcool.ru)
3838
- `ACTIVITY_API`, `ACTIVITY_ID` – URL и ID для API активности. Используется проект [activity.andcool.ru](https://github.com/Andcool-Systems/Andcool-Activity)
3939

4040
- `DATETIME_TIMEZONE` – Часовой пояс в формате JavaScript Date.

src/apis/apis.service.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,7 @@ export class APIService {
229229
}
230230
}
231231

232-
async getWeather(
233-
api: string,
234-
query: string
235-
): Promise<WeatherResponse | null> {
232+
async getWeather(query: string): Promise<WeatherResponse | null> {
236233
try {
237234
const cache = await this.cacheManager.get<string>(
238235
`weather:${query}`
@@ -242,20 +239,30 @@ export class APIService {
242239
return JSON.parse(cache);
243240
}
244241

245-
const response = await instance.get(`${api}${query}`, {
246-
validateStatus: () => true
247-
});
242+
const capitalize = (str: string) =>
243+
String(str).charAt(0).toUpperCase() + String(str).slice(1);
248244

249-
if (response.status !== 200) {
250-
return null;
251-
}
245+
const response = await axios.get(
246+
`https://api.openweathermap.org/data/2.5/weather` +
247+
`?lat=${process.env.WEATHER_LAT}` +
248+
`&lon=${process.env.WEATHER_LON}` +
249+
`&appid=${process.env.WEATHER_TOKEN}` +
250+
`&units=metric`
251+
);
252+
253+
if (response.status !== 200) return null;
254+
255+
const data = {
256+
temp: response.data.main.temp,
257+
condition: capitalize(response.data.weather[0].description)
258+
};
252259

253260
await this.cacheManager.set(
254261
`weather:${query}`,
255-
JSON.stringify(response.data),
262+
JSON.stringify(data),
256263
1000 * 60 * 30
257264
);
258-
return response.data;
265+
return data;
259266
} catch (e) {
260267
console.error(e);
261268
return null;

src/apis/types.d.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,7 @@ interface WakatimeLanguages {
8282

8383
interface WeatherResponse {
8484
temp: number;
85-
feels_like: number;
86-
pressure: number;
87-
visibility: number;
88-
humidity: number;
89-
wind: {
90-
speed: number;
91-
deg: number;
92-
};
9385
condition: string;
94-
icon: string;
9586
}
9687

9788
interface ActivityResponse {

src/widget/widget.service.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@ export class WidgetService {
5858
this.apiService.getWakatimeLanguages(
5959
process.env.WAKATIME_LANGS
6060
),
61-
this.apiService.getWeather(
62-
process.env.WEATHER_API,
63-
process.env.WEATHER_QUERY
64-
),
61+
this.apiService.getWeather(process.env.WEATHER_QUERY),
6562
this.apiService.getActivity(
6663
process.env.ACTIVITY_API,
6764
process.env.ACTIVITY_ID

0 commit comments

Comments
 (0)