-
Notifications
You must be signed in to change notification settings - Fork 9
/
pull-mcu-timeline-sheet.ts
281 lines (208 loc) · 8.44 KB
/
pull-mcu-timeline-sheet.ts
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import fs from 'fs-extra'
import 'dotenv/config'
import axios from 'axios'
import type { MCUTimelineSheetRecord } from '~/src/helpers/types'
import { storePath } from '~/src/config.ts'
import {
matchListingToOrdered,
organizeOrderData,
} from '~/src/helpers/node/mcu-timeline-sheet.ts'
import {
makeInUniverseMarkdown,
makeUpcomingListingsMarkdown,
updateReadmeListContent,
} from '~/src/helpers/node/readme.ts'
import { upsertListingMarkdown } from '~/src/helpers/markdown-page.ts'
import {
getListingDetailsFromPaths,
getListingFiles,
getListingFromFile,
getUpcomingListings,
writeMarkdownFileNode,
} from '~/src/helpers/node/listing-files.ts'
import {
FilteredListings,
} from '~/src/helpers/listing-filters'
import {
ensureFiltersHaveStories,
} from '~/src/helpers/node/markdown-files.ts'
import { saveMoviesFandomTimeline } from '~/src/helpers/node/movies-fandom-timeline.ts'
const macroUrl = 'https://script.google.com/macros/s/AKfycbzGvKKUIaqsMuCj7-A2YRhR-f7GZjl4kSxSN1YyLkS01_CfiyE/exec'
const mcuTimelineSheetId = '1Xfe--9Wshbb3ru0JplA2PnEwN7mVawazKmhWJjr_wKs'
async function readMarkdownFileNode ( filePath: string ) {
// const markdownContent = await fs.readFile( filePath, 'utf8' )
return await getListingFromFile( filePath )
}
const typesReadmeMap = {
'movie': '🎬 Marvel Studios',
'abc': '⚫️ ABC',
'freeform': '🔵 Freeform',
'sony': '🕷 Sony',
'disney-plus-netflix': '🟥 Netflix',
'disney-plus': '🏰 Disney+',
'hulu': '🟩 Hulu',
}
function buildReadmeList ( matchesMap: Map<number, any> ) {
const matches = Array.from( matchesMap.values() )
const readmeListLines = matches.map( ( match: any, index ) => {
const { listing, timelineType } = match
const parts = [
'',
`<kbd>${ index + 1 }</kbd>`,
`[${ listing.title }](https://marvelorder.com${ listing.endpoint })`,
typesReadmeMap[ timelineType ],
`[Edit](${ listing.githubEditUrl })`,
]
return parts.join( ' - ' ).trim()
} )
return `\n\n${ readmeListLines.join( '\n' ) }\n\n`
}
( async () => {
const listingFiles = await getListingFiles()
// console.log( 'listingFiles', listingFiles )
const listingsDetails = await getListingDetailsFromPaths( listingFiles )
const listingDetailsMap = new Map( listingsDetails.map( ( details: any ) => [ details.listing.id, details ] ) )
const allListings = listingsDetails.map( ( details: any ) => details.listing )
const orderableListings = new FilteredListings( {
listings: allListings,
// initialFilters: new Map([
// [ isUpcoming, false ]
// ])
listingsSort: 'default',
} )
const fetchedSheet: {
records: MCUTimelineSheetRecord[]
} = await axios( macroUrl, {
params: {
id: mcuTimelineSheetId,
sheet: 'Chronological Order',
header: 1,
startRow: 4,
},
} ).then( ( res ) => {
return {
...res.data,
records: res.data.records.map( ( record: MCUTimelineSheetRecord, recordIndex ) => {
const cleanedRecordEntries = Object.entries( record )
.filter( ( [ key ] ) => {
const hasKey = key.length > 0
return hasKey
} )
.map( ( [ key, value ]: any ) => {
const isNotes = key.toLowerCase() === 'notes'
if ( !isNotes ) {
return [ key, value ]
}
const isBelowDetailsRows = recordIndex > 30
return [
key,
isBelowDetailsRows ? value : '',
]
} )
// console.log( 'cleanedRecordEntries', cleanedRecordEntries )
return Object.fromEntries( cleanedRecordEntries )
} ),
}
} )
const sheetJsonPath = `${ storePath }/mcu-timeline-sheet.json`
// Read existing data
const existingSheet = await fs.readJson( sheetJsonPath )
const recordsByTitle: Map<MCUTimelineSheetRecord['TITLE'], MCUTimelineSheetRecord> = new Map( existingSheet.records.map( ( record: MCUTimelineSheetRecord ) => [ record.TITLE, record ] ) )
// We want to keep existing types if they are just '{}'
const mergedRecords: MCUTimelineSheetRecord[] = fetchedSheet.records.map( ( newRecord: MCUTimelineSheetRecord ) => {
// Check if the new record has '{}' as the type
const hasEmptyType = typeof newRecord.TYPE !== 'string'
const existingRecord = recordsByTitle.get( newRecord.TITLE )
// If it does have an empty type, and there is an existing record, use the existing record's type
if ( hasEmptyType && existingRecord ) {
return {
...newRecord,
TYPE: existingRecord.TYPE || JSON.stringify( existingRecord.TYPE ),
}
}
return newRecord
} )
const mergedSheet = { records: mergedRecords }
// Write data to JSON
await fs.writeFile( `${ storePath }/mcu-timeline-sheet.json`, JSON.stringify( mergedSheet, null, 2 ) )
const orderedDetails = organizeOrderData( mergedSheet.records )
const matches = new Map()
// console.log('orderedDetails', orderedDetails)
const matchableOrderedTypes = new Set( [
'movie',
'disney-plus',
'disney-plus-netflix',
'abc',
'freeform',
'hulu',
'web-series',
'sony',
// 'whih',
// 'other'
] )
for ( const entry of Object.entries( orderedDetails ) ) {
const [
,
orderedDetails = null as any,
] = entry
// console.log('orderedDetails.timelineType', orderedDetails.timelineType)
// Skip entries not from matchable types
if ( !matchableOrderedTypes.has( orderedDetails.timelineType ) ) {
continue
}
// console.log('details', details )
for ( const listing of orderableListings.list ) {
const details: any = listingDetailsMap.get( listing.id )
const alreadyMatched = matches.has( listing.id )
if ( !alreadyMatched && matchListingToOrdered( listing, orderedDetails ) ) {
// console.log( 'Match!', 1, orderedDetails.title, 2, details.listing.title, getYearAndMonth( orderedDetails.premiereDate ) )
matches.set( details.listing.id, {
...orderedDetails,
listing,
} )
await upsertListingMarkdown( {
listing: {
id: details.listing.id,
slug: listing.slug,
overview: listing.sourceListing.overview,
title: listing.title,
mcuTimelineOrder: orderedDetails.mcuTimelineOrder,
},
tmdb: {},
readMarkdownFile: readMarkdownFileNode,
writeMarkdownFile: writeMarkdownFileNode,
exists: fs.exists,
} )
}
}
}
console.log( 'Updating README viewing-order-list' )
const updatedList = buildReadmeList( matches )
// // console.log( 'updatedList', updatedList )
await updateReadmeListContent( updatedList, 'viewing-order-list' )
console.log( 'Done' )
console.log( 'Updating README in-universe-list' )
const inUniverseList = await makeInUniverseMarkdown()
// console.log( 'updatedList', updatedList )
await updateReadmeListContent( inUniverseList, 'in-universe-list' )
console.log( 'Done' )
console.log( 'Updating README upcoming-list' )
const upcomingListings = await getUpcomingListings()
// Generate markdown
const newUpcomingMarkdown = makeUpcomingListingsMarkdown( upcomingListings )
await updateReadmeListContent( newUpcomingMarkdown, 'upcoming-list' )
console.log( 'Done' )
console.log( 'Saving Marvel Movies Fandom Timeline' )
try {
await saveMoviesFandomTimeline()
console.log( 'Done' )
}
catch ( error ) {
console.log( 'Error', error )
}
console.log( 'Adding any new filters as stories' )
await ensureFiltersHaveStories()
console.log( 'Done' )
console.log( 'Finished All README Updates' )
process.exit()
} )()