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
|
import json import urllib import urllib2 import requests import re import url_utils from urllib import urlretrieve import oss2
QUESTION_BANK_ID=88 QUESTION_ADD_URL='http://47.99.50.105:8882/api/v1/questions/add' X_USERID='72' X_ORGANIZATION_ID = '21'
rootURL = 'http://miscrofile.oss-cn-hangzhou.aliyuncs.com/' targetFold = 'root/question/yuanketang/'
access_key_id = 'LTAILQg6DvGdPrFN' access_key_secret = 'pN6ScRagLy6k2dehw8BheFmNR7WDtZ' bucket_name = 'miscrofile' oss_url = 'http://oss-cn-hangzhou.aliyuncs.com'
def uploadFileViaSDK(imgName,targetFold): bucket = oss2.Bucket(oss2.Auth(access_key_id,access_key_secret), oss_url, bucket_name) key = targetFold+imgName bucket.put_object_from_file(key,imgName) result = bucket.get_object(key)
def getAndUploadPic(localUrl): img = re.match(r'.*/(.*png)',localUrl).group(1) urlretrieve(localUrl, './'+img) uploadFileViaSDK(img,targetFold) return rootURL+targetFold+img
def doReplace(line): pics = re.findall(r'http.*?png',line) for localUrl in pics: print 'do replace pic' line = re.sub(localUrl,getAndUploadPic(localUrl),line) print line return line
def replacePicUrl(question): questionJson = json.dumps(question) question = question.loads
def commonChange(question,categoryId):
del question['updateTime'] del question['id'] del question['version'] del question['disabled'] del question['author'] del question['label'] del question['reviewed'] del question['createTime'] question['questionBankId'] = QUESTION_BANK_ID question['knowledgePointIds']=[question['knowledgePoints'][0]['id']] del question['knowledgePoints'] for i in range(len(question['tags'])): del question['tags'][i]['id'] if question['difficulty'] == 'SIMPLE' : question['difficulty']=0 elif question['difficulty'] == 'NORMAL' : question['difficulty']=1 elif question['difficulty'] == 'HARD' : question['difficulty']=2 else: print 'error , unknown difficulty type' question['categoryId'] = categoryId
if question['questionType']=='TRUE_OR_FALSE': question['questionType']=0 elif question['questionType']=='SINGLE_CHOICE': question['questionType']=1 elif question['questionType']=='MULTIPLE_CHOICE': question['questionType']=2 elif question['questionType']=='FILL_IN_BLANK': question['questionType']=3 elif question['questionType']=='SUBJECTIVE': question['questionType']=4 else: print 'error , unknown question type'
def formatQuestion(question,categoryId): commonChange(question,categoryId)
question = json.loads(doReplace(json.dumps(question))) if question['questionType'] == 0: formatJudgeQuestion(question) elif question['questionType']==1: formatSingleChoiceQuestion(question) elif question['questionType']==2: formatMuitlChoiceQuestion(question) elif question['questionType']==3: formatFillQuestion(question) elif question['questionType']==4: formatAnswerQuestion(question) elif question['questionType']==5: formatCodeQuestion(question) else: print 'error can\'t parse' def formatSingleChoiceQuestion(question): print 'format 单选题' del question['answer'] for i in range(len(question['options'])): del question['options'][i]['id']
url_utils.pushData(json.dumps(question),QUESTION_ADD_URL,X_USERID,X_ORGANIZATION_ID)
def formatJudgeQuestion(question): print 'format 判断题' question['answer'] = question['answer']['value'] url_utils.pushData(json.dumps(question),QUESTION_ADD_URL,X_USERID,X_ORGANIZATION_ID) def formatMuitlChoiceQuestion(question): print 'format 多选题' del question['answer'] for i in range(len(question['options'])): del question['options'][i]['id'] url_utils.pushData(json.dumps(question),QUESTION_ADD_URL,X_USERID,X_ORGANIZATION_ID)
def formatFillQuestion(question): print 'format 填空题' parseStr = question['stem'] blankID='' blanks=[] while re.match(r'.*(__.{1}__).*',parseStr): blankID=re.match(r'.*(__.{1}__).*',parseStr).group(1) parseStr = re.sub(blankID,'',parseStr) blanks.append({ "blankAnswer":[question['blankItems'][blankID]['answer']['value']], "blankId":blankID })
question['blanks'] = json.dumps(blanks) url_utils.pushData(json.dumps(question),QUESTION_ADD_URL,X_USERID,X_ORGANIZATION_ID)
def formatAnswerQuestion(question): print 'format 简答题' try: question['answer']=question['answer']['value'] except Exception as e: print e print 'error no answer,and stem is: '+question['stem'] url_utils.pushData(json.dumps(question),QUESTION_ADD_URL,X_USERID,X_ORGANIZATION_ID)
def formatCodeQuestion(question): print 'format 代码题'
def parseQuestions(questionArray,categoryId): for i in range(len(questionArray)): load_json = json.loads(questionArray[i]) formatQuestion(load_json['res'],categoryId)
def doAction(getAllQuestionUrl,getSingleQuestionUrl,token,requirementBasicBody,categoryName): categoryId = url_utils.createCategory(categoryName,'http://47.99.50.105:8882/api/v1/category/add',X_USERID,X_ORGANIZATION_ID) questionArray = url_utils.getAndParseData(getAllQuestionUrl,getSingleQuestionUrl,token,requirementBasicBody) parseQuestions(questionArray,categoryId)
def main(): token = 'bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJtc2ciOiJzdWNjZXNzIiwiY29kZSI6MCwidXNlcl9uYW1lIjoicm9vdCIsInNjb3BlIjpbImFsbCIsInJlYWQiLCJ3cml0ZSJdLCJleHAiOjE1NTc3MjA1OTgsInVzZXJJZCI6MSwiYXV0aG9yaXRpZXMiOlsiUk9MRV9VU0VSIiwiUk9MRV9URUFDSEVSIl0sImp0aSI6ImEwY2MwYjkzLTZlNjUtNDBiNy05YjY3LWQ3OGI1NmQ3MGM0OCIsImNsaWVudF9pZCI6Inl1YW50dSJ9.adVGNhqKZm6rIeLnmmL3a1Jx8A5oDbf31Q5LNkI0Z-rIzsmYIJ15FNXKLmPO2_H2izr9VQ5vDirHoP9YY_jrPpZvtB57hTYP4n116yIjAtlScVQzVlHi_bwtZH7OSdbwcJ76nJzluvbD-xIk177i9tmMI3KjKrToPNHdIlLUGBMBo25RjplFig1MRrfvv-DAZnH7ymxoCGbNnnaGtKmYJIgCIk5IAAhekFaF-iKIyQT7bFMN5KYAM02YwvbWjQYs0C4l_nACZ-yG9KW24s5iepna44E_34UW4rsNzAdGods1P039CXrkVH83M14hxpI2qClmKZASmvWqAQizM1tF6Q' getAllQuestionUrl = 'http://192.168.0.100:18080/api/v1/questions/search/conditions' getSingleQuestionUrl = 'http://192.168.0.100:18080/api/v1/question/'
body = [] category = [] body.append('{"knowledgePointIds":[2415],"difficulties":null,"questionTypes":null,"knowledgeState":null,"pageSize":1000,"page":0,"keywords":""}') category.append('需求基础') body.append('{"knowledgePointIds":[3004,3006,3007,3008,3009,3010,3011,3020,3021,3035,3523],"difficulties":null,"questionTypes":null,"knowledgeState":null,"pageSize":1000,"page":0,"keywords":""}') category.append('OOP') body.append('{"knowledgePointIds":[3379],"difficulties":null,"questionTypes":null,"knowledgeState":null,"pageSize":1000,"page":0,"keywords":""}') category.append('职责分配') body.append('{"knowledgePointIds":[2749],"difficulties":null,"questionTypes":null,"knowledgeState":null,"pageSize":1000,"page":0,"keywords":""}') category.append('模块化') body.append('{"knowledgePointIds":[2457],"difficulties":null,"questionTypes":null,"knowledgeState":null,"pageSize":1000,"page":0,"keywords":""}') category.append('设计模式') body.append('{"knowledgePointIds":[2982],"difficulties":null,"questionTypes":null,"knowledgeState":null,"pageSize":1000,"page":0,"keywords":""}') category.append('起始体系结构构建') body.append('{"knowledgePointIds":[2673,2708,2715,2717,3054,3055,3056,2716],"difficulties":null,"questionTypes":null,"knowledgeState":null,"pageSize":1000,"page":0,"keywords":""}') category.append('OO分析') body.append('{"knowledgePointIds":[2673],"difficulties":null,"questionTypes":null,"knowledgeState":null,"pageSize":1000,"page":0,"keywords":""}') category.append('用例描述')
for i in range(len(body)): doAction(getAllQuestionUrl,getSingleQuestionUrl,token,body[i],category[i])
if __name__=="__main__": main()
|