zohlathu 1.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 +63 -0
- package/index.js +83 -0
- package/package.json +28 -0
- package/test.js +28 -0
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# zohlathu
|
|
2
|
+
|
|
3
|
+
A Node.js package for fetching Mizo song lyrics from [www.zohlathu.in](https://www.zohlathu.in).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
You can install the package using npm:
|
|
8
|
+
```bash
|
|
9
|
+
npm install zohlathu
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
Since fetching data is an asynchronous operation, use `async/await` or `.then()`.
|
|
15
|
+
|
|
16
|
+
```javascript
|
|
17
|
+
const { get_lyrics } = require('zohlathu');
|
|
18
|
+
|
|
19
|
+
async function fetchSong() {
|
|
20
|
+
const song_name = "C. Sanga - Tawnmang Lasi"; // Replace with the requested song
|
|
21
|
+
const lyrics = await get_lyrics(song_name); // Get the lyrics
|
|
22
|
+
|
|
23
|
+
if (lyrics) {
|
|
24
|
+
// Format the response
|
|
25
|
+
const response = `${lyrics.title}\n\n${lyrics.lyrics}\n\nSource: ${lyrics.source_url}`;
|
|
26
|
+
console.log(response);
|
|
27
|
+
} else {
|
|
28
|
+
console.log("Lyrics not found.");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
fetchSong();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Response Object format
|
|
36
|
+
* `lyrics.title` = The title of the lyrics
|
|
37
|
+
* `lyrics.lyrics` = The lyrics content
|
|
38
|
+
* `lyrics.source_url` = The url of the original post
|
|
39
|
+
|
|
40
|
+
## Handle Errors
|
|
41
|
+
|
|
42
|
+
The function handles most errors internally and returns `null` if it fails. You can add your own `try...catch` block around the execution.
|
|
43
|
+
|
|
44
|
+
```javascript
|
|
45
|
+
const { get_lyrics } = require('zohlathu');
|
|
46
|
+
|
|
47
|
+
async function safeFetch() {
|
|
48
|
+
try {
|
|
49
|
+
const song_name = "C. Sanga - Tawnmang Lasi";
|
|
50
|
+
const lyrics = await get_lyrics(song_name);
|
|
51
|
+
|
|
52
|
+
if (lyrics) {
|
|
53
|
+
console.log(`${lyrics.title}\n\n${lyrics.lyrics}\n\nSource: ${lyrics.source_url}`);
|
|
54
|
+
}
|
|
55
|
+
} catch (e) {
|
|
56
|
+
console.error(`An error occurred: ${e.message}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
safeFetch();
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
**Requirements:** Node.js 14 or higher.
|
package/index.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
const Parser = require('rss-parser');
|
|
2
|
+
const { htmlToText } = require('html-to-text');
|
|
3
|
+
const ytSearch = require('yt-search');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Fetch lyrics for the given song query
|
|
7
|
+
* * @param {string} query - Song name or artist to search for
|
|
8
|
+
* @returns {Promise<Object|null>} - Dictionary containing title and lyrics, or null if not found
|
|
9
|
+
*/
|
|
10
|
+
async function get_lyrics(query) {
|
|
11
|
+
await new Promise(resolve => setTimeout(resolve, 3000));
|
|
12
|
+
|
|
13
|
+
if (!query || typeof query !== 'string') {
|
|
14
|
+
console.error("Input validation error: Invalid search query. Must be a non-empty string.");
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const base_url = "https://www.blogger.com/feeds/690973182178026088/posts/default";
|
|
19
|
+
let feed_url = "";
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
const results = await ytSearch(query);
|
|
23
|
+
const videos = results.videos;
|
|
24
|
+
|
|
25
|
+
if (!videos || videos.length === 0) {
|
|
26
|
+
console.log(`No YouTube results found for query: ${query}`);
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const titlep = videos[0].title.substring(0, 100);
|
|
31
|
+
const search_query = titlep.replace(/ /g, '+');
|
|
32
|
+
feed_url = `${base_url}?q=${search_query}`;
|
|
33
|
+
|
|
34
|
+
} catch (youtube_error) {
|
|
35
|
+
console.error(`Youtube error: ${youtube_error}`);
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
const parser = new Parser({
|
|
41
|
+
customFields: {
|
|
42
|
+
item: ['content']
|
|
43
|
+
},
|
|
44
|
+
headers: {
|
|
45
|
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
|
|
46
|
+
'Accept-Language': 'en-US,en;q=0.9',
|
|
47
|
+
'Accept': 'application/atom+xml,text/html'
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
const feed = await parser.parseURL(feed_url);
|
|
53
|
+
|
|
54
|
+
if (!feed.items || feed.items.length === 0) {
|
|
55
|
+
console.log(`No entries found in feed for query: ${query}`);
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const entry = feed.items[0];
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
let content = htmlToText(entry.content, {
|
|
63
|
+
wordwrap: false,
|
|
64
|
+
preserveNewlines: true
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
const pattern = /\* \* \*[\s\S]*?\* \* \*/g;
|
|
69
|
+
const cleaned_content = content.replace(pattern, '').trim();
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
title: entry.title,
|
|
73
|
+
lyrics: cleaned_content,
|
|
74
|
+
source_url: entry.link
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
} catch (feed_error) {
|
|
78
|
+
console.error(`Feed parsing or Unexpected error: ${feed_error}`);
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
module.exports = { get_lyrics };
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "zohlathu",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A Node.js package for fetching Mizo song lyrics from www.zohlathu.in",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/RSR-TG-Info/ZoHlathu-JS.git"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"mizo",
|
|
15
|
+
"lyrics",
|
|
16
|
+
"zohlathu"
|
|
17
|
+
],
|
|
18
|
+
"author": "RSR",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=14.0.0"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"rss-parser": "*",
|
|
25
|
+
"html-to-text": "*",
|
|
26
|
+
"yt-search": "*"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/test.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const { get_lyrics } = require('./index.js');
|
|
2
|
+
|
|
3
|
+
async function runTest() {
|
|
4
|
+
console.log("Hla thu kan zawng mek e, lo nghak lawk rawh...");
|
|
5
|
+
|
|
6
|
+
// I hla zawn duh chu hetah hian i thlak thei ang
|
|
7
|
+
const songToSearch = "Tawngmang lasi";
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
const result = await get_lyrics(songToSearch);
|
|
11
|
+
|
|
12
|
+
if (result) {
|
|
13
|
+
console.log("\n✅ HLA HMUH A NI!");
|
|
14
|
+
console.log("----------------------------------");
|
|
15
|
+
console.log("Hla Hming :", result.title);
|
|
16
|
+
console.log("Link :", result.source_url);
|
|
17
|
+
console.log("----------------------------------");
|
|
18
|
+
console.log(result.lyrics);
|
|
19
|
+
console.log("----------------------------------");
|
|
20
|
+
} else {
|
|
21
|
+
console.log("\n❌ Pawi lutuk, he hla hi hmuh a ni lo.");
|
|
22
|
+
}
|
|
23
|
+
} catch (error) {
|
|
24
|
+
console.error("Harsatna a awm:", error);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
runTest();
|