Skip to content

Commit

Permalink
Merge pull request #15 from Mostafa-wael/feat/pause-resume
Browse files Browse the repository at this point in the history
Feat/pause_resume
  • Loading branch information
ahmedihabb2 authored Sep 28, 2023
2 parents d334749 + bb4de79 commit af9e51c
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 28 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ https://user-images.githubusercontent.com/56788883/181006153-f1d41833-fc60-43e0-
- Debian based distributions
`sudo add-apt-repository ppa:mc3man/mpv-tests`
`sudo apt update && sudo apt install mpv`
- [Update] For Ubuntu 22.04
`sudo apt install mpv`
- Windows
[Follow This link](https://mpv.io/installation/#:~:text=master%20is%20recommended.-,Windows,-All%20binary%20packages)

Expand All @@ -36,6 +38,8 @@ https://user-images.githubusercontent.com/56788883/181006153-f1d41833-fc60-43e0-
Quran-CLI
Listen to the Quran from your terminal
While listening press P to toggle between pause and resume
Press q to exit
Options
Expand Down Expand Up @@ -67,4 +71,4 @@ Options
- Install typescript using: `npm install -g typescript` if you do not have typescript globally
- Install dependencies: `npm install`
- Build the source files: `npm run build`
- Run the index: `node src/index.js [options]`
- Run the index: `node bin/index.js [options]`
17 changes: 9 additions & 8 deletions lib/modes/radioMode/radioMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
Expand All @@ -36,20 +36,21 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runRadio = exports.showAllRadios = void 0;
var node_fetch_1 = require("node-fetch");
var helperFunctions_1 = require("../../utilities/helperFunctions");
var url = "https://api.mp3quran.net/radios/radio_english.json";
function getData() {
try {
helperFunctions_1.print("Fetching data...", "cyan");
return node_fetch_1.default(url)
(0, helperFunctions_1.print)("Fetching data...", "cyan");
return (0, node_fetch_1.default)(url)
.then(function (res) { return res.json(); })
.then(function (res) {
return res['radios'];
});
}
catch (err) {
helperFunctions_1.print("No available Internet connection", "red");
(0, helperFunctions_1.print)("No available Internet connection", "red");
process.exit(1);
}
}
Expand Down Expand Up @@ -85,9 +86,9 @@ function getSpecificRadioData(radioIndex) {
}
function showAllRadios() {
getRadioNamesList().then(function (res) {
helperFunctions_1.showListIndex(res, 'Radio Index', 'Name');
(0, helperFunctions_1.showListIndex)(res, 'Radio Index', 'Name');
}).catch(function (err) {
helperFunctions_1.raiseError("SHOW_ALL_RADIOS", "Error while showing all the radios");
(0, helperFunctions_1.raiseError)("SHOW_ALL_RADIOS", "Error while showing all the radios");
});
}
exports.showAllRadios = showAllRadios;
Expand All @@ -104,7 +105,7 @@ function runRadio(radioIndex) {
return [4, data['name']];
case 2:
radioName = _b.sent();
helperFunctions_1.print("Radio Channel: " + radioName, "green");
(0, helperFunctions_1.print)("Radio Channel: ".concat(radioName), "green");
_a = helperFunctions_1.runFromURL;
return [4, data['radio_url']];
case 3:
Expand All @@ -116,7 +117,7 @@ function runRadio(radioIndex) {
case 5:
list = _b.sent();
errMsg = "Please, enter an index from 0 to " + (list.length - 1) + "\nYou can list all the radio channels using the '-n' option.";
helperFunctions_1.raiseError("INVALID_VALUE", errMsg);
(0, helperFunctions_1.raiseError)("INVALID_VALUE", errMsg);
return [3, 6];
case 6: return [2];
}
Expand Down
23 changes: 12 additions & 11 deletions lib/modes/reciterMode/reciterMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
Expand All @@ -36,6 +36,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runSurah = exports.showReciterAvailableSuras = exports.getReciterAvailableSuras = exports.showAllReciters = void 0;
var node_fetch_1 = require("node-fetch");
var helperFunctions_1 = require("../../utilities/helperFunctions");
var data_2 = require("../../utilities/data");
Expand All @@ -47,8 +48,8 @@ function getData() {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, , 4]);
helperFunctions_1.print("Fetching data...", "cyan");
return [4, node_fetch_1.default(url)];
(0, helperFunctions_1.print)("Fetching data...", "cyan");
return [4, (0, node_fetch_1.default)(url)];
case 1:
data = _a.sent();
return [4, data.json()];
Expand All @@ -57,7 +58,7 @@ function getData() {
return [2, data_1['reciters']];
case 3:
err_1 = _a.sent();
helperFunctions_1.print("No available Internet connection", "red");
(0, helperFunctions_1.print)("No available Internet connection", "red");
process.exit(1);
return [3, 4];
case 4: return [2];
Expand Down Expand Up @@ -98,10 +99,10 @@ function getSpecificReciterData(reciterIndex) {
function showAllReciters() {
getReciterNamesList()
.then(function (res) {
helperFunctions_1.showListIndex(res, 'Reciter Index', 'Name');
(0, helperFunctions_1.showListIndex)(res, 'Reciter Index', 'Name');
})
.catch(function (err) {
helperFunctions_1.raiseError("SHOW_ALL_RECITERS", "Error while showing all the reciters");
(0, helperFunctions_1.raiseError)("SHOW_ALL_RECITERS", "Error while showing all the reciters");
});
}
exports.showAllReciters = showAllReciters;
Expand Down Expand Up @@ -144,11 +145,11 @@ function showReciterAvailableSuras(reciterIndex) {
return [4, getReciterAvailableSuras(reciterIndex)];
case 1:
availableSuras = _a.sent();
helperFunctions_1.showListIndex(availableSuras, 'Surah Index', 'Name', true);
(0, helperFunctions_1.showListIndex)(availableSuras, 'Surah Index', 'Name', true);
return [3, 3];
case 2:
err_2 = _a.sent();
helperFunctions_1.raiseError("INVALID_VALUE", "Reciter not available, you can list the available reciters using the '-r' option.");
(0, helperFunctions_1.raiseError)("INVALID_VALUE", "Reciter not available, you can list the available reciters using the '-r' option.");
return [3, 3];
case 3: return [2];
}
Expand All @@ -172,7 +173,7 @@ function runSurah(reciterIndex, surahIndex) {
if (surahIndex > reciterData['suras'].split(",").length) {
throw new Error("Surah not available, you can check the available suras for the specified reciter by passing the reciter index only to the '-c' option.");
}
helperFunctions_1.print("Reciter: " + reciterName + ", Surah: " + data_2.surasDictionary[surahIndex], "green");
(0, helperFunctions_1.print)("Reciter: ".concat(reciterName, ", Surah: ").concat(data_2.surasDictionary[surahIndex]), "green");
_a = helperFunctions_1.runFromURL;
return [4, getSurahURL(reciterIndex, surahIndex)];
case 3:
Expand All @@ -181,9 +182,9 @@ function runSurah(reciterIndex, surahIndex) {
case 4:
err_3 = _b.sent();
if (data_2.surasDictionary[surahIndex] === undefined)
helperFunctions_1.raiseError("INVALID_VALUE", "Surah not available, you can check the available suras for the specified reciter by passing the reciter index only to the '-c' option.");
(0, helperFunctions_1.raiseError)("INVALID_VALUE", "Surah not available, you can check the available suras for the specified reciter by passing the reciter index only to the '-c' option.");
else
helperFunctions_1.raiseError("INVALID_VALUE", "Reciter not available, you can list the available reciters using the '-r' option.");
(0, helperFunctions_1.raiseError)("INVALID_VALUE", "Reciter not available, you can list the available reciters using the '-r' option.");
return [3, 5];
case 5: return [2];
}
Expand Down
1 change: 1 addition & 0 deletions lib/utilities/data.js

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

33 changes: 29 additions & 4 deletions lib/utilities/helperFunctions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.raiseError = exports.runFromURL = exports.showListIndex = exports.print = void 0;
var data_1 = require("./data");
var colors = require("colors");
var Table = require("../custom_node_modules/cli-table3");
var mpv = require("node-mpv");
var readline = require('readline');
function print(str, color) {
if (color === void 0) { color = "white"; }
console.log(colors[color](str));
Expand All @@ -25,16 +27,39 @@ function showListIndex(arr, header1, header2, suras) {
exports.showListIndex = showListIndex;
function runFromURL(url) {
try {
var mpvPlayer = new mpv({
var mpvPlayer_1 = new mpv({
"verbose": false,
"audio_only": true
});
mpvPlayer.load(url);
mpvPlayer.play();
mpvPlayer.on('stopped', function () {
mpvPlayer_1.addProperty('pause', 'no');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
mpvPlayer_1.load(url);
mpvPlayer_1.play();
mpvPlayer_1.on('stopped', function () {
print("Finished playing", "green");
process.exit(1);
});
rl.input.on('keypress', function (key, data) {
if (data.name === 'p') {
mpvPlayer_1.togglePause();
mpvPlayer_1.getProperty('pause').then(function (val) {
if (val) {
mpvPlayer_1.setProperty('pause', 'yes');
print("\nPaused", "yellow");
}
else {
mpvPlayer_1.setProperty('pause', 'no');
print("\nResumed", "green");
}
});
}
else if (data.name === 'q') {
mpvPlayer_1.stop();
}
});
}
catch (err) {
print("You don't have MPV installed", "red");
Expand Down
28 changes: 28 additions & 0 deletions lib/utilities/helperFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { surasDictionary } from "./data";
import colors = require('colors');
import Table = require('../custom_node_modules/cli-table3');
import mpv = require('node-mpv');
const readline = require('readline');


export function print(str: string, color: string = "white") {
console.log(colors[color](str));
Expand All @@ -28,12 +30,38 @@ export function runFromURL(url: string) {
"verbose": false,
"audio_only": true
});
// set property
mpvPlayer.addProperty('pause', 'no');
// Handle keypress events
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
mpvPlayer.load(url);
mpvPlayer.play();
mpvPlayer.on('stopped', function () {
print("Finished playing", "green");
process.exit(1); // close the program
});
rl.input.on('keypress', (key, data) => {
if (data.name === 'p') {
// Toggle pause and resume
mpvPlayer.togglePause();
mpvPlayer.getProperty('pause').then(function (val) {
if (val) {
mpvPlayer.setProperty('pause', 'yes');
print("\nPaused", "yellow");
} else {
mpvPlayer.setProperty('pause', 'no');
print("\nResumed", "green");
}
}
);
}
else if (data.name === 'q') {
mpvPlayer.stop();
}
});
}
catch (err) {
print("You don't have MPV installed", "red");
Expand Down
5 changes: 3 additions & 2 deletions lib/utilities/parser.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommandLine = void 0;
var ts_command_line_args_1 = require("ts-command-line-args");
var CommandLine = (function () {
function CommandLine() {
}
CommandLine.getArgs = function () {
var args = ts_command_line_args_1.parse({
var args = (0, ts_command_line_args_1.parse)({
showRadios: { type: Boolean, optional: true, alias: 'n', description: 'Shows all available radio channels' },
radio: { type: Number, optional: true, alias: 'd', description: 'Play specific radio' },
reciterSurah: { type: Number, multiple: true, optional: true, alias: 'c', description: 'lay specific surah by a specific reciter. \nIf no surah specified, it will shows the available suras for the specified reciter.' },
Expand All @@ -15,7 +16,7 @@ var CommandLine = (function () {
help: { type: Boolean, optional: true, alias: 'h', description: 'Prints this usage guide' },
}, {
helpArg: 'help',
headerContentSections: [{ header: 'Quran-CLI', content: 'Listen to the Quran from your terminal' }],
headerContentSections: [{ header: 'Quran-CLI', content: 'Listen to the Quran from your terminal \n While listening press P to toggle between pause and resume \n Press q to exit' }],
footerContentSections: [{ header: '^_^', content: "Recall us in your doa'!" }],
});
if (args.reciterSurah == undefined) {
Expand Down
2 changes: 1 addition & 1 deletion lib/utilities/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class CommandLine {
},
{
helpArg: 'help',
headerContentSections: [{ header: 'Quran-CLI', content: 'Listen to the Quran from your terminal' }],
headerContentSections: [{ header: 'Quran-CLI', content: 'Listen to the Quran from your terminal \n While listening press P to toggle between pause and resume \n Press q to exit' }],
footerContentSections: [{ header: '^_^', content: `Recall us in your doa'!` }],
},
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"name": "@quran-cli/q-cli",
"description": "A simple command line interface for listening to Quran. ## Used API [mp3quran](https://mp3quran.net/eng/api) ## Installation - `npm install` - Install **mpv** as it is required for playing the audio. - Debian based distributions `sudo add-apt-repository ppa:mc3man/mpv-tests` `sudo apt update && sudo apt install mpv` - Windows [Follow This link](https://mpv.io/installation/#:~:text=master%20is%20recommended.-,Windows,-All%20binary%20packages)",
"version": "1.1.2",
"version": "1.1.3",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified",
Expand Down

0 comments on commit af9e51c

Please sign in to comment.