在线查询:https://linuxdo.1124.eu.org/
源码
from flask import Flask, request, render_template
import requests
from datetime import datetime
import pytz
app = Flask(__name__)
def get_info(username):
url = f"https://linux.do/u/{username}.json"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if "user" in data:
return data["user"]
return None
def convert_to_shanghai_time(utc_time_str):
utc_time = datetime.strptime(utc_time_str, "%Y-%m-%dT%H:%M:%S.%fZ")
utc_time = pytz.utc.localize(utc_time)
shanghai_time = utc_time.astimezone(pytz.timezone("Asia/Shanghai"))
return shanghai_time.strftime("%Y-%m-%d %H:%M:%S")
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
username = request.form.get('username')
user = get_info(username)
if user is not None:
keys_to_print = ["username", "last_posted_at", "last_seen_at", "created_at", "time_read", "groups", "trust_level"]
chinese_keys = ["用户名", "最后发帖时间", "最后在线时间", "创建时间", "阅读时间", "组", "信任等级"]
result = {}
for key, chinese_key in zip(keys_to_print, chinese_keys):
if key in user:
if key in ["last_posted_at", "last_seen_at", "created_at"]:
result[chinese_key] = convert_to_shanghai_time(user[key])
else:
result[chinese_key] = user[key]
return render_template('result.html', result=result)
else:
return "获取信息失败"
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
templates:
#######index.html
<!DOCTYPE html>
<html>
<head>
<title>用户信息查询</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
form {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}
input[type="text"] {
width: 90%;
padding: 10px;
margin-bottom: 10px;
border-radius: 4px;
border: 1px solid #ddd;
}
input[type="submit"] {
padding: 10px 20px;
border: none;
border-radius: 4px;
background-color: #007BFF;
color: #fff;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<form method="POST">
<label for="username">请输入要查询的用户名:</label>
<input type="text" id="username" name="username">
<input type="submit" value="查询">
</form>
</body>
</html>
######result.html
<!DOCTYPE html>
<html>
<head>
<title>查询结果</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
table {
border-collapse: collapse;
width: 50%;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<table>
<tr>
<th>信息</th>
<th>值</th>
</tr>
{% for key, value in result.items() %}
<tr>
<td>{{ key }}</td>
<td>{{ value }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>