Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(module): add support for German and Ukrainian languages #438

Merged
merged 4 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { loadEPUB } from 'jw-epub-parser/dist/node/index.js';
const epubJW = await loadEPUB('/path/to/file.epub');

const epubJW = await loadEPUB({ url: epubUrl });

```

### loadEPUB(epubData)
Expand Down Expand Up @@ -99,9 +98,11 @@ Currently, we only support enhanced parsing for the following languages:
```bash
Enlish
French
German
Malagasy
Portuguese Brazil
Tandroy, Tankarana
Ukrainian
Vezo
```

Expand Down
34 changes: 34 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { fetchData } from './sample.js';

const runLiveCommand = async () => {
const languageIndex = process.argv.indexOf('--language');
if (languageIndex === -1) {
console.error('language missing from arguments');
return;
}

const issueIndex = process.argv.indexOf('--issue');
const pubIndex = process.argv.indexOf('--pub');

if (issueIndex >= 0 && pubIndex === -1) {
console.error('issue date was provided but pub type is missing');
return;
}

if (pubIndex >= 0 && issueIndex === -1) {
console.error('pub type was provided but issue date is missing');
return;
}

const language = process.argv[languageIndex + 1];
const issue = issueIndex >= 0 ? process.argv[issueIndex + 1] : undefined;
const pub = pubIndex >= 0 ? process.argv[pubIndex + 1] : undefined;

console.time();
const data = await fetchData(language, issue, pub);

console.log(data);
console.timeEnd();
};

runLiveCommand();
225 changes: 225 additions & 0 deletions example/sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
import { loadEPUB } from '../src/node/index.js';

const JW_CDN = 'https://app.jw-cdn.org/apis/pub-media/GETPUBMEDIALINKS?';
const JW_FINDER = 'https://www.jw.org/finder?';
const WOL_CDN = 'https://b.jw-cdn.org/apis/wol-link/';
const WOL_E = 'https://wol.jw.org/wol/dt/r1/lp-e';

const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];

const fetchIssueData = async (issue, pub) => {
try {
if (issue.hasEPUB) {
const epubFile = issue.hasEPUB[0].file;
const epubUrl = epubFile.url;

const epubData = await loadEPUB({ url: epubUrl });
return epubData;
}

if (!issue.hasEPUB) {
const language = issue.language;

const parser = new window.DOMParser();

if (pub === 'mwb') {
const url =
JW_FINDER +
new URLSearchParams({
wtlocale: language,
pub,
issue: issue.issueDate,
});

const res = await fetch(url);
const result = await res.text();

const htmlItem = parser.parseFromString(result, 'text/html');

const docIds = [];
const accordionItems = htmlItem.getElementsByClassName(`docClass-106 iss-${issue.issueDate}`);
for (const weekLink of accordionItems) {
weekLink.classList.forEach((item) => {
if (item.indexOf('docId-') !== -1) {
docIds.push(item.split('-')[1]);
}
});
}

const htmlRaws = [];

const fetchSchedule1 = fetch(`https://www.jw.org/finder?wtlocale=${language}&docid=${docIds[0]}`).then((res) =>
res.text()
);
const fetchSchedule2 = fetch(`https://www.jw.org/finder?wtlocale=${language}&docid=${docIds[1]}`).then((res) =>
res.text()
);
const fetchSchedule3 = fetch(`https://www.jw.org/finder?wtlocale=${language}&docid=${docIds[2]}`).then((res) =>
res.text()
);
const fetchSchedule4 = fetch(`https://www.jw.org/finder?wtlocale=${language}&docid=${docIds[3]}`).then((res) =>
res.text()
);
const fetchSchedule5 = fetch(`https://www.jw.org/finder?wtlocale=${language}&docid=${docIds[4]}`).then((res) =>
res.text()
);
const fetchSchedule6 = fetch(`https://www.jw.org/finder?wtlocale=${language}&docid=${docIds[5]}`).then((res) =>
res.text()
);
const fetchSchedule7 = fetch(`https://www.jw.org/finder?wtlocale=${language}&docid=${docIds[6]}`).then((res) =>
res.text()
);
const fetchSchedule8 = docIds[7]
? fetch(`https://www.jw.org/finder?wtlocale=${language}&docid=${docIds[7]}`).then((res) => res.text())
: Promise.resolve('');
const fetchSchedule9 = docIds[8]
? fetch(`https://www.jw.org/finder?wtlocale=${language}&docid=${docIds[8]}`).then((res) => res.text())
: Promise.resolve('');
const fetchSchedule10 = docIds[9]
? fetch(`https://www.jw.org/finder?wtlocale=${language}&docid=${docIds[9]}`).then((res) => res.text())
: Promise.resolve('');

