45 const fetch = (await
import(
'node-fetch')).
default;
56 this.materialNames = [];
61 let params =
new URLSearchParams({
68 url +=
'?' + params.toString();
71 let haveMoreMaterials =
true;
72 while (haveMoreMaterials)
76 console.log(
'Fetch materials from url:', url)
78 const response = await fetch(url);
81 throw new Error(`HTTP error! status: ${response.status}`);
84 const jsonData = await response.json();
85 this.materials.push(jsonData)
87 for (
const material of jsonData.results) {
88 this.materialNames.push(material[
'title']);
93 let nextURL = jsonData.next
97 haveMoreMaterials =
true
101 console.log(
'Finished fetching materials')
102 haveMoreMaterials =
false;
107 this.logger.info(`Error: ${error.message}`);
108 haveMoreMaterials =
true;
112 return this.materials;
123 if (this.materials ===
null || this.materials.length === 0) {
127 const jsonData = this.materials[listNumber];
132 let jsonResults =
null;
133 let jsonResult =
null;
134 if (
'results' in jsonData) {
135 jsonResults = jsonData[
'results'];
136 if (jsonResults.length <= materialNumber) {
139 jsonResult = jsonResults[materialNumber];
147 let jsonPackages =
null;
148 if (
'packages' in jsonResult) {
149 jsonPackages = jsonResult[
'packages'];
155 if (jsonPackages.length <= packageId) {
158 const packageIdValue = jsonPackages[packageId];
160 if (!packageIdValue) {
164 const fetch = (await
import(
'node-fetch')).
default;
166 const url = `${this.packageUrl}/${packageIdValue}/download`;
167 const response = await fetch(url);
168 const data = await response.arrayBuffer();
169 const title = jsonResult[
'title'];
171 console.log(`Downloaded package: ${title} from ${url}`);
173 return [data, title];
225 const downloadList = [];
228 if (foundList.length > 0) {
229 for (
const found of foundList) {
230 const listNumber = found[
'listNumber'];
231 const materialNumber = found[
'materialNumber'];
232 const matName = found[
'title'];
233 this.logger.info(`> Download material: ${matName} List: ${listNumber}. Index: ${materialNumber}`);
234 const [data, title] = await this.
downloadPackage(listNumber, materialNumber, packageId);
235 downloadList.push([data, title]);