MCPcopy Index your code
hub / github.com/ajatkj/scriptable / fetchQuotes

Function fetchQuotes

LSWeather.js:901–933  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

899FUNCTION fetchQuotes
900------------------------------------------------------------------*/
901async function fetchQuotes(){
902 if (!QUOTE_SHOW_QUOTES) return unknownQuotesData;
903
904 if (TESTING) return unknownQuotesData;
905
906 let response;
907
908 const quotesURL = baseQuotesURL + '?maxLength=' + QUOTE_MAX_LENGTH + '&tags=' + QUOTE_TAGS.join('%7C'); // %7C is code for |
909
910 try {
911 writeLOG(`Fetching quotes at url: ${quotesURL}`);
912 const request = new Request(quotesURL);
913 request.timeoutInterval = 30;
914 response = await request.loadJSON();
915 } catch (error) {
916 writeLOG(`Couldn't fetch quotes from ${quotesURL}`);
917 return unknownQuotesData;
918 }
919 writeLOG("JSON Quotes Data: " + JSON.stringify(response));
920 let quoteWithAuthor = [];
921 // Wrap text
922 let wrapText = (s, w) => s.replace(
923 new RegExp(`(?![^\\n]{1,${w}}$)([^\\n]{1,${w}})\\s`, 'g'), ' $1| '
924 );
925 quoteWithAuthor = wrapText(response.content,QUOTE_WRAP_LENGTH).split('|');
926 quoteWithAuthor.push({rsize: DETAIL_RSIZE, title: "[" + response.author + "]"});
927 return {
928 quote: response.content,
929 author: response.author,
930 quoteWithAuthor: quoteWithAuthor,
931 length: response.length
932 }
933}
934/*------------------------------------------------------------------
935FUNCTION fetchCalendar
936------------------------------------------------------------------*/

Callers 1

LSWeather.jsFile · 0.70

Calls 2

writeLOGFunction · 0.70
wrapTextFunction · 0.70

Tested by

no test coverage detected