Skip to content

Commit

Permalink
✨ chore: init project
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Aug 18, 2023
1 parent 92ef0d6 commit a615a41
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 87 deletions.
28 changes: 0 additions & 28 deletions CHANGELOG.md

This file was deleted.

10 changes: 5 additions & 5 deletions README.md
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
52 changes: 29 additions & 23 deletions api/v1/_utils.ts
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;
3 changes: 1 addition & 2 deletions api/v1/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { WeatherParams } from '../../type';
import fetchWeather from './_utils';

export const config = {
Expand All @@ -8,7 +7,7 @@ export const config = {
export default async (req: Request) => {
if (req.method !== 'POST') return new Response('Method Not Allowed', { status: 405 });

const args = (await req.json()) as WeatherParams;
const args = await req.json();

const result = await fetchWeather(args);

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"private": true,
"description": "Lobe Chat Plugin Market",
"repository": "https://github.com/lobehub/chat-plugin-real-time-weather.git",
"repository": "https://github.com/lobehub/chat-plugin-search-engine.git",
"scripts": {
"ci": "npm run lint && npm run type-check",
"lint": "npm run lint:js && npm run lint:prettier",
Expand All @@ -30,6 +30,9 @@
"last 2 versions",
"not ie <= 10"
],
"dependencies": {
"query-string": "^8"
},
"devDependencies": {
"@lobehub/lint": "latest",
"@vercel/node": "^2",
Expand Down
102 changes: 74 additions & 28 deletions type.ts
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;
}

0 comments on commit a615a41

Please sign in to comment.