Skip to content

Commit 736e0ef

Browse files
committed
update generate.js
1 parent 3a85fd0 commit 736e0ef

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

generate.js

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ const eventproxy = require('eventproxy');
1010

1111
let CONFIG = {
1212
// cookie from -> https://leetcode.com/api/problems/algorithms/
13-
cookie: '',
14-
baseLocalSrc: '',
15-
markdownFileSrc: '',
13+
cookie: '__cfduid=d49cf8f975443623a42cea16ce958f1d51505577150; LEETCODE_SESSION=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImhhbnppY2hpIiwidXNlcl9zbHVnIjoiaGFuemljaGkiLCJfYXV0aF91c2VyX2lkIjoiMjA3MDc0IiwiUkVNT1RFX0FERFIiOiIxNzIuNjguMTMyLjUiLCJ0aW1lc3RhbXAiOiIyMDE3LTA5LTMwIDA4OjI0OjUzLjg0MjM5NCswMDowMCIsIl9hdXRoX3VzZXJfYmFja2VuZCI6ImRqYW5nby5jb250cmliLmF1dGguYmFja2VuZHMuTW9kZWxCYWNrZW5kIiwiaWQiOjIwNzA3NCwiX3Nlc3Npb25fZXhwaXJ5IjowLCJfYXV0aF91c2VyX2hhc2giOiI2NzczMjI0M2YwZjhjMmZlMDVhMDBkMmVjN2E3NjBjYjhjYWY3Y2I2IiwiZW1haWwiOiJiaWdiaWdzdW5yaXNlQGZveG1haWwuY29tIiwiSURFTlRJVFkiOiJkYzkxOTI4NmE3YjFhOTAxZWI4YzJlYjJhYWM2NzIwNiJ9.VISGbiIlFirZxAFBE-hS_WsRTc2bqoOu173RwdXe6RU; _ga=GA1.2.783262919.1488459467; _gid=GA1.2.1984063667.1507463694; csrftoken=ibul4lK9xgp6oErImXb2gf1ZJpGDTwZQku3pn0RQbqBsBL6etp7EuSEcuhBOVP3Q; __atuvc=0%7C37%2C0%7C38%2C5%7C39%2C5%7C40%2C3%7C41; __atuvs=59da2970e14d45b2000',
14+
// from local
15+
baseLocalSrc: 'Algorithms/',
16+
markdownFileSrc: 'readme.md',
17+
// from the net
1618
baseNetSrc: 'https://github.com/hanzichi/leetcode/blob/master/Algorithms/',
1719
api: 'https://leetcode.com/api/problems/algorithms/',
1820
ua: 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36'
@@ -23,7 +25,7 @@ function makeMarkdownFile(data) {
2325

2426
// use ES6 template string
2527
let tpl =
26-
`# Leetcode Solutions with JavaScript
28+
`# Leetcode Solutions with JavaScript [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
2729
2830
Update time: ${new Date}
2931
@@ -37,15 +39,23 @@ I have solved **${num_solved} / ${num_total}** problems while **${num_locked}**
3739
// sort by the problemId desc
3840
problems.sort((a, b) => b.problemId - a.problemId);
3941

40-
problems.forEach((item) => {
41-
let {problemId, title, url, language, sourceCode, explanation, difficulty, isSolved, isLocked} = item;
42+
problems.forEach(item => {
43+
let {problemId, title, url, languageJS, sourceCodeJS, languagePY, sourceCodePY, languageCPP, sourceCodeCPP, explanation, difficulty, isSolved, isLocked} = item;
4244
tpl += `| ${problemId} | [${title}](${url}) `;
4345

4446
if (isLocked)
4547
tpl += ':blue_book: ';
4648

4749
if (isSolved) {
48-
tpl += `| [${language}](${sourceCode}) `;
50+
let languageArray = [];
51+
if (languageJS)
52+
languageArray.push(`[${languageJS}](${sourceCodeJS})`)
53+
if (languagePY)
54+
languageArray.push(`[${languagePY}](${sourceCodePY})`)
55+
if (languageCPP)
56+
languageArray.push(`[${languageCPP}](${sourceCodeCPP})`)
57+
58+
tpl += '| ' + languageArray.join(' ') + ' '
4959
} else {
5060
tpl += '| ';
5161
}
@@ -73,28 +83,35 @@ function dealWithFile(data) {
7383
let ep = new eventproxy();
7484
let baseNetSrc = CONFIG.baseNetSrc;
7585

76-
ep.after('read', data.num_solved, (problems) => {
86+
ep.after('read', data.num_solved, problems => {
7787
resolve(data);
7888
});
7989

80-
data.problems.forEach((p) => {
90+
data.problems.forEach(p => {
8191
if (p.isSolved) {
82-
let fileSrc = CONFIG.baseLocalSrc + p.title;
92+
let fileSrc = CONFIG.baseLocalSrc + p.title.trim();
8393

8494
fs.readdir(fileSrc, (err, files) => {
95+
if (err) {
96+
console.error(fileSrc)
97+
return
98+
}
99+
85100
files.forEach((fileName) => {
86101
if (fileName.endsWith("md")) {
87102
p.explanation = encodeURI(baseNetSrc + p.title + '/' + fileName);
88103
} else {
89-
// language -> JavaScript/Python/C++
104+
// language -> JavaScript / Python / C++
90105
if (fileName.endsWith("js")) {
91-
p.language = "JavaScript";
106+
p.languageJS = "JavaScript";
107+
p.sourceCodeJS = encodeURI(baseNetSrc + p.title + '/' + fileName);
92108
} else if (fileName.endsWith("cpp")) {
93-
p.language = "C++";
109+
p.languageCPP = "C++";
110+
p.sourceCodeCPP = encodeURI(baseNetSrc + p.title + '/' + fileName);
94111
} else if (fileName.endsWith("py")) {
95-
p.language = "Python";
112+
p.languagePY = "Python";
113+
p.sourceCodePY = encodeURI(baseNetSrc + p.title + '/' + fileName);
96114
}
97-
p.sourceCode = encodeURI(baseNetSrc + p.title + '/' + fileName);
98115
}
99116
});
100117

@@ -115,7 +132,7 @@ function makeRequest() {
115132
let {stat_status_pairs, num_total, num_solved} = JSON.parse(res.text);
116133
let num_locked = 0;
117134

118-
let problems = stat_status_pairs.map((item) => {
135+
let problems = stat_status_pairs.map(item => {
119136
let obj = {
120137
isSolved: item.status === "ac",
121138
problemId: item.stat.question_id,
@@ -135,10 +152,10 @@ function makeRequest() {
135152
}
136153

137154
// Promise
138-
makeRequest().then((data) => {
155+
makeRequest().then(data => {
139156
return dealWithFile(data);
140-
}).then((data) => {
157+
}).then(data => {
141158
return makeMarkdownFile(data);
142-
}).then((msg) => {
159+
}).then(msg => {
143160
console.log(msg);
144161
});

0 commit comments

Comments
 (0)