Action

Daily Readings

Posted by cowpi, Last update over 6 years ago

This action accesses the EWTN website for references for the Daily Readings for Mass for the selected date and parses the JSON data returned. The readings are formatted in Markdown and appended to the text in the Drafts’ editor.

(Employs several JavaScript techniques: Drafts’ date picker, HTTP request, JSON parse, and simple date formatting.)

Steps

  • script

    // This script accesses the EWTN website for references for the
    // Daily Readings for the selected date and parses the JSON data
    // returned. The readings are appended to the text in the editor.
    
    
    function ymdFormat(jsDate) {
    	// Formats date as yyyy-mm-dd
    	var m = jsDate.getMonth() + 1;
    	m = (m > 9) ? m : '0' + m;
    	var d = jsDate.getDate();
    	d = (d > 9) ? d : '0' + d;
    	return jsDate.getFullYear() + '-' + m + '-' + d;
    }
    
    function niceDateFormat(jsDate) {
    	// Formats date as day, mmm d
    	var mo = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    	var dow = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
    	// do not include day if Sunday
    	var day = (jsDate.getDay() > 0) ? (dow[jsDate.getDay()] + ', ') : '';
    	return day + mo[jsDate.getMonth()] + ' ' + jsDate.getDate();
    }
    
    
    // Date picker
    var p = Prompt.create();
    p.title = 'Daily Readings';
    p.addDatePicker('pickDate', 'Select date for readings…', new Date(), {"mode": "date"});
    p.addButton('Select');
    var didSelect = p.show();
    
    if (didSelect) {
    	// format URL
    	var ymd = ymdFormat(p.fieldValues['pickDate']);
    	var targetUrl = 'https://www.ewtn.com/se/readings/readingsservice.svc/day/' + ymd + '/en';
    
    	// HTTP Request
    	var http = HTTP.create();
    	var response = http.request({"url": targetUrl, "method": "GET"});
    
    	if (!response.success) {
    		// Error in retrieving data
    		var txt = response.statusCode + ' - ' + response.error;
    	} else if (response.responseText.length < 1) {
    		// no data
    		var txt = 'data unavailable for '+ ymd;
    	} else {
    		// Parse and format readings
    		var liturgy = JSON.parse(response.responseText);
    		
    		var txt = niceDateFormat(p.fieldValues['pickDate']);
    		txt += ' - ' + liturgy.Title + '\n';
    		
    		var numReadings = liturgy.ReadingGroups[0].Readings.length;
    
    		for (var i = 0; i < numReadings; i++) {
    			var ref = liturgy.ReadingGroups[0].Readings[i].Citations[0].Reference;
    			var type = liturgy.ReadingGroups[0].Readings[i].Type;
    			txt += '**' + type + ':** ' + ref + '\n';
    		}
    		//txt += liturgy.Color + ', ' + liturgy.Note + '\n';
    	}
    
    	// insert text into editor
    	var len = editor.getText().length;
    	var prefix = (len == 0) ? '' : '\n';  // beginning or append
    	editor.setTextInRange(len, 0, prefix + txt);
    }
    
    editor.activate();
    
    

Options

  • After Success Nothing
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.