learncoder.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // "id" : "1009",
  2. // "type" : "choice",
  3. // "stem" : "计算机感染特洛伊木马后的典型现象是( )。",
  4. // "options" : {
  5. // "A" : "程序异常退出",
  6. // "B" : "有未知程序试图建立网络连接",
  7. // "C" : "邮箱被垃圾邮件填满",
  8. // "D" : "Windows 系统黑屏"
  9. // },
  10. // "answer" : "B",
  11. // "key_points" : "空",
  12. // "analysis" : "计算机感染特洛伊木马后,会受到特洛伊木马程序的控制,有以下几种典型现象。●有未知程序试图建立网络连接。●系统中有可疑的进程在运行等现象。●系统运行速度越来越慢,且CPU资源占用率高。●任务表中有可疑的文件在运行。●系统长时间读/写硬盘或搜索软盘。●系统经常死机或重启等。",
  13. // "tags" : null,
  14. // "knowledgeId" : [ "CF.NCB", "CF.SSDM" ],
  15. // "lastModified" : "2020-03-11 19:29:37",
  16. var request = require("request");
  17. var fs = require('fs');
  18. let startNum=0;
  19. var options = {
  20. url:'https://zhjts.learncoder.com:5555/question/api/v1/category/get/exercise?categoryId='+startNum
  21. };
  22. function get(opts) {
  23. return new Promise ((resolve, reject) => {
  24. request.get(opts,function(err,response,body){
  25. if(!err && response.statusCode == 200){
  26. if(body!=='null'){
  27. results=JSON.parse(body);
  28. resolve(results);
  29. }
  30. }
  31. });
  32. });
  33. }
  34. canhoice=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
  35. questionType = {
  36. "1":"choice",
  37. "0":"true_false",
  38. "2":"multiple_choice",
  39. "3":"blank",
  40. "4":"short_answer"
  41. }
  42. var categoryList=[];
  43. categoryList.push({
  44. id:0,
  45. name: "练习",
  46. parentId: 0,
  47. hasChild: true,
  48. orderNum: 0,
  49. hasNew: false
  50. });
  51. var totalList=[];
  52. let pormis= crawler(categoryList);
  53. pormis.then(result=>{
  54. console.log(totalList.length);
  55. let obj = {
  56. "totalList": totalList,
  57. "length": totalList.length
  58. };
  59. fs.writeFileSync('output.json', JSON.stringify(obj));
  60. })
  61. //使用它
  62. async function crawler(categoryList) {
  63. // console.log(categoryList[categoryList.length-1].name)
  64. let result = await get(buildCateOptions(categoryList[categoryList.length-1].id));
  65. for(let i of result.res){
  66. categoryList.push(i);
  67. if(i.hasChild){
  68. await crawler(categoryList);
  69. }else{
  70. let key_points="知识点";
  71. for(let cate of categoryList){
  72. key_points=key_points+"."+cate.name;
  73. }
  74. // totalList.push(key_points);
  75. let retlist = await getQuestion(i.id,key_points);
  76. for(let j of retlist){
  77. totalList.push(j);
  78. }
  79. }
  80. categoryList.pop();
  81. }
  82. return result;
  83. }
  84. function buildQuestionOptions(id){
  85. let option = {
  86. url:'https://zhjts.learncoder.com:5555/question/api/v1/exercise/questions/get/auth?page=0&size=1000&categoryId='+id
  87. };
  88. return option;
  89. }
  90. // getQuestion(94,"112231");
  91. async function getQuestion(id,key_points){
  92. let retList=[]
  93. let res = await get(buildQuestionOptions(id));
  94. for(let q of res.res.data){
  95. let newQ={
  96. };
  97. newQ.id="learncoder_"+id+"_"+q.id;
  98. newQ.stem=q.stem;
  99. newQ.key_points=key_points;
  100. if(q.analysis===null||q.analysis=="[analysis]空"){
  101. newQ.analysis='空';
  102. }else{
  103. newQ.analysis=q.analysis.replace("[analysis]","").replace("【考点】","");;
  104. }
  105. newQ.type=questionType[''+q.questionType];
  106. let ind;
  107. switch(q.questionType){
  108. case 0://判断题
  109. newQ.answer=q.answer;
  110. break;
  111. case 1://选择题
  112. newQ.options={}
  113. newQ.answer="";
  114. ind=0;
  115. for(let opt of q.options){
  116. let choice =canhoice[ind];
  117. ind++;
  118. let content= opt.content;
  119. if(opt.content.charAt(1)=='.'){
  120. choice = opt.content.slice(0,1)
  121. content= opt.content.slice(2)
  122. }
  123. newQ.options[choice]=content;
  124. if(opt.correct){
  125. newQ.answer=choice;
  126. }
  127. }
  128. break;
  129. case 2://多选题
  130. newQ.options={}
  131. newQ.answer="";
  132. ind=0;
  133. for(let opt of q.options){
  134. let choice =canhoice[ind];
  135. ind++;
  136. let content= opt.content;
  137. if(opt.content.charAt(1)=='.'){
  138. choice = opt.content.slice(0,1)
  139. content= opt.content.slice(2)
  140. }
  141. newQ.options[choice]=content;
  142. if(opt.correct){
  143. newQ.answer=newQ.answer+";"+choice;
  144. }
  145. }
  146. newQ.answer=newQ.answer.slice(1);
  147. break;
  148. case 3://填空题
  149. newQ.options={}
  150. q.answer="";
  151. for(let opt of q.blanks){
  152. let choice = opt.blankId
  153. let content= opt.blankAnswer[0]
  154. newQ.options[choice]=content;
  155. }
  156. q.answer=q.answer.slice(1);
  157. break;
  158. case 4://简答题
  159. newQ.answer=q.answer.replace("[answer_question_answer]","")
  160. break;
  161. default:
  162. console.log(q);
  163. throw new Error('questionType error='+q.questionType);
  164. }
  165. retList.push(newQ);
  166. }
  167. // console.log(retList);
  168. return retList;
  169. }
  170. function buildCateOptions(id){
  171. let option = {
  172. url:'https://zhjts.learncoder.com:5555/question/api/v1/category/get/exercise?categoryId='+id
  173. };
  174. return option;
  175. }