Skip to content

Instantly share code, notes, and snippets.

@0507spc
Last active October 8, 2020 08:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0507spc/cb762e62e5a2564ac009cc502b1d1787 to your computer and use it in GitHub Desktop.
Save 0507spc/cb762e62e5a2564ac009cc502b1d1787 to your computer and use it in GitHub Desktop.
// Shows latest news from MacStories in a table.
// The table shows an image from the article, if available, and the title of the article.
let inputStr = args.widgetParameter
// if exclude itrms, this meeds to be higger than vLimit
// logError(inputStr)
if ( inputStr == null ) { inputStr = "apollo@scriptable@1@pic@4" }
let vRedditLimit = 15
let vItemCount = 1
var res = inputStr.split("@")
let rReddit = res[1]
let vLimit = res[4]
let baseURL = "www.reddit.com"
let url = "https://" + baseURL + "/r/" + rReddit + "/new.json?limit=" + vRedditLimit + "&show=all"
let req = new Request(url)
let json = await req.loadJSON()
let items = json.data.children
let appURL = res[0] + "://" + baseURL + rReddit
let simpleEmoji = new Font("Menlo-Regular", 10)
let mainText = new Font("GillSans-SemiBold", 14)
let titleTextFont = new Font("GillSans-SemiBold", 14)
// set widget
let w = new ListWidget()
w.setPadding(10, 10, 10, 10)
w.spacing = 1.5
let titleStack = w.addStack()
// titleStack.cornerRadius = 4
titleStack.setPadding(2, 100, 2, 100)
// titleStack.setPadding(top, leading, bottom, trailing)
// titleStack.backgroundColor = new Color("#33ff00", 0.2)
vTitle = "r/" + rReddit
let vLength = vTitle.length
const vMaxSub = 50 // This is for the simpleEmoji font - needs to calculate
let vTitlePadding = ((vMaxSub - vLength) / 2) * 6
titleStack.setPadding(0, vTitlePadding, 0, 0)
let wtitle = titleStack.addText(vTitle)
wtitle.font = titleTextFont
wtitle.textColor = new Color("#33ff00")
function addLine(main, link, r, col, alignMe, txtFont) {
let stack = r.addStack()
// stack.layoutVertically()
stack.layoutHorizontally()
stack.url = link
switch(alignMe) {
case "left":
stack.setPadding(0, 0, 0, 0)
break;
case "right":
let vLength = main.length
const vMaxSub = 50 // This is for the simpleEmoji font - needs to calculate
let vPadding = (vMaxSub - vLength) * 6
stack.setPadding(0, vPadding, 0, 0)
break;
case "none":
stack.setPadding(0, 0, 0, 0)
break;
default:
// code block
}
let wMain = stack.addText(main)
wMain.font = txtFont
wMain.textColor = col
}
let newLineStack = []
for (item of items) {
if (! item.data.title.includes("Questions Thread ") && vItemCount <= vLimit ) {
vItemCount = vItemCount + 1
var myDate = new Date(item.data.created_utc * 1000)
var myFormDate = addZero(myDate.getHours()) + ":" + addZero(myDate.getMinutes())
let linkToPost = res[0] + "://" + baseURL + item.data.permalink
let linkToUser = res[0] + "://" + baseURL + "/user/" + item.data.author
// log(linkToPost)
let titleTxt = "• " + item.data.title
let subTitle = "✾ " + item.data.author + " ↑ " + ("0000" + item.data.score).slice (-3) + " ✐ " + ("0000" + item.data.num_comments).slice (-3) + " ◔ " + myFormDate
newLineStack[vItemCount + "_1"] = w.addStack()
addLine(titleTxt,linkToPost, newLineStack[vItemCount + "_1"], Color.white(), "none", mainText)
newLineStack[vItemCount + "_2"] = w.addStack()
addLine(subTitle,linkToUser, newLineStack[vItemCount + "_2"], Color.gray(), "right", simpleEmoji)
}
}
w.presentMedium()
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment