Last active
May 31, 2022 23:11
-
-
Save breskeby/1fe9c19f7e722a4634694c26071be5fb to your computer and use it in GitHub Desktop.
MyLeo Daily WOD Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let widget = await createWidget(); | |
// Check where the script is running | |
if (config.runsInWidget) { | |
// Runs inside a widget so add it to the homescreen widget | |
Script.setWidget(widget); | |
} else { | |
// Show the large widget inside the app | |
widget.presentLarge(); | |
} | |
Script.complete(); | |
async function createWidget() { | |
// Fetch next launch date | |
let response = await getRequestData(); | |
let item = resolveDataFromRss(response); | |
return displayWod(item); | |
} | |
async function getRequestData() { | |
// Query url | |
const url = "https://myleo.de/wods.rss"; | |
// Initialize new request | |
const request = new Request(url); | |
// Execute the request and parse the response as String | |
const response = await request.loadString(); | |
// Return the returned wod rss data | |
return response; | |
} | |
function resolveDataFromRss(launchData) { | |
let wodDescription = null | |
const xmlParser = new XMLParser(launchData) | |
let items = [] | |
let currentValue = null | |
let currentItem = null | |
let todaysItem = null | |
xmlParser.didStartElement = name => { | |
currentValue = "" | |
if (name == "item") { | |
currentItem = {} | |
} | |
} | |
xmlParser.didEndElement = name => { | |
const hasItem = currentItem != null | |
if (hasItem && name == "title") { | |
currentItem["title"] = currentValue | |
} | |
if (hasItem && name == "description") { | |
currentItem["description"] = currentValue | |
} | |
if (hasItem && name == "guid") { | |
currentItem["guid"] = currentValue | |
} | |
if (name == "item") { | |
items.push(currentItem) | |
currentItem = {} | |
} | |
} | |
xmlParser.foundCharacters = str => { | |
currentValue += str | |
} | |
xmlParser.didEndDocument = () => { | |
items.sort(function(a, b){return b["guid"] - a["guid"]}) | |
} | |
xmlParser.parse() | |
return items[0]; | |
} | |
function displayWod(item) { | |
// Create new empty ListWidget instance | |
let listWidget = new ListWidget(); | |
// Set new background color | |
listWidget.backgroundColor = new Color("#62203a"); | |
let heading = listWidget.addText("Myleo " + item["title"]); | |
heading.leftAlignText(); | |
heading.font = Font.lightSystemFont(25); | |
heading.textColor = new Color("#ffffff"); | |
// Spacer between heading and launch date | |
listWidget.addSpacer(15); | |
addWodText(listWidget, item['description']); | |
return listWidget; | |
} | |
function addWodText(stack, text) { | |
let dateText = stack.addText(text); | |
dateText.leftAlignText(); | |
dateText.font = Font.boldSystemFont(10); | |
dateText.textColor = new Color("#ffffff"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment