-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtools.py
More file actions
75 lines (64 loc) · 2.23 KB
/
tools.py
File metadata and controls
75 lines (64 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:ping
# datetime:2025/4/2 14:43
import httpx
import aiohttp
import os
OPENWEATHER_API_BASE = "https://api.openweathermap.org/data/2.5/weather"
API_KEY = "xxxxxx" # 请替换为你自己的 OpenWeather API Key
USER_AGENT = "weather-app/1.0"
async def fetch_weather(city: str) :
"""
从 OpenWeather API 获取天气信息。
:param city: 城市名称(需使用英文,如 Beijing)
:return: 天气数据字典;若出错返回包含 error 信息的字典
"""
params = {
"q": city,
"appid": API_KEY,
"units": "metric",
"lang": "zh_cn"
}
headers = {"User-Agent": USER_AGENT}
async with httpx.AsyncClient() as client:
try:
response = await client.get(OPENWEATHER_API_BASE, params=params, headers=headers, timeout=30.0)
response.raise_for_status()
return response.json() # 返回字典类型
except httpx.HTTPStatusError as e:
return {"error": f"HTTP 错误: {e.response.status_code}"}
except Exception as e:
return {"error": f"请求失败: {str(e)}"}
#---------------关键词抽取-------------------
async def get_keyword(query):
print("*************",query)
params = {
"sentence": query
}
print("params",params)
url = "http://x/nlp/getTerms"
async with aiohttp.ClientSession() as session:
async with session.post(url, data=params) as res:
result = await res.json()
res = result.get("result")
if res:
term = res.get("terms")
else:
term = []
return "".join(term)
return "".join(result.get("result", []) ) # 直接返回 spoList,默认为空列表
#---------------摘要抽取-------------------
async def get_summery(data):
url="http://xxxx/nlp/doc2title"
params = {"docText": data}
print("**************",params)
async with aiohttp.ClientSession() as session:
async with session.post(url, data=params) as res:
result = await res.json()
res = result.get("result")
if res:
term = res.get("title")
else:
term = []
return "".join(term)