Linux美化开机欢迎语并使用Python获取Ip调用天气

Linux 美化开机欢迎语并使用 Python 获取 IP 调用天气

本篇文章涉及知识:

  • Python 的 requests 的使用
  • Python 对 JSON 数据的处理
  • Linux 中 Toilet 命令的基本使用
  • .bashrc 脚本的基础使用
  • echo 颜色输出

.bashrc

.bashrc 是 bash 终端里面指令运行的配置脚本——可以简单理解为登录后自动执行的脚本文件。

修改文件内容

1
vim ~/.bashrc

.bashrc 做出的修改,会在下一次启动终端时候生效。如果希望立即生效,可以执行 source 指令:

1
source ~/.bashrc

Toilet

toilet 是一个命令行工具,用于生成漂亮的 ASCII 艺术字。它类似于 figlet,支持多种字体、颜色和样式。

1. 安装

1
apt-get install toilet

2. 显示简单的文本

1
toilet "Hello, World!"
1
2
3
4
5
6
输出结果
# H H EEEEE L L OOO W W OOO RRRR L DDDD
# H H E L L O O W W O O R R L D D
# HHHHH EEEE L L O O W W W O O RRRR L D D
# H H E L L O O W W W O O R R L D D
# H H EEEEE LLLLL LLLLL OOO W W OOO R RR LLLLL DDDD

3. 常用选项

  • -f [字体名] 指定字体文件
  • -F [效果] 添加特殊效果(border, metal 等)
  • --gay 彩虹色文字
  • -w [宽度] 设置输出宽度

4. 组合使用

1
toilet -F border --gay "Hello World!"

因此,我们可以把 toilet 的命令添加到 .bashrc 文件中,开机就可以自动展示自定义的彩色字符画。

Python

1. 下载 requests

1
pip install requests

2. 简单使用

1
2
3
4
5
6
import requests

url = "https://www.cz88.net/api/cz88/ip/base"
params = {"ip": "要查询的ip"}
headers = {"User-Agent": "Mozilla/5.0"}
response = requests.get(url, headers=headers, params=params)

3. 解析响应

1
2
3
if response.status_code == 200:
data = response.json()
print(data)

4. 代码示例

4.1 通过 IP 调用 API 返回地址值

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
import requests
import os

# 获取登录者 IP
ip = os.popen("last | head -n 1 | awk '{print $3}'").read().strip()

# 请求的 URL
url = f"https://www.cz88.net/api/cz88/ip/base?ip={ip}"
headers = {"User-Agent": "Mozilla/5.0"}

# 发起 GET 请求
ipResponse = requests.get(url, headers=headers)
city_code = ""

if ipResponse.status_code == 200:
data = ipResponse.json()
if data.get("success"):
ip_data = data["data"]
country = ip_data["country"]
province = ip_data["province"]
city = ip_data["city"]
city_code = ip_data["cityCode"]
isp = ip_data["isp"]
time = data["time"]
print(f"{country} {province} {city} {isp} {time}")

4.2 通过城市 code 获取天气

1
2
3
4
5
6
7
8
9
10
11
12
weatherResponse = requests.get(f"https://restapi.amap.com/v3/weather/weatherInfo?city={city_code}&key=[你的key]")

if weatherResponse.status_code == 200:
weather_data = weatherResponse.json()
if weather_data.get("status") == "1":
live_weather = weather_data["lives"][0]
weather = live_weather["weather"]
temperature = live_weather["temperature"]
wind_direction = live_weather["winddirection"]
wind_power = live_weather["windpower"]
humidity = live_weather["humidity"]
print(f"天气:{weather} 温度:{temperature} 风向:{wind_direction} 风力:{wind_power} 湿度:{humidity}%")

5. 在 Linux 启动时自动执行

将代码保存为 ipInfo.py,然后在 .bashrc 文件中添加:

1
python3 /usr/local/python/ipInfo.py

这样,当你登录 Linux 服务器时,就会自动弹出字符画和 IP 信息。

6. 美化输出

可以使用 tputpvlolcat 进行美化:

1
apt install pv lolcat

然后使用以下方式打印美化输出:

1
2
3
4
bold=$(tput bold)
normal=$(tput sgr0)
output=$(python3 /usr/local/python/ipInfo.py)
echo -e "${bold}${output}${normal}" | pv -qL 10 | lolcat

这样,文字会以每秒 10 个字符的速度逐字打印,并且带有彩虹渐变。

7. 参考资料


后会有期!

2024 年 12 月 8 日 12:45:31


Linux美化开机欢迎语并使用Python获取Ip调用天气
https://www.zheep.top/2024/12/08/Linux美化开机欢迎语并使用Python获取Ip调用天气/
作者
西行寺岩羊
发布于
2024年12月8日
许可协议