-
Notifications
You must be signed in to change notification settings - Fork 0
/
bibles.js
164 lines (143 loc) · 5.8 KB
/
bibles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
// Assuming JSON files are in a 'Files/Alkitab' directory
const versionsPath = path.join(__dirname, 'data', 'versions.json');
const booksPath = path.join(__dirname, 'data', 'books.json');
const chaptersPath = path.join(__dirname, 'data', 'chapters.json');
// Load JSON data with error handling
let versions, books, chapters;
try {
versions = JSON.parse(fs.readFileSync(versionsPath, 'utf8'));
books = JSON.parse(fs.readFileSync(booksPath, 'utf8'));
chapters = JSON.parse(fs.readFileSync(chaptersPath, 'utf8'));
} catch (error) {
console.error('Error loading JSON files:', error);
app.use((req, res) => {
res.status(500).json({ message: 'Failed to load data' });
});
return;
}
// Helper function to find the book code
const getBookCode = (bookName) => {
for (let book of books) {
if (
book.name.toLowerCase() === bookName.toLowerCase() ||
book.code.toLowerCase() === bookName.toLowerCase() ||
book.code_en.toLowerCase() === bookName.toLowerCase() ||
book.name_en.toLowerCase() === bookName.toLowerCase()
) {
return book.code;
}
}
return null;
};
// 0. API Documentation
app.get('/api/alkitab/v1/docs', (req, res) => {
res.json({
message: 'Welcome to the Alkitab API!',
description: 'This API provides access to Bible data, including versions, books, chapters, and verses.',
endpoints: {
'/api/alkitab/v1/versions': {
title: 'Get Available Bible Versions',
method: 'GET',
description: 'Fetch a list of available Bible versions.',
sample_request: 'GET /api/alkitab/v1/versions',
sample_response: [{ code: 'tb', name: 'Terjemahan Baru' }, { code: 'ayt', name: 'Alkitab Yang Terbuka (AYT)' }],
error: false
},
'/api/alkitab/v1/books': {
title: 'Get List of Books',
method: 'GET',
description: 'Fetch a list of books in the Bible.',
sample_request: 'GET /api/alkitab/v1/books',
sample_response: [{ name: 'Genesis', code: 'gen' }, { name: 'Exodus', code: 'exod' }],
error: false
},
'/api/alkitab/v1/{bookName}/chapter': {
title: 'Get Chapters for Book',
method: 'GET',
description: 'Fetch a list of chapter numbers for a specific book.',
parameters: { bookName: 'The name of the book (e.g., "gen" for Genesis)' },
sample_request: 'GET /api/alkitab/v1/gen/chapter',
sample_response: [1, 2, 3],
error: false
},
'/api/alkitab/v1/{version}/{bookName}/{chapter}/{verse}': {
title: 'Get Verse Text',
method: 'GET',
description: 'Fetch the text of a specific verse from a specific version.',
parameters: {
version: 'The Bible version code (e.g., "tb")',
bookName: 'The name of the book (e.g., "gen")',
chapter: 'The chapter number',
verse: 'The verse number'
},
sample_request: 'GET /api/alkitab/v1/tb/gen/1/1',
sample_response: { verse: 'In the beginning, God created the heavens and the earth.' },
error: false
}
}
});
});
// 1. Get Versions
app.get('/api/alkitab/v1/versions', (req, res) => {
res.json(versions);
});
// 2. Get Books
app.get('/api/alkitab/v1/books', (req, res) => {
const bookNames = books.map(book => ({
name: book.name,
name_en: book.name_en,
code: book.code,
code_en: book.code_en
}));
res.json(bookNames);
});
// 3. Get Chapters for a Book
app.get('/api/alkitab/v1/:bookName/chapter', (req, res) => {
const { bookName } = req.params;
// Find the correct book code
const bookCode = getBookCode(bookName);
if (!bookCode) {
return res.status(404).json({ error: 'Book not found' });
}
if (!chapters[bookCode]) {
return res.status(404).json({ error: 'Chapters not found for this book' });
}
const chaptersList = chapters[bookCode].map(chapter => ({ chapter }));
res.json(chaptersList);
});
// 4. Get Verses from a Chapter
app.get('/api/alkitab/v1/:version/:bookName/:chapter/:startVerse/:endVerse?', (req, res) => {
const { version, bookName, chapter, startVerse, endVerse } = req.params;
// Ensure startVerse and endVerse are integers
if (!version || !bookName || !chapter || !startVerse) {
return res.status(400).json({ error: 'Invalid parameters' });
}
const bookCode = getBookCode(bookName);
if (!bookCode) {
return res.status(404).json({ error: 'Book not found' });
}
const filePath = path.join(__dirname, 'Files', 'Alkitab', 'Verses', version, bookCode, `${chapter}.json`);
if (!fs.existsSync(filePath)) {
return res.status(404).json({ error: 'Chapter not found' });
}
const versesData = JSON.parse(fs.readFileSync(filePath, 'utf8'));
// Default to single verse if endVerse is not provided
const end = endVerse || startVerse;
const selectedVerses = versesData.verses.filter(verse => verse.verse_number >= startVerse && verse.verse_number <= end);
if (selectedVerses.length === 0) {
return res.status(404).json({ error: 'No verses found' });
}
res.json(selectedVerses.map(verse => ({
verse_number: verse.verse_number,
verse_text: verse.verse_text
})));
});
// Server setup
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});