-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchFromZeroheight.js
125 lines (111 loc) · 3.24 KB
/
fetchFromZeroheight.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const fs = require('fs');
// Replace API_KEY and API_CLIENT with your zeroheight API credentials
const headers = {
'Accept': 'application/json',
'X-API-KEY': '{API_KEY}',
'X-API-CLIENT': '{API_CLIENT}'
};
// Replace STYLEGUIDE_ID with the ID of the styleguide you want to retrieve pages from
const styleguideId = '{STYLEGUIDE_ID}';
const getPages = async () => {
try {
const response = await fetch(
`https://zeroheight.com/open_api/v2/styleguides/${styleguideId}/pages`,
{
method: "GET",
headers: headers
}
);
if (!response.ok) {
console.error('Error response:', response);
return null;
}
const json = await response.json();
if (!json || !json.data || !json.data.pages) {
console.error('Error: Invalid response format');
return null;
}
const pageIds = json.data.pages.map(page => page.id);
console.log('Page IDs:', pageIds);
return { pageIds };
} catch (error) {
console.error('Error fetching pages:', error);
return null;
}
};
const getPageContent = (pageId) => {
const url = `https://zeroheight.com/open_api/v2/pages/${pageId}?format=markdown`;
console.log(`Fetching URL: ${url}`);
return fetch(url, {
method: "GET",
headers: headers
})
.then((response) => {
console.log(`Response status for ${pageId}: ${response.status}`);
if (response.ok) {
return response.json();
} else {
console.error(`${pageId} skipped because of error ${response.status}`);
return null;
}
})
.then((json) => {
if (!json || !json.data.page) {
console.error(`${pageId} skipped because of error ${json ? json.status : 'unknown'}`);
return null;
}
const page = json.data.page;
if (page.tabs) {
return {
name: page.name,
url: page.url,
tabs: page.tabs.map(tab => ({
name: tab.name,
content: tab.content
}))
};
} else {
return {
name: page.name,
url: page.url,
content: page.content
};
}
});
};
async function writePageContentsToFile() {
const pages = await getPages();
if (!pages || !pages.pageIds) {
console.error('Error: getPages did not return a valid object with pageIds');
return;
}
const { pageIds } = pages;
const formattedContents = [];
for (const pageId of pageIds) {
const content = await getPageContent(pageId);
//flatten the tabbed content to make it easier to process by Pinecone
if (content) {
if (content.tabs) {
content.tabs.forEach(tab => {
formattedContents.push({
id: `${pageId}-${tab.name}`,
name: content.name,
url: content.url,
tabName: tab.name,
content: tab.content
});
});
} else {
formattedContents.push({
id: pageId,
name: content.name,
url: content.url,
content: content.content
});
}
}
}
fs.writeFileSync('formattedResponse.json', JSON.stringify(formattedContents, null, 2));
}
// Call the function to execute it
writePageContentsToFile();