
Python搭建咕咕咕时间API
1.导入
1.1前言
《Python搭建咕咕咕时间API》文如其名,我在翻友链博客的时候发现许多博客页脚都会有“距离上次更新xxx天xxx小时xxx分钟”,也想给自己博客添加一个,翻遍了主题后台,愣是没找到这个功能,索性一不做二不休,自己搓一个出来。
1.2准备
1.需要你的博客是wordpress博客,且启用了rss
2.Python环境,可以看我之前的文章安装
3.Python模块
4.服务器放行8848端口
import time, feedparser
from flask import Flask, make_response
from datetime import datetime
import pytz
可以使用:
python -m pip install time feedparser datetime pytz
2.代码
2.1Python代码
话不多说直接上代码,使用方法及端口可以跳转3.1
import time, feedparser
from flask import Flask, make_response
from datetime import datetime
import pytz
app = Flask(__name__)
suoxieLIB = {'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu': 4, 'Fri': 5, 'Sat': 6, 'Sun': 7,
'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
@app.route('/')
def hello_world():
"""
变量注释:
rss_ochina:FEED原文
datay:日期原文&日期格式化列表
fresponse:返回数据参数
dt:当前时间
zerotime:基准线时间(+0000)
chattime:本地实际转基准线时间
timestampNOW:当前时间戳
timestampARC:最后一篇文章更新时间时间戳
chageTIME:现在时间和最后一篇文章更新时间的差(秒)
"""
dt = datetime.now()
#print(f'本地时间:{dt}')
zerotime = pytz.timezone('Etc/GMT-0')
chatime = dt.astimezone(zerotime)
#print(f'基准线时间:{chatime}')
rss_oschina = feedparser.parse('https://lowion.cn/?feed=rss2') # 获取rss
#print('Start')
datay = rss_oschina['entries'][0]['published'].split(" ")
for i in suoxieLIB:
if datay[0].find(i)!=-1:
datay[0]=datay[0].replace(i,str(suoxieLIB[i]))
if datay[2].find(i)!=-1:
datay[2]=datay[2].replace(i,str(suoxieLIB[i]))
#print(datay)
chatime=str(chatime).split(' ')
chatime[1]=chatime[1].replace('+00:00','').split(':')
chatime[1][2]=chatime[1][2].split('.')[0]
chatime=chatime[0]+' '+chatime[1][0]+':'+chatime[1][1]+':'+chatime[1][2]
#print(chatime)
dt = chatime
# 转换成时间数组
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
# 转换成时间戳
timestampNOW = time.mktime(timeArray)
#print(timestampNOW)
for i in range(0,len(datay)):
datay[i]=datay[i].replace(',','')
datay[i]=str(datay[i])
if len(datay[i])==1:
datay[i]='0'+datay[i]
#print(datay)
dt = datay[3]+'-'+datay[2]+'-'+datay[1]+' '+datay[4]
# 转换成时间数组
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
# 转换成时间戳
timestampARC = time.mktime(timeArray)
#print(timestampARC)
timestampARC=int(timestampARC)
timestampNOW=int(timestampNOW)
chageTIME=timestampNOW-timestampARC
seconds=chageTIME
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
#print("%02d:%02d:%02d" % (h, m, s))
backjson={'hours':h,'min':m,'sec':s}
fresponse = make_response(backjson) # 防止跨域报错
fresponse.headers['Access-Control-Allow-Origin'] = '*'
return fresponse
if __name__ == '__main__':
app.config['JSON_AS_ASCII'] = False
app.run(host='0.0.0.0', port=8848)
2.2代码释义
主要变量列表,如需添加功能可以对照

2.2.1代码分块
将当前时间转为GMT-0时区并移除小数点后数位
dt = datetime.now()
zerotime = pytz.timezone('Etc/GMT-0')
chatime = dt.astimezone(zerotime)
chatime = str(chatime).split(' ')
chatime[1] = chatime[1].replace('+00:00', '').split(':')
chatime[1][2] = chatime[1][2].split('.')[0]
chatime = chatime[0] + ' ' + chatime[1][0] + ':' + chatime[1][1] + ':' + chatime[1][2]
获取rss并切割出最新文章的更新时间

rss_oschina = feedparser.parse('https://lowion.cn/?feed=rss2') # 获取rss
datay = rss_oschina['entries'][0]['published'].split(" ")
将简写日期转为数字
获取到的日期有些是简写的,如:Mar,Sat等
suoxieLIB = {'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu': 4, 'Fri': 5, 'Sat': 6, 'Sun': 7,
'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10,
'Nov': 11, 'Dec': 12}
for i in suoxieLIB:
if datay[0].find(i) != -1:
datay[0] = datay[0].replace(i, str(suoxieLIB[i]))
if datay[2].find(i) != -1:
datay[2] = datay[2].replace(i, str(suoxieLIB[i]))
转换时间戳
dt = chatime
# 转换成时间数组
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
# 转换成时间戳
timestampNOW = time.mktime(timeArray)
格式转换并计算差值,进而算出咕咕咕时间
timestampARC = int(timestampARC)
timestampNOW = int(timestampNOW)
chageTIME = timestampNOW - timestampARC
seconds = chageTIME
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
# print("%02d:%02d:%02d" % (h, m, s))
backjson = {'hours': h, 'min': m, 'sec': s}
添加header防止跨域警告
fresponse = make_response(backjson) # 防止跨域报错
fresponse.headers['Access-Control-Allow-Origin'] = '*'
return fresponse
3.完成
3.1使用

使用前需要将红框里的地址改为你博客的RSS地址,改好运行后可以通过访问:
”http://你服务器的ip+:8848“ 使用
- 感谢你赐予我前进的力量
赞赏者名单
因为你们的支持让我意识到写文章的价值🙏
本文是原创文章,采用 CC BY-NC-SA 协议,完整转载请注明来自 Lowion.cn
评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果