Last active
January 1, 2017 00:39
-
-
Save deptno/fa3ff16b6b9596a1db2ab00dc906af63 to your computer and use it in GitHub Desktop.
fetch 4clojure problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const FormData = require('form-data'); | |
const fetch = require('node-fetch'); | |
const cheerio = require('cheerio'); | |
const problem = problem => new Promise(async (resolve, reject) => { | |
try { | |
const result = await fetch(`http://www.4clojure.com/problem/${problem}`); | |
const html = await result.text(); | |
const $ = cheerio.load(html); | |
const number = $('#prob-number').text(); | |
const title = $('#prob-title').text(); | |
const difficult = $('#tags>tr:nth-child(1)>td').text(); | |
const topic = $('#tags>tr:nth-child(2)>td').text(); | |
const description = $('#prob-desc').text(); | |
const testcases = $('.testcases tr').map((_, el) => $(el).find('pre').text()).get(); | |
resolve({number, title, difficult, topic, description, testcases}); | |
} catch (ex) { | |
console.error(ex); | |
resolve({}); | |
} | |
}); | |
const check = (problem, code) => { | |
const form = new FormData(); | |
form.append('id', problem); | |
form.append('code', code); | |
return fetch(`http://www.4clojure.com/rest/problem/${problem}`, form); | |
} | |
const print = ({number='#0', title='fail to fetch', difficult='', topic='', description='', testcases=''}) => `; ${number} ${title} | |
; | |
; ${difficult} | |
; ${topic} | |
; | |
; ${description.replace(/\n/gm, '\n; ')} | |
; | |
; ${testcases.join('\n; ')}`; | |
const printNo = async num => console.log(print(await problem(num))); | |
exports.check = check; | |
exports.print = print; | |
exports.printNo = printNo; | |
const no = process.argv[2]; | |
no && printNo(no); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "fa3ff16b6b9596a1db2ab00dc906af63", | |
"version": "1.0.0", | |
"main": "index.js", | |
"repository": "https://gist.github.com/fa3ff16b6b9596a1db2ab00dc906af63.git", | |
"author": "bglee <[email protected]>", | |
"license": "MIT", | |
"dependencies": { | |
"cheerio": "^0.22.0", | |
"form-data": "^2.1.2", | |
"node-fetch": "^1.6.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment