使用scriptable,在IOS桌面上展示,从notion中提取出的数据

使用scriptable,在IOS桌面上展示,从notion中提取出的数据,第1张

使用python,定时获取并推送微信读书的阅读进度到notion,其中部分字段如下,半小时获取,仅推送有进度时的数据记录:

使用scriptable app,选择script-When Interacting:Open App的方式,配置桌面组件。

效果区分白天模式和夜晚模式,大概如下:

脚本代码:



let widget = new ListWidget()
widget.setPadding(16, 16, 16, 16)

const spc = 3

// ShangHai
var LAT = 31.18
var LON = 121.43
const date = new Date()
const sunData = await new Request("http://api.sunrise-sunset.org/json?lat=" + LAT + "&lng=" + LON + "&formatted=0&date=" + date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate()).loadJSON();
let now = date.getTime()
let sunrise = new Date(sunData.results.sunrise).getTime()
let sunset = new Date(sunData.results.sunset).getTime()
let night = (now < sunrise) || (now > sunset)

//Title Text
let titleTxt = widget.addText("今日待阅")
titleTxt.font= Font.boldSystemFont(17)
titleTxt.leftAlignText()
widget.addSpacer(spc)

//Value Text
let vlFnt = Font.semiboldSystemFont(20)

//Untertitel Textstyle
let ptFnt = Font.systemFont(8)
let ptCol

//Hintergrund
if (night) {
  titleTxt.textColor = Color.lightGray()
  ptCol = Color.gray()
  const gradient = new LinearGradient()
  gradient.locations = [0, 1]
  gradient.colors = [
    new Color("19191b"),
    new Color("1f1f1b")
  ]
  widget.backgroundGradient = gradient
}
else {
  titleTxt.textColor = Color.darkGray()
  ptCol = Color.darkGray()
}

await loadCnt()

if (!config.runsInWidget) widget.presentSmall()
Script.setWidget(widget)
Script.complete()

async function loadCnt() {

var urlbookmark='https://api.notion.com/v1/databases/idxxxxxxx/query'
const headers = {
  Accept: "application/json",
  "Content-Type": "application/json",
  "Authorization": "secret_xxxxxxx",
  "Notion-Version":"2021-08-16"
}

 const bookjson=await post({'url':urlbookmark,'headers':headers})


 var nowchapter = bookjson.results[0].properties.当前章节.number;
 var totalchapterwords = bookjson.results[0].properties.当前字数纠正.formula.number;
 var chaptertile = bookjson.results[0].properties.章节名.title[0].plain_text;
 var twords = bookjson.results[0].properties.总字数.number;
 
 //当天0点
 var today_0_0_0_0 = new Date(new Date().setHours(0, 0, 0, 0));
 //周一0点
 var nowTime = today_0_0_0_0.getTime()
 var day = today_0_0_0_0.getDay() || 7  //为周日的时候 day 修改为7  否则当天周天会有问题
console.log(day)
 var oneDayTime = 24*60*60*1000 ;
 var MondayTime = nowTime - (day-1)*oneDayTime ;//显示周一
 //var SundayTime =  nowTime + (7-day)*oneDayTime ;//显示周日
 var Monday_0_0_0_0 = Date.parse(new Date(MondayTime)) 
 
 
 today_weekday = today_0_0_0_0.getDay()
 today_0_0_0_0 = Date.parse(today_0_0_0_0)
 var today_count = new Array()
 var weekday_count = new Array()

 for (i = 0; i < bookjson.results.length; i++) { 
 var created_time = bookjson.results[i].properties.记录时间.created_time
 //console.log(created_time)
 created_time = Date.parse(created_time)
 if (created_time >= today_0_0_0_0){
  today_count[i] = bookjson.results[i].properties.当前字数纠正.formula.number;
 }
 if (created_time >= Monday_0_0_0_0){
  weekday_count[i] = bookjson.results[i].properties.当前字数纠正.formula.number;
 }
 }
 /*按照每天3W字计算
 获取当天剩余字数;
 本周剩余字数
 当前进度差距
 取最大值展示
 */
 var today_wordscount_left = -22000
 var weekday_count_left = -22000*day
 if (today_count.length != 0){

 today_wordscount_left = Math.max(...today_count)-Math.min(...today_count) - 22000
 console.log(today_wordscount_left)
}
 if (weekday_count.length != 0){
 weekday_count_left = Math.max(...weekday_count)-Math.min(...weekday_count) - 22000*day
  console.log(weekday_count_left)
}
 
 var tempstep1 = bookjson.results[0].properties.进度差距.formula.number;
 
 var take_max_arr = [0,0,0]
 take_max_arr[0] = today_wordscount_left
 take_max_arr[1] = weekday_count_left
// 进度之前计算的是负值,需要取反
 take_max_arr[2] = -tempstep1

 take_max = Math.min(...take_max_arr)
 if (take_max > 0){
  take_max = 0
}
 
  // 总字数6125833
 var currentstep = ((totalchapterwords/twords)*100).toFixed(4)

//数字转字符串
 tempstep1 = String(tempstep1);
 take_max = String(take_max);
 nowchapter = String(nowchapter);
 totalchapterwords = String(totalchapterwords);
 currentstep = String(currentstep) + "%";
 
 console.log(nowchapter);
 console.log(totalchapterwords);
 console.log(currentstep)

 async function post(opts) {
    const request = new Request(opts.url)
    request.headers = {
   ...opts.headers,
   ...this.defaultHeaders
    }
    request.method = "POST";
    var result=await request.loadJSON()
    console.log(result)
    return result
  
 }



//**************************
  
  //Inzidenz Text
  if (take_max != null) {
    let tx2 = widget.addText(take_max)
      tx2.leftAlignText()
      tx2.font = vlFnt
      tx2.textColor = new Color("#FF0000")
  }
  //Inzidenz Untertitel
  if (chaptertile != null) {
   let tx1 = widget.addText(chaptertile)
   tx1.textColor = ptCol
   tx1.font= ptFnt
   tx1.leftAlignText()
   widget.addSpacer(spc)
  }
  //Hospitalisierung Text
  if (totalchapterwords != null) {
    let tx4 = widget.addText(totalchapterwords)
      tx4.leftAlignText()
      tx4.font = vlFnt
      tx4.textColor = new Color("#FF0000")
  }
  //Hospitalisierung Untertitel
  let tx3 = widget.addText("当前阅读字数")
  tx3.textColor = ptCol
  tx3.font= ptFnt
  tx3.leftAlignText()
  widget.addSpacer(spc)
  
  //Intensivbetten Text
  if (currentstep != null) {
    let tx6 = widget.addText(currentstep)
      tx6.leftAlignText()
      tx6.font = vlFnt
      tx6.textColor = new Color("#FF0000")
  }
  //Intensivbetten Untertitel
  let tx5 = widget.addText("当前阅读比例")
  tx5.textColor = ptCol
  tx5.font= ptFnt
  tx5.leftAlignText()
}

参考:

JS 根据今天的日期获取本周星期一与星期天的日期_zhooson的博客-CSDN博客_js获取本周一的日期var now = new Date(); var nowTime = now.getTime() ; var day = now.getDay();var oneDayTime = 24*60*60*1000 ; //显示周一var MondayTime = nowTime - (day-1)*oneDayTime ; //显示周日var SundayTime =https://blog.csdn.net/zhooson/article/details/54861822

Widget Examples - #617 by eqsOne - Scriptable - Automators TalkHi everyone, I’m trying to gather a few examples of scripts that work with the new widgets in iOS 14, so people have a place to find inspiration for their own widgets. I would love if you would share your scripts that w…https://talk.automators.fm/t/widget-examples/7994/617

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/990632.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存