It use in widget.
1.get date in a json file
2.get date from datetime
3.If the two dates are the same, read the json file,else update the json file and read it.
4.put the result in widget.
I am a newbie, please advise.
And I want to ask how to display multiple buttons in the widget and can turn pages?
# -*- coding: UTF-8 -*-
import requests
import re
import json
import datetime
import appex, ui
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
'Accept-Language': 'zh-CN,zh;q=0.9'
}
def get_page(url):
response = requests.get(url, headers=headers)
data = json.loads(response.text)
return data
def write_file(data):
with open(r'./mryj.json', 'w+') as f:
f.write(json.dumps(data))
print('文件已更新')
def get_filedate():
try:
with open(r'./mryj.json', 'r') as f:
d = json.loads(f.read())
filedate = d['dateline']
print('文件日期 ', filedate)
except:
with open(r'./mryj.json', 'w+') as f:
d = f.write(json.dumps({'dateline':'None'}))
filedate = 'None'
return filedate
def get_datetime():
timenow = datetime.datetime.now().strftime('%Y-%m-%d')
print('实际日期 ', timenow)
return timenow
def layout():
with open(r'./mryj.json', 'r') as f:
data = json.loads(f.read())
v = ui.View()
text = ui.TextView(font=('Didot', 15))
text.text = ' ' + data['note'] + '\n' + ' ' + data['content']
text.editable = False
text.selectable = False
v.add_subview(text)
appex.set_widget_view(text)
url = 'http://open.iciba.com/dsapi/'
filedate = get_filedate()
timenow = get_datetime()
if filedate == timenow:
print('时间相同,读取文件')
layout()
else:
print('时间不同,更新文件')
data = get_page(url)
write_file(data)
layout()