1
1
const fs = require ( "fs" ) ;
2
2
const path = require ( "path" ) ;
3
+ const fetch = require ( 'node-fetch' )
3
4
5
+ const { leetcodeConfig :{ allProblem } } = require ( '../../config/index' )
4
6
const { encrypt } = require ( "../../utils/crypto.js" ) ;
5
7
6
8
const solutions = require ( "./solutions.json" ) ;
9
+ let lcProblemIdMap = { }
7
10
8
11
function toArray ( sep = "-" , txt ) {
9
12
if ( ! txt ) return txt ;
@@ -50,6 +53,15 @@ function matchWioutPaddingLine(reg, txt) {
50
53
) ;
51
54
}
52
55
56
+ function getQuestionId ( link = "" ) {
57
+ if ( ! link ) return null
58
+ let slug = link
59
+ . split ( '/' )
60
+ . reverse ( )
61
+ . find ( item => item )
62
+ return lcProblemIdMap [ slug ]
63
+ }
64
+
53
65
function generate ( rawMD , rawMDBuffer , i ) {
54
66
const regs = {
55
67
...getSatelliteDataReg ( ) ,
@@ -73,8 +85,9 @@ function generate(rawMD, rawMDBuffer, i) {
73
85
description,
74
86
content : encrypt ( rawMDBuffer ) ,
75
87
title,
76
- link,
88
+ link
77
89
} ;
90
+ solutions [ i ] [ 'question_id' ] = getQuestionId ( link ) || solutions [ i ] [ 'question_id' ]
78
91
}
79
92
// 基础篇
80
93
function generateBasic ( ) {
@@ -113,8 +126,36 @@ function generateAdvance() {
113
126
} ) ;
114
127
}
115
128
116
- generateBasic ( ) ;
117
- generateTopic ( ) ;
118
- generateAdvance ( ) ;
129
+ function getLcProblemIdMap ( ) {
130
+ return fetch ( allProblem )
131
+ . then ( res => res . json ( ) )
132
+ . then ( res => {
133
+ let result = { }
134
+ let data = res . stat_status_pairs
135
+ if ( data ) {
136
+ result = data . reduce ( ( pre , item ) => {
137
+ let { stat : { question__title_slug, question_id } = { } } = item || { }
138
+ if ( question__title_slug && question_id ) {
139
+ pre [ question__title_slug ] = question_id
140
+ }
141
+ return pre
142
+ } , { } )
143
+ }
144
+ return result
145
+ } )
146
+ }
147
+
148
+ async function main ( ) {
149
+ try {
150
+ lcProblemIdMap = await getLcProblemIdMap ( )
151
+ } catch ( err ) {
152
+ console . log ( err ) ;
153
+ }
154
+ generateBasic ( ) ;
155
+ generateTopic ( ) ;
156
+ generateAdvance ( ) ;
157
+
158
+ fs . writeFileSync ( __dirname + "/solutions.json" , JSON . stringify ( solutions ) ) ;
159
+ }
119
160
120
- fs . writeFileSync ( __dirname + "/solutions.json" , JSON . stringify ( solutions ) ) ;
161
+ main ( )
0 commit comments