11/*
2- Only to be run seperatly when importing new music files
2+ Only needs to be run seperatly when importing new music files
33 Very basic
44
55*/
66
77
8- const fs = require ( "fs" ) ;
9- const localLibrary = require ( "./localLibrary.json" ) ;
8+ const fs = require ( "fs/promises " ) ;
9+ const localLibrary = { } ;
1010
1111const homedir = require ( "os" ) . homedir ( ) ;
12- const pathToFiles = homedir + "/mandarineFiles/" ;
13-
1412const path = require ( "path" ) ;
1513
14+ const pathToDLFiles = path . join ( homedir , "mandarineFiles" ) ;
15+
16+ const config = require ( "../config.json" ) ;
17+
1618let parseFile ;
19+ let fileCount = 0 ;
20+ let dirCount = 0 ;
21+
1722// import('music-metadata').then((mmModule)=>{console.log("mm: " + mmModule); parseFile = mmModule.parseFile});
1823
19- fs . readdir ( pathToFiles , async function ( err , files ) {
2024
21- let filecount = 0 ;
22- if ( err ) throw err ;
23- // console.log(files);
24- const musicmetadata = await import ( "music-metadata" ) ;
25- parseFile = musicmetadata . parseFile ;
26- for ( let i = 0 ; i < files . length ; i ++ ) {
27- const fileName = files [ i ] ;
28- // console.log(fileName);
29- // only support mp3
30- if ( fileName . endsWith ( ".mp3" ) ) {
31- await addFile ( fileName ) ;
32- filecount ++ ;
25+ async function indexDirectory ( directory ) {
26+ const files = await fs . readdir ( directory , { withFileTypes : true } ) ;
27+ for ( const file of files ) {
28+ if ( file . isDirectory ( ) ) {
29+ await indexDirectory ( path . join ( directory , file . name ) ) ;
30+ dirCount ++ ;
3331 }
34- if ( i == files . length - 1 ) {
35- console . log ( "write" ) ;
36- setTimeout ( ( ) => {
37- console . log ( "Indexed " + filecount + " files." ) ;
38- write ( ) ;
39- } , 3_000 ) ;
32+ else if ( file . name . endsWith ( ".mp3" ) ) {
33+ await addFile ( path . join ( directory , file . name ) ) ;
34+ fileCount ++ ;
4035 }
4136 }
42- } ) ;
37+ return true ;
38+ }
4339
44- async function addFile ( fileName ) {
40+ async function addFile ( filePath ) {
4541
46- if ( ! localLibrary [ fileName ] ) {
47- const metadata = await parseFile ( path . join ( pathToFiles , fileName ) ) ;
42+ if ( ! localLibrary [ filePath ] ) {
43+ const metadata = await parseFile ( filePath ) ;
4844 // console.log("metadata:" + JSON.stringify(metadata.common.title));
4945 const title = metadata . common . title ;
5046 const artist = metadata . common . artist ;
51- localLibrary [ fileName ] = {
47+ const search = [
48+ ( title ) . replace ( / \. | ' | - / g, "" ) ,
49+ ( artist + " " + title ) . replace ( / \. | ' | - / g, "" ) ,
50+ ( title + " " + artist ) . replace ( / \. | ' | - / g, "" ) ,
51+ ] ;
52+
53+ if ( artist . split ( " " ) . length > 1 ) {
54+ // if the artist has spaces in their names, chances are people will only add one part of their name
55+ for ( const artistNameSplit of artist . split ( " " ) ) {
56+ search . push ( ( title + " " + artistNameSplit ) . replace ( / \. | ' | - / g, "" ) ) ;
57+ search . push ( ( artistNameSplit + " " + title ) . replace ( / \. | ' | - / g, "" ) ) ;
58+ }
59+ }
60+
61+ if ( search [ 2 ] != ( title ) . replace ( / \( .* \) | \. | ' | - / g, "" ) ) {
62+ search . push ( ( title ) . replace ( / \( .* \) | \. | ' | - / g, "" ) ) ;
63+ }
64+
65+ localLibrary [ filePath ] = {
5266 "title" : title ,
5367 "artist" : artist ,
54- "search" : [
55- ( title + " " + artist ) . replace ( / \. | ' | - / g, "" ) ,
56- ( artist + " " + title ) . replace ( / \. | ' | - / g, "" ) ,
57- ( title ) . replace ( / \. | ' | - / g, "" ) ,
58- ( title ) . replace ( / \( .* \) | \. | ' | - / g, "" ) ,
59- ] ,
68+ "search" : search ,
6069 } ;
6170
62-
6371 }
6472}
6573
66- function write ( ) {
74+ async function write ( ) {
6775 // console.log(localLibrary);
68- fs . writeFileSync ( path . join ( __dirname , "localLibrary.json" ) , JSON . stringify ( localLibrary ) ) ;
69- }
76+ await fs . writeFile ( path . join ( __dirname , "localLibrary.json" ) , JSON . stringify ( localLibrary ) ) ;
77+ return true ;
78+ }
79+
80+ async function index ( ) {
81+ const musicmetadata = await import ( "music-metadata" ) ;
82+ parseFile = musicmetadata . parseFile ;
83+
84+ if ( config . additionalMusicDirs ) {
85+ for ( const dir of config . additionalMusicDirs ) {
86+ console . log ( "Indexing " + dir + " and its subdirectories." ) ;
87+ await indexDirectory ( dir ) ;
88+ }
89+ }
90+ console . log ( "Indexing " + pathToDLFiles + " and its subdirectories." ) ;
91+ await indexDirectory ( pathToDLFiles ) ;
92+ console . log ( "write" ) ;
93+ console . log ( "Indexed " + fileCount + " files in " + dirCount + " folders." ) ;
94+ await write ( ) ;
95+ return 1 ;
96+ }
97+
98+
99+ if ( require . main === module ) {
100+ // called directly
101+ index ( ) ;
102+ }
103+
104+ module . exports = { index } ;
0 commit comments