generated from lobehub/chat-plugin-realtime-weather
-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
113 additions
and
87 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
# LobeChat Plugin RealTime Weather | ||
# LobeChat Plugin Search Engine | ||
|
||
[![Coverage][coverage]][codecov-url] ![Test CI status][test-ci] | ||
|
||
[test-ci]: https://github.com/lobehub/chat-plugin-market/workflows/Test/badge.svg | ||
[coverage]: https://codecov.io/gh/arvinxx/vercel-serverless-api-template/branch/master/graph/badge.svg | ||
[codecov-url]: https://codecov.io/gh/arvinxx/vercel-serverless-api-template/branch/master | ||
[test-ci]: https://github.com/lobehub/chat-plugin-search-engine/workflows/Test/badge.svg | ||
[coverage]: https://codecov.io/gh/lobehub/chat-plugin-search-engine/branch/master/graph/badge.svg | ||
[codecov-url]: https://codecov.io/gh/lobehub/chat-plugin-search-engine/branch/master | ||
|
||
LobeChat Plugin RealTime Weather | ||
LobeChat Plugin Search Engine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,39 @@ | ||
import { Response, WeatherParams, WeatherResult } from '../../type'; | ||
import { OrganicResults, Result } from '../../type'; | ||
|
||
const weatherBaseURL = 'https://restapi.amap.com/v3/weather/weatherInfo'; | ||
const BASE_URL = 'https://serpapi.com/search'; | ||
|
||
const citySearchURL = 'https://restapi.amap.com/v3/config/district'; | ||
const API_KEY = process.env.SERPAI_API_KEY; | ||
|
||
const KEY = process.env.GAODE_WEATHER_KEY; | ||
const fetchResult = async ({ keywords }: { keywords: string }): Promise<Result> => { | ||
const params = { | ||
api_key: API_KEY, | ||
engine: 'google', | ||
gl: 'cn', | ||
google_domain: 'google.com', | ||
hl: 'zh-cn', | ||
location: 'China', | ||
q: keywords, | ||
}; | ||
|
||
const fetchCityCode = async (keywords: string): Promise<string> => { | ||
const URL = `${citySearchURL}?keywords=${keywords}&subdistrict=0&extensions=base&key=${KEY}`; | ||
const res = await fetch(URL); | ||
const { default: querystring } = await import('query-string'); | ||
|
||
const data = await res.json(); | ||
console.log(data); | ||
|
||
return data.districts[0].adcode; | ||
}; | ||
const query = querystring.stringify(params); | ||
|
||
const fetchWeather = async ({ | ||
city, | ||
extensions = 'all', | ||
}: WeatherParams): Promise<WeatherResult> => { | ||
const cityCode = await fetchCityCode(city); | ||
const res = await fetch(`${BASE_URL}?${query}`); | ||
|
||
const URL = `${weatherBaseURL}?city=${cityCode}&extensions=${extensions}&key=${KEY}`; | ||
const res = await fetch(URL); | ||
|
||
const data: Response = await res.json(); | ||
const data = await res.json(); | ||
|
||
return data.forecasts; | ||
const results = data.organic_results as OrganicResults; | ||
|
||
return results.map((r) => ({ | ||
content: r.snippet, | ||
date: r.date, | ||
displayed_link: r.displayed_link, | ||
favicon: r.favicon, | ||
link: r.link, | ||
source: r.source, | ||
title: r.title, | ||
})); | ||
}; | ||
|
||
export default fetchWeather; | ||
export default fetchResult; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,82 @@ | ||
export interface WeatherParams { | ||
city: string; | ||
extensions?: 'base' | 'all'; | ||
export type OrganicResults = OrganicResult[]; | ||
|
||
export interface SearchItem { | ||
content: string; | ||
date?: string; | ||
displayed_link?: string; | ||
favicon?: string; | ||
link: string; | ||
source?: string; | ||
title: string; | ||
} | ||
export type Result = SearchItem[]; | ||
|
||
interface OrganicResult { | ||
about_page_link: string; | ||
about_page_serpapi_link: string; | ||
about_this_result: AboutThisResult; | ||
cached_page_link?: string; | ||
date?: string; | ||
displayed_link: string; | ||
favicon?: string; | ||
link: string; | ||
position: number; | ||
related_results?: RelatedResult[]; | ||
rich_snippet?: RichSnippet; | ||
snippet: string; | ||
snippet_highlighted_words?: string[]; | ||
source: string; | ||
thumbnail?: string; | ||
title: string; | ||
} | ||
export type WeatherResult = Forecast[]; | ||
|
||
export interface Response { | ||
count: string; | ||
forecasts: Forecast[]; | ||
info: string; | ||
infocode: string; | ||
status: string; | ||
interface AboutThisResult { | ||
languages: string[]; | ||
regions: string[]; | ||
source: Source; | ||
} | ||
|
||
export interface Forecast { | ||
adcode: string; | ||
casts: Cast[]; | ||
city: string; | ||
province: string; | ||
reporttime: string; | ||
interface Source { | ||
description: string; | ||
icon: string; | ||
security?: string; | ||
source_info_link?: string; | ||
} | ||
|
||
export interface Cast { | ||
interface RelatedResult { | ||
about_page_link: string; | ||
about_page_serpapi_link: string; | ||
about_this_result: AboutThisResult2; | ||
cached_page_link: string; | ||
date: string; | ||
daypower: string; | ||
daytemp: string; | ||
daytemp_float: string; | ||
dayweather: string; | ||
daywind: string; | ||
nightpower: string; | ||
nighttemp: string; | ||
nighttemp_float: string; | ||
nightweather: string; | ||
nightwind: string; | ||
week: string; | ||
displayed_link: string; | ||
link: string; | ||
position: number; | ||
snippet: string; | ||
snippet_highlighted_words: string[]; | ||
title: string; | ||
} | ||
|
||
interface AboutThisResult2 { | ||
languages: string[]; | ||
regions: string[]; | ||
source: Source2; | ||
} | ||
|
||
interface Source2 { | ||
description: string; | ||
icon: string; | ||
} | ||
|
||
interface RichSnippet { | ||
top: Top; | ||
} | ||
|
||
interface Top { | ||
detected_extensions: DetectedExtensions; | ||
extensions: string[]; | ||
} | ||
|
||
interface DetectedExtensions { | ||
month_ago: number; | ||
} |