-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-info-boxes-for-board.mjs
149 lines (121 loc) · 3.99 KB
/
get-info-boxes-for-board.mjs
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import * as fs from 'node:fs';
import * as path from 'node:path';
import playwright from 'playwright';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath( import.meta.url );
const __dirname = path.dirname( __filename );
const BOARD_ID_PLACEHOLDER = '[BOARD ID]';
const COLLABORATION_BOX_PLACEHOLDER = '[COLLABORATION BOX PLACEHOLDER]';
const CURSOR_PLACEHOLDER = '[CURSOR]';
const GET_ITEM_IDS_INITIAL_QUERY = `query {
boards(ids: ${ BOARD_ID_PLACEHOLDER }) {
name
items_page{
cursor
items {
id
name
}
}
}
}`;
const GET_ITEM_IDS_NEXT_PAGE_QUERY = `query {
next_items_page (cursor: "${ CURSOR_PLACEHOLDER }") {
cursor
items {
id
name
}
}
}
`;
const COLLABORATION_BOX_URL = `https://nyu-lib.monday.com/projects/${ COLLABORATION_BOX_PLACEHOLDER }/collaboration_boxes.json`
const MONDAY_COM_API_URL = 'https://api.monday.com/v2/';
// Collaboration box JSON files written to this output directory
const COLLABORATION_BOX_FILES_DIR = path.join( __dirname, 'collaboration-box-files' );
// Playwright
const DEFAULT_TIMEOUT = 30_000;
let browser;
let page;
async function fetchInfoBoxes( itemIds, outputDir ) {
for ( let i = 0; i < itemIds.length; i++ ) {
const itemId = itemIds[ i ];
const url = COLLABORATION_BOX_URL.replaceAll( COLLABORATION_BOX_PLACEHOLDER, itemId );
try {
const response = await page.goto( url );
const responseObject = await response.json();
fs.writeFileSync(
path.join( outputDir, `${ itemId }.json` ),
JSON.stringify( responseObject, null, ' ' ),
{ encoding: 'utf8' },
);
} catch ( e ) {
console.error( e );
}
}
}
async function getItemIdsForBoard( boardId ) {
const itemIds = [];
const initialQuery = GET_ITEM_IDS_INITIAL_QUERY.replaceAll( BOARD_ID_PLACEHOLDER, boardId );
let queryResults = await getQueryResults( initialQuery );
itemIds.push( ...queryResults.itemIds );
while ( queryResults.cursor ) {
const nextPageQuery = GET_ITEM_IDS_NEXT_PAGE_QUERY.replaceAll( CURSOR_PLACEHOLDER, queryResults.cursor );
queryResults = await getQueryResults( nextPageQuery );
itemIds.push( ...queryResults.itemIds );
}
return itemIds;
}
async function getQueryResults( query ) {
const init = {
method : 'post',
headers : {
'Content-Type' : 'application/json',
'Authorization' : process.env.API_KEY,
'API-Version' : '2023-04',
},
body : JSON.stringify( { 'query' : query } ),
};
const response = await getResponseJson( MONDAY_COM_API_URL, init );
// Get items for this page
const itemsPage = response.data.boards ?
response.data.boards[ 0 ].items_page :
response.data.next_items_page;
return {
itemIds : itemsPage.items.map( item => item.id ),
cursor : itemsPage.cursor,
};
}
async function getResponseJson( url, init ) {
let responseJson;
try {
const response = await fetch( url, init );
responseJson = await response.json();
} catch ( error ) {
throw (
error
);
}
return responseJson;
}
async function initializePlaywright( timeoutOption ) {
browser = await playwright
.chromium
.connectOverCDP( 'http://localhost:9222' );
const defaultContext = browser.contexts()[ 0 ];
page = defaultContext.pages()[ 0 ];
const timeout = timeoutOption || DEFAULT_TIMEOUT;
page.setDefaultTimeout( timeout );
}
async function main() {
const boardId = process.argv[ 2 ];
const itemIds = await getItemIdsForBoard( boardId );
await initializePlaywright();
const outputDir = path.join( COLLABORATION_BOX_FILES_DIR, boardId );
if ( ! fs.existsSync( outputDir ) ) {
fs.mkdirSync( outputDir );
}
await fetchInfoBoxes( itemIds, outputDir );
browser.close();
}
main();