aa-daily-reflections
Version:
A lightweight Node.js library to fetch Daily Reflections from Alcoholics Anonymous (AA)
78 lines (77 loc) • 3.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReflectionParser = void 0;
const he_1 = require("he");
const text_1 = require("../utils/text");
class ReflectionParser {
parse(response) {
if (response.err !== 200) {
throw new Error(`API returned error: ${response.err}`);
}
const html = (0, he_1.decode)(response.data);
return {
title: this.extractTitle(html),
date: this.extractDate(html),
day: this.extractDay(html),
month: this.extractMonth(html),
monthName: this.extractMonthName(html),
quote: this.extractQuote(html),
reference: this.extractReference(html),
reflection: this.extractReflection(html),
copyright: this.extractCopyright(html)
};
}
extractTitle(html) {
const match = html.match(/<span class="field field--name-title[^>]*>([^<]+)<\/span>/);
return match ? (0, text_1.cleanText)(match[1]) : '';
}
extractDate(html) {
const dateMatch = html.match(/data-date="([^"]+)"/);
return dateMatch ? dateMatch[1] : '';
}
extractDay(html) {
const match = html.match(/<input[^>]*name="day"[^>]*value="([^"]+)"/);
return match ? parseInt(match[1], 10) : 0;
}
extractMonth(html) {
const match = html.match(/<input[^>]*name="month"[^>]*value="([^"]+)"/);
return match ? parseInt(match[1], 10) : 0;
}
extractMonthName(html) {
const match = html.match(/<input[^>]*name="month-name"[^>]*value="([^"]+)"/);
return match ? match[1] : '';
}
extractQuote(html) {
const match = html.match(/<div class="clearfix text-formatted field field--name-field-teaser[^>]*>(.*?)<\/div>/s);
if (!match)
return '';
const content = match[1];
const strongMatch = content.match(/<strong>(.*?)<\/strong>/s);
return strongMatch ? (0, text_1.cleanText)(strongMatch[1]) : '';
}
extractReference(html) {
const match = html.match(/<div class="clearfix text-formatted field field--name-field-teaser[^>]*>(.*?)<\/div>/s);
if (!match)
return '';
const content = match[1];
const strongMatches = content.match(/<strong>(.*?)<\/strong>/gs);
if (strongMatches && strongMatches.length > 1) {
return (0, text_1.cleanText)(strongMatches[1].replace(/<\/?strong>/g, ''));
}
return '';
}
extractReflection(html) {
const match = html.match(/<div class="clearfix text-formatted field field--name-body[^>]*>(.*?)<\/div>/s);
if (!match)
return '';
let content = match[1];
content = content.replace(/<p><strong>.*?<\/strong><\/p>\s*<p><strong>.*?<\/strong><\/p>/s, '');
const reflectionMatch = content.match(/<p>([^<].*?)<\/p>/s);
return reflectionMatch ? (0, text_1.cleanText)(reflectionMatch[1]) : '';
}
extractCopyright(html) {
const match = html.match(/<div class="copyright-block"[^>]*>.*?<p>(.*?)<\/p>/s);
return match ? (0, text_1.cleanText)(match[1]) : '';
}
}
exports.ReflectionParser = ReflectionParser;