const raws = await Promise.all([
fetchSchedule1,
fetchSchedule2,
fetchSchedule3,
fetchSchedule4,
fetchSchedule5,
fetchSchedule6,
fetchSchedule7,
fetchSchedule8,
fetchSchedule9,
fetchSchedule10,
]);

for (let z = 0; z < raws.length; z++) {
const rawText = raws[z];
if (rawText !== '') {
htmlRaws.push(rawText);
}
}

const epubData = await loadEPUB({ htmlRaws, epubYear: issue.currentYear, epubLang: language, isMWB: true });
return epubData;
}
}
} catch (err) {
throw new Error(err);
}
};

export const fetchData = async (language, issue, pub) => {
let data = [];

if (!issue && !pub) {
for await (const pub of ['mwb']) {
const issues = [];

if (pub === 'mwb') {
let notFound = false;

// get current issue
const today = new Date();
const day = today.getDay();
const diff = today.getDate() - day + (day === 0 ? -6 : 1);
const weekDate = new Date(today.setDate(diff));
const validDate = weekDate.setMonth(weekDate.getMonth());

const startDate = new Date(validDate);
const currentMonth = startDate.getMonth() + 1;
const monthOdd = currentMonth % 2 === 0 ? false : true;
let monthMwb = monthOdd ? currentMonth : currentMonth - 1;
let currentYear = startDate.getFullYear();

do {
const issueDate = currentYear + String(monthMwb).padStart(2, '0');
const url =
JW_CDN +
new URLSearchParams({
langwritten: language,
pub: 'mwb',
output: 'json',
issue: issueDate,
});

const res = await fetch(url);

if (res.status === 200) {
const result = await res.json();
const hasEPUB = result.files[language].EPUB;

issues.push({ issueDate, currentYear, language, hasEPUB: hasEPUB });
}

if (res.status === 404) {
notFound = true;
}

// assigning next issue
monthMwb = monthMwb + 2;
if (monthMwb === 13) {
monthMwb = 1;
currentYear++;
}
} while (notFound === false);
}

if (issues.length > 0) {
const fetchSource1 = fetchIssueData(issues[0], pub);
const fetchSource2 = issues.length > 1 ? fetchIssueData(issues[1], pub) : Promise.resolve(undefined);
const fetchSource3 = issues.length > 2 ? fetchIssueData(issues[2], pub) : Promise.resolve(undefined);
const fetchSource4 = issues.length > 3 ? fetchIssueData(issues[3], pub) : Promise.resolve(undefined);
const fetchSource5 = issues.length > 4 ? fetchIssueData(issues[4], pub) : Promise.resolve(undefined);
const fetchSource6 = issues.length > 5 ? fetchIssueData(issues[5], pub) : Promise.resolve(undefined);
const fetchSource7 = issues.length > 6 ? fetchIssueData(issues[6], pub) : Promise.resolve(undefined);

const allData = await Promise.all([
fetchSource1,
fetchSource2,
fetchSource3,
fetchSource4,
fetchSource5,
fetchSource6,
fetchSource7,
]);

for (let z = 0; z < allData.length; z++) {
const tempObj = allData[z];
if (tempObj) {
data.push(tempObj);
}
}
}
}
}

if (issue && pub) {
const url = JW_CDN + new URLSearchParams({ langwritten: language, pub, output: 'json', issue });

const res = await fetch(url);

if (res.status === 200) {
const result = await res.json();
const hasEPUB = result.files[language].EPUB;

const issueFetch = { issueDate: issue, currentYear: issue.substring(0, 4), language, hasEPUB: hasEPUB };

data = await fetchIssueData(issueFetch, pub);
}
}

return data;
};
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
"version": "1.40.1",
"type": "module",
"description": "This tool will help you to parse and extract the needed source materials from Meeting Workbook EPUB file. Support for parsing Watchtower Study will be added in future release.",
"keywords": ["epub parse", "jw", "jw.org", "epub"],
"keywords": [
"epub parse",
"jw",
"jw.org",
"epub"
],
"main": "./dist/index.js",
"module": "./dist/index.js",
"files": ["dist/*"],
"files": [
"dist/*"
],
"homepage": "https://github.com/sws2apps/jw-epub-parser#readme",
"author": "Scheduling Workbox System <sws2apps.notification@gmail.com>",
"bugs": {
Expand All @@ -20,11 +27,12 @@
},
"funding": {
"type": "buymeacoffee",
"url": "https://www.buymeacoffee.com/sws2apps"
"url": "https://www.buymeacoffee.com/sws2apps/e/146062"
},
"scripts": {
"test": "mocha",
"build": "npx rimraf dist && npx rollup -c"
"build": "npx rimraf dist && npx rollup -c",
"parse": "node example/index.js"
},
"devDependencies": {
"@babel/preset-env": "^7.16.11",
Expand Down
4 changes: 4 additions & 0 deletions src/browser/languageRules.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import source from '../locales/en/text.json';
import F from '../locales/fr-FR/text.json';
import K from '../locales/uk-UA/text.json';
import MG from '../locales/mg-MG/text.json';
import T from '../locales/pt-BR/text.json';
import TND from '../locales/mg-TND/text.json';
import TNK from '../locales/mg-TNK/text.json';
import VZ from '../locales/mg-VZ/text.json';
import X from '../locales/de-DE/text.json';

const dataLang = {};

for (const [key, value] of Object.entries(source)) {
dataLang[key] = {
E: value,
F: F[key],
K: K[key],
MG: MG[key],
T: T[key],
TND: TND[key],
TNK: TNK[key],
VZ: VZ[key],
X: X[key],
};
}

Expand Down
10 changes: 5 additions & 5 deletions src/locales/de-DE/text.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"octoberVariations": "Oktober",
"novemberVariations": "November",
"decemberVariations": "Dezember",
"tgwTalk10Variations": "{{ title }}: (10 Min.)",
"tgwBibleReadingVariations": "Bibellesung: (4 Min.) {{ source }} (th Lektion {{ study }})",
"tgwTalk10Variations": "{{ title }} (10 Min.)",
"tgwBibleReadingVariations": "Bibellesung (4 Min.): {{ source }} (th Lektion {{ study }})",
"initialCallVideoVariations": "Erstes Gespräch (Video)",
"returnVisitVideoVariations": "Rückbesuch (Video)",
"memorialInvitationVideoVariations": "Einladung zum Gedächtnismahl (Video)",
Expand All @@ -21,7 +21,7 @@
"bibleStudyVariations": "Bibelstudium",
"talkVariations": "Vortrag",
"memorialInvitationVariations": "Einladung zum Gedächtnismahl",
"assignmentAyfVariations": "{{ assignment }}: ({{ duration }} Min.) {{ source }} (th Lektion {{ study }})|{{ assignment }}: ({{ duration }} Min.) {{ source }}",
"assignmentLcVariations": "{{ source }}: ({{ duration }} Min.) {{ content }}",
"cbsVariations": "Versammlungs­bibelstudium: (30 Min.) {{ source }}"
"assignmentAyfVariations": "{{ assignment }} ({{ duration }} Min.): {{ source }} (th Lektion {{ study }})|{{ assignment }} ({{ duration }} Min.): {{ source }}",
"assignmentLcVariations": "{{ source }} ({{ duration }} Min.)|{{ source }} ({{ duration }} Min.): {{ content }}",
"cbsVariations": "Versammlungs­bibelstudium (30 Min.): {{ source }}"
}
2 changes: 1 addition & 1 deletion src/locales/en/text.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
"talkVariations": "Talk",
"memorialInvitationVariations": "Memorial Invitation",
"assignmentAyfVariations": "{{ assignment }}: ({{ duration }} min.) {{ source }} (th study {{ study }})|{{ assignment }}: ({{ duration }} min.) {{ source }}",
"assignmentLcVariations": "{{ source }}: ({{ duration }} min.) {{ content }}",
"assignmentLcVariations": "{{ source }}: ({{ duration }} min.)|{{ source }}: ({{ duration }} min.) {{ content }}",
"cbsVariations": "Congregation Bible Study: (30 min.) {{ source }}"
}
2 changes: 2 additions & 0 deletions src/locales/languages.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export default [
{ locale: 'en', code: 'E' },
{ locale: 'fr-FR', code: 'F' },
{ locale: 'uk-UA', code: 'K' },
{ locale: 'mg-MG', code: 'MG' },
{ locale: 'pt-BR', code: 'T' },
{ locale: 'mg-TND', code: 'TND' },
{ locale: 'mg-TNK', code: 'TNK' },
{ locale: 'mg-VZ', code: 'VZ' },
{ locale: 'de-DE', code: 'X' },
];
Loading