@@ -5,11 +5,48 @@ import { getConfig } from "./config";
5
5
import { logger } from "./logger" ;
6
6
import { CFIDToConfigGroup , ConfigarrCF , QualityDefintionsRadarr , QualityDefintionsSonarr } from "./types/common.types" ;
7
7
import { ConfigCustomFormat , ConfigQualityProfile , ConfigQualityProfileItem } from "./types/config.types" ;
8
- import { TrashArrSupported , TrashCF , TrashQP , TrashQualityDefintion } from "./types/trashguide.types" ;
8
+ import { TrashArrSupported , TrashCache , TrashCF , TrashQP , TrashQualityDefintion } from "./types/trashguide.types" ;
9
9
import { cloneGitRepo , loadJsonFile , mapImportCfToRequestCf , notEmpty , toCarrCF , trashRepoPaths } from "./util" ;
10
10
11
11
const DEFAULT_TRASH_GIT_URL = "https://github.com/TRaSH-Guides/Guides" ;
12
12
13
+ let cache : TrashCache ;
14
+ let cacheReady = false ;
15
+
16
+ const createCache = async ( ) => {
17
+ logger . debug ( `Creating TRaSH-Guides cache ...` ) ;
18
+
19
+ const radarrCF = await loadTrashCFs ( "RADARR" ) ;
20
+ const sonarrCF = await loadTrashCFs ( "SONARR" ) ;
21
+
22
+ const radarrQP = await loadQPFromTrash ( "RADARR" ) ;
23
+ const sonarrQP = await loadQPFromTrash ( "SONARR" ) ;
24
+
25
+ const radarrQDMovie = await loadQualityDefinitionFromTrash ( "movie" , "RADARR" ) ;
26
+ const sonarrQDSeries = await loadQualityDefinitionFromTrash ( "series" , "SONARR" ) ;
27
+ const sonarrQDAnime = await loadQualityDefinitionFromTrash ( "anime" , "SONARR" ) ;
28
+
29
+ cache = {
30
+ SONARR : {
31
+ qualityProfiles : sonarrQP ,
32
+ customFormats : sonarrCF ,
33
+ qualityDefinition : {
34
+ anime : sonarrQDAnime ,
35
+ series : sonarrQDSeries ,
36
+ } ,
37
+ } ,
38
+ RADARR : {
39
+ qualityProfiles : radarrQP ,
40
+ customFormats : radarrCF ,
41
+ qualityDefinition : {
42
+ movie : radarrQDMovie ,
43
+ } ,
44
+ } ,
45
+ } ;
46
+
47
+ cacheReady = true ;
48
+ } ;
49
+
13
50
export const cloneTrashRepo = async ( ) => {
14
51
logger . info ( `Checking TRaSH-Guides repo ...` ) ;
15
52
@@ -20,6 +57,7 @@ export const cloneTrashRepo = async () => {
20
57
21
58
const cloneResult = await cloneGitRepo ( rootPath , gitUrl , revision ) ;
22
59
logger . info ( `TRaSH-Guides repo: ref[${ cloneResult . ref } ], hash[${ cloneResult . hash } ], path[${ cloneResult . localPath } ]` ) ;
60
+ await createCache ( ) ;
23
61
} ;
24
62
25
63
export const loadTrashCFs = async ( arrType : TrashArrSupported ) : Promise < CFIDToConfigGroup > => {
@@ -29,6 +67,10 @@ export const loadTrashCFs = async (arrType: TrashArrSupported): Promise<CFIDToCo
29
67
return new Map ( ) ;
30
68
}
31
69
70
+ if ( cacheReady ) {
71
+ return cache [ arrType ] . customFormats ;
72
+ }
73
+
32
74
const carrIdToObject = new Map < string , { carrConfig : ConfigarrCF ; requestConfig : MergedCustomFormatResource } > ( ) ;
33
75
34
76
let pathForFiles : string ;
@@ -54,7 +96,7 @@ export const loadTrashCFs = async (arrType: TrashArrSupported): Promise<CFIDToCo
54
96
} ) ;
55
97
}
56
98
57
- logger . debug ( `Trash CFs: ${ carrIdToObject . size } ` ) ;
99
+ logger . debug ( `( ${ arrType } ) Trash CFs: ${ carrIdToObject . size } ` ) ;
58
100
59
101
return carrIdToObject ;
60
102
} ;
@@ -65,6 +107,13 @@ export const loadQualityDefinitionFromTrash = async (
65
107
) : Promise < TrashQualityDefintion > => {
66
108
let trashPath = arrType === "RADARR" ? trashRepoPaths . radarrQualitySize : trashRepoPaths . sonarrQualitySize ;
67
109
110
+ if ( cacheReady ) {
111
+ const cacheObject = cache [ arrType ] . qualityDefinition as any ;
112
+ if ( qdType in cacheObject ) {
113
+ return cacheObject [ qdType ] ;
114
+ }
115
+ }
116
+
68
117
switch ( qdType ) {
69
118
case "anime" :
70
119
return loadJsonFile ( path . resolve ( `${ trashPath } /anime.json` ) ) ;
@@ -73,21 +122,26 @@ export const loadQualityDefinitionFromTrash = async (
73
122
case "movie" :
74
123
return loadJsonFile ( path . resolve ( `${ trashPath } /movie.json` ) ) ;
75
124
case "custom" :
76
- throw new Error ( " Not implemented yet" ) ;
125
+ throw new Error ( `( ${ arrType } ) Not implemented yet` ) ;
77
126
default :
78
- throw new Error ( `Unknown QualityDefintion type: ${ qdType } ` ) ;
127
+ throw new Error ( `( ${ arrType } ) Unknown QualityDefintion type: ${ qdType } ` ) ;
79
128
}
80
129
} ;
81
130
82
131
export const loadQPFromTrash = async ( arrType : TrashArrSupported ) => {
83
132
let trashPath = arrType === "RADARR" ? trashRepoPaths . radarrQP : trashRepoPaths . sonarrQP ;
133
+
134
+ if ( cacheReady ) {
135
+ return cache [ arrType ] . qualityProfiles ;
136
+ }
137
+
84
138
const map = new Map < string , TrashQP > ( ) ;
85
139
86
140
try {
87
141
const files = fs . readdirSync ( `${ trashPath } ` ) . filter ( ( fn ) => fn . endsWith ( "json" ) ) ;
88
142
89
143
if ( files . length <= 0 ) {
90
- logger . info ( `Not found any TRaSH-Guides QualityProfiles. Skipping.` ) ;
144
+ logger . info ( `( ${ arrType } ) Not found any TRaSH-Guides QualityProfiles. Skipping.` ) ;
91
145
}
92
146
93
147
for ( const item of files ) {
@@ -96,7 +150,7 @@ export const loadQPFromTrash = async (arrType: TrashArrSupported) => {
96
150
map . set ( importTrashQP . trash_id , importTrashQP ) ;
97
151
}
98
152
} catch ( err : any ) {
99
- logger . warn ( " Failed loading TRaSH-Guides QualityProfiles. Continue without ..." , err ?. message ) ;
153
+ logger . warn ( `( ${ arrType } ) Failed loading TRaSH-Guides QualityProfiles. Continue without ...` , err ?. message ) ;
100
154
}
101
155
102
156
// const localPath = getLocalTemplatePath();
@@ -105,7 +159,7 @@ export const loadQPFromTrash = async (arrType: TrashArrSupported) => {
105
159
// fillMap(localPath);
106
160
// }
107
161
108
- logger . debug ( `Found ${ map . size } TRaSH-Guides QualityProfiles.` ) ;
162
+ logger . debug ( `( ${ arrType } ) Found ${ map . size } TRaSH-Guides QualityProfiles.` ) ;
109
163
return map ;
110
164
} ;
111
165
0 commit comments