salat 2.0.2 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/app.js +12 -5
- package/constants.js +2 -3
- package/data/prayers.json +18 -18
- package/package.json +10 -10
- package/utils.js +37 -35
package/README.md
CHANGED
package/app.js
CHANGED
|
@@ -8,12 +8,12 @@ const {
|
|
|
8
8
|
getCityName,
|
|
9
9
|
displayResult,
|
|
10
10
|
getData,
|
|
11
|
-
parsePrayerTimesFromResponse
|
|
11
|
+
parsePrayerTimesFromResponse,
|
|
12
12
|
} = require("./utils.js");
|
|
13
|
-
process.env.NODE_TLS_REJECT_UNAUTHORIZED="0"
|
|
13
|
+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
|
|
14
14
|
|
|
15
15
|
// Logging functioin
|
|
16
|
-
const green = msg => console.log(chalk.green(msg));
|
|
16
|
+
const green = (msg) => console.log(chalk.green(msg));
|
|
17
17
|
|
|
18
18
|
// Project's data
|
|
19
19
|
const { BANNER, LOCAL_STORAGE_PATH } = require("./constants");
|
|
@@ -31,7 +31,13 @@ const main = async () => {
|
|
|
31
31
|
green(BANNER);
|
|
32
32
|
|
|
33
33
|
const storageKey = `${cityName.toLowerCase()}_${new Date().toLocaleDateString()}`;
|
|
34
|
-
|
|
34
|
+
let item = localStorage.getItem(storageKey);
|
|
35
|
+
|
|
36
|
+
// Disable localStorage for local development
|
|
37
|
+
if (process.env.NODE_ENV === "development") {
|
|
38
|
+
console.log("development mode: localStorage is disabled");
|
|
39
|
+
item = null;
|
|
40
|
+
}
|
|
35
41
|
let prayers;
|
|
36
42
|
|
|
37
43
|
if (item) {
|
|
@@ -43,12 +49,13 @@ const main = async () => {
|
|
|
43
49
|
} catch (ex) {
|
|
44
50
|
//TODO: Use a more descriptif error message
|
|
45
51
|
console.error("Something went wrong!");
|
|
46
|
-
console.log(ex)
|
|
52
|
+
console.log(ex);
|
|
47
53
|
// console.log(ex);
|
|
48
54
|
return;
|
|
49
55
|
}
|
|
50
56
|
}
|
|
51
57
|
|
|
58
|
+
console.clear();
|
|
52
59
|
displayResult(prayers, cityName);
|
|
53
60
|
};
|
|
54
61
|
|
package/constants.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
const API_URL =
|
|
2
|
-
"http://www.habous.gov.ma/prieres/horaire-api.php";
|
|
1
|
+
const API_URL = "http://www.habous.gov.ma/prieres/horaire-api.php";
|
|
3
2
|
|
|
4
3
|
const BANNER = ``;
|
|
5
4
|
|
|
@@ -15,5 +14,5 @@ module.exports = {
|
|
|
15
14
|
BANNER,
|
|
16
15
|
NOT_FOUND_ERROR,
|
|
17
16
|
DEFAULT_CITY: "Marrakech",
|
|
18
|
-
LOCAL_STORAGE_PATH: "./storage"
|
|
17
|
+
LOCAL_STORAGE_PATH: "./storage",
|
|
19
18
|
};
|
package/data/prayers.json
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
[
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
2
|
+
{
|
|
3
|
+
"prayer": "Fajr"
|
|
4
|
+
},
|
|
5
|
+
{
|
|
6
|
+
"prayer": "Chorouq"
|
|
7
|
+
},
|
|
8
|
+
{
|
|
9
|
+
"prayer": "Dhuhr"
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"prayer": "Asr"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"prayer": "Maghrib"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"prayer": "Ishae"
|
|
19
|
+
}
|
|
20
20
|
]
|
package/package.json
CHANGED
|
@@ -1,23 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "salat",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Daily Moroccan prayers time, right in your console, at the tip of your fingers",
|
|
5
|
-
"homepage": "https://github.com/
|
|
5
|
+
"homepage": "https://github.com/kafiln/salat-cli",
|
|
6
6
|
"main": "app.js",
|
|
7
7
|
"bin": {
|
|
8
8
|
"salat": "app.js"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"axios": ">=0.19.0",
|
|
12
11
|
"chalk": "^2.4.2",
|
|
13
|
-
"jsdom": "^
|
|
12
|
+
"jsdom": "^24.0.0",
|
|
13
|
+
"node-fetch": "^2.7.0",
|
|
14
14
|
"node-localstorage": "^1.3.1"
|
|
15
15
|
},
|
|
16
|
-
"devDependencies": {
|
|
17
|
-
"nodemon": "^2.0.2"
|
|
18
|
-
},
|
|
19
16
|
"scripts": {
|
|
20
|
-
"dev": "nodemon app.js",
|
|
17
|
+
"dev": "env NODE_ENV=development nodemon app.js",
|
|
21
18
|
"start": "node app"
|
|
22
19
|
},
|
|
23
20
|
"keywords": [
|
|
@@ -30,5 +27,8 @@
|
|
|
30
27
|
"name": "Kafil NASDAMI",
|
|
31
28
|
"email": "kafil.nasdami@gmail.com"
|
|
32
29
|
},
|
|
33
|
-
"license": "MIT"
|
|
34
|
-
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"nodemon": "^3.1.0"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/utils.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
const axios = require("axios");
|
|
2
1
|
const { JSDOM } = require("jsdom");
|
|
3
2
|
|
|
4
3
|
const prayers = require("./data/prayers.json");
|
|
5
4
|
const chalk = require("chalk");
|
|
6
5
|
const { API_URL, DEFAULT_CITY } = require("./constants");
|
|
7
6
|
const { NOT_FOUND_ERROR } = require("./constants");
|
|
7
|
+
const fetch = require("node-fetch");
|
|
8
|
+
const cheerio = require("cheerio");
|
|
8
9
|
|
|
9
|
-
const error = msg => console.log(chalk.red(msg));
|
|
10
|
+
const error = (msg) => console.log(chalk.red(msg));
|
|
10
11
|
|
|
11
12
|
const getCityId = (arg, cities) => {
|
|
12
13
|
const parsed = parseInt(arg);
|
|
@@ -23,57 +24,58 @@ module.exports.getCityName = (arg, cities) => {
|
|
|
23
24
|
const index = getCityIndex(arg, cities);
|
|
24
25
|
if (index == -1) {
|
|
25
26
|
error(NOT_FOUND_ERROR);
|
|
27
|
+
return DEFAULT_CITY;
|
|
26
28
|
}
|
|
27
|
-
return
|
|
29
|
+
return arg;
|
|
28
30
|
};
|
|
29
31
|
|
|
30
32
|
const getCityIndex = (city, cities) =>
|
|
31
|
-
cities.map(e => e.name.toLowerCase()).indexOf(city.toLowerCase());
|
|
33
|
+
cities.map((e) => e.name.toLowerCase()).indexOf(city.toLowerCase());
|
|
32
34
|
|
|
33
|
-
module.exports.getData = async cityId =>
|
|
34
|
-
await
|
|
35
|
+
module.exports.getData = async (cityId) => {
|
|
36
|
+
const response = await fetch(`${API_URL}?ville=${cityId}`);
|
|
37
|
+
return await response.text();
|
|
38
|
+
};
|
|
35
39
|
|
|
36
|
-
module.exports.parsePrayerTimesFromResponse = response => {
|
|
37
|
-
const dom = new JSDOM(
|
|
40
|
+
module.exports.parsePrayerTimesFromResponse = (response) => {
|
|
41
|
+
const dom = new JSDOM(response);
|
|
38
42
|
const tds = dom.window.document.getElementsByTagName("td");
|
|
39
43
|
|
|
40
44
|
let j = 0;
|
|
41
|
-
for (let i =
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
j++;
|
|
45
|
-
}
|
|
45
|
+
for (let i = 1; i < tds.length; i += 2) {
|
|
46
|
+
prayers[j].time = tds[i].textContent.trim();
|
|
47
|
+
j++;
|
|
46
48
|
}
|
|
49
|
+
|
|
47
50
|
// Transorm array to object and return it
|
|
48
|
-
return prayers.reduce((acc, {
|
|
49
|
-
acc[
|
|
51
|
+
return prayers.reduce((acc, { prayer, time }) => {
|
|
52
|
+
acc[prayer] = time;
|
|
50
53
|
return acc;
|
|
51
54
|
}, {});
|
|
52
|
-
|
|
53
55
|
};
|
|
54
56
|
|
|
55
57
|
function tConv24(time24) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
58
|
+
const [hours, minutes] = time24.split(":");
|
|
59
|
+
const hour = Number(hours);
|
|
60
|
+
const formattedHour = hour % 12 || 12;
|
|
61
|
+
const formattedHourWithZero = (formattedHour + "").padStart(2, "0");
|
|
62
|
+
const formattedMinutes = minutes.padStart(2, "0");
|
|
63
|
+
const formattedTime = `${formattedHourWithZero}:${formattedMinutes}`;
|
|
64
|
+
const ampm = hour < 12 ? "AM" : "PM";
|
|
65
|
+
return `${formattedTime} ${ampm}`;
|
|
66
|
+
}
|
|
64
67
|
|
|
65
68
|
module.exports.displayResult = (prayers, city) => {
|
|
69
|
+
if (!prayers) return;
|
|
66
70
|
if (prayers) {
|
|
67
|
-
console.log(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
◽ ${chalk.cyan('Ishae')} --> ${chalk.green(tConv24(prayers.Ishae))}`
|
|
77
|
-
);
|
|
71
|
+
console.log(` 🧭 ${city}, Morocco\n\n 📆 ${new Date().toDateString()}\n`);
|
|
72
|
+
Object.keys(prayers).forEach((key) => {
|
|
73
|
+
console.log(
|
|
74
|
+
` ${chalk.cyan(key.padEnd(7, " "))} --> ${chalk.green(
|
|
75
|
+
tConv24(prayers[key])
|
|
76
|
+
)}`
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
console.log("\n");
|
|
78
80
|
}
|
|
79
81
|
};
|