├── .gitignore ├── ChaoXing.py ├── LICENSE ├── README.md ├── Util.py ├── config.default.yml ├── main.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__ 2 | cookies.json 3 | config.yml -------------------------------------------------------------------------------- /ChaoXing.py: -------------------------------------------------------------------------------- 1 | # /usr/bin/python3 2 | # coding=utf8 3 | 4 | import re 5 | import html 6 | import json 7 | import base64 8 | import hashlib 9 | import requests 10 | from urllib.parse import quote 11 | from Util import get_timestamp, obj2str 12 | 13 | """ 14 | 超星学习通操作类 15 | """ 16 | class ChaoXing: 17 | 18 | s = requests.session() 19 | 20 | token = "4faa8662c59590c6f43ae9fe5b002b42" 21 | 22 | uid = "" 23 | 24 | def __init__(self): 25 | # 设置全局Http协议头 26 | self.s.headers.update( 27 | { 28 | 'Accept': "*/*", 29 | 'Accept-Language': "zh-Hans-CN;q=1", 30 | 'Connection': "keep-alive", 31 | 'Accept-Encoding': "gzip, deflate, br", 32 | 'User-Agent': "Mozilla/5.0 (iPhone; CPU iPhone OS 13_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 ChaoXingStudy/ChaoXingStudy_3_4.4_ios_phone_202004071850_39 (@Kalimdor)_15167665855436465586 ChaoXingStudy/ChaoXingStudy_3_4.4_ios_phone_202004071850_39 (@Kalimdor)_15167665855436465586" 33 | }) 34 | 35 | # 设置Cookie 36 | def set_cookie(self, ck): 37 | obj = json.loads(ck) 38 | 39 | cookies = {} 40 | for o in obj: 41 | cookies[o[0]] = o[1] 42 | 43 | self.s.cookies.update(cookies) 44 | 45 | # 获得验证码 46 | def getCode(self, name): 47 | uri = "http://passport2.chaoxing.com/num/code?" + str(get_timestamp()) 48 | 49 | r = self.s.get(uri) 50 | 51 | with open(name, 'wb') as fd: 52 | for chunk in r.iter_content(): 53 | fd.write(chunk) 54 | 55 | # 执行登陆; Web网页接口 56 | def login(self, u_name, u_pass, code): 57 | 58 | headers = { 59 | 'Content-Type': "application/x-www-form-urlencoded", 60 | 'Accept-Encoding': 'gzip, deflate, br', 61 | 'Origin': "http://i.mooc.chaoxing.com/", 62 | 'Referer': "http://passport2.chaoxing.com/login?fid=&refer=http://i.mooc.chaoxing.com" 63 | } 64 | 65 | uri = "http://passport2.chaoxing.com/login?refer=" + \ 66 | quote("http://i.mooc.chaoxing.com", "utf-8") 67 | 68 | data = obj2str({ 69 | 'refer_0x001': quote("http://i.mooc.chaoxing.com", "utf-8"), 70 | 'pid': "-1", 71 | 'pidName': "", 72 | 'fid': "-1", 73 | 'fidName': "", 74 | 'allowJoin': "0", 75 | 'isCheckNumCode': "1", 76 | 'f': "0", 77 | 'productid': "", 78 | 't': "true", 79 | 'uname': u_name, 80 | 'password': base64.b64encode(u_pass.encode("utf-8")).decode('ascii'), 81 | 'numcode': code, 82 | 'verCode': "" 83 | }) 84 | 85 | r = self.s.post(uri, headers=headers, data=data, allow_redirects=False) 86 | 87 | if r.status_code != 302: 88 | rule = r'id=\"show_error\">(.*?)' 89 | d = re.findall(rule, r.text) 90 | print(d[0]) 91 | return False 92 | else: 93 | return True 94 | 95 | # 执行登陆: Mobile端 96 | # 优点是可以免验证码登陆 97 | def login_m(self, u_name, u_pass): 98 | 99 | uri = "https://passport2-api.chaoxing.com/v11/loginregister?code=" + u_pass + \ 100 | "&cx_xxt_passport=json&uname=" + u_name + "&loginType=1&roleSelect=true" 101 | 102 | r = self.s.get(uri) 103 | 104 | a = json.loads(r.text) 105 | 106 | if a['status']: 107 | print("✅ %s" % a['mes']) 108 | else: 109 | print("❌ %s" % a['mes']) 110 | 111 | return a['status'] 112 | 113 | # 获取用户信息 114 | def get_user_info(self): 115 | r = self.s.get("https://sso.chaoxing.com/apis/login/userLogin4Uname.do") 116 | if r.text != "": 117 | o = json.loads(r.text) 118 | if o['result'] == 1: 119 | puid = o['msg']['puid'] 120 | _time = str(get_timestamp()) 121 | enc = "token={0}&_time={1}&puid={2}&myPuid={3}&DESKey={4}".format( 122 | self.token, _time, puid, puid, "Z(AfY@XS") 123 | enc = hashlib.md5(enc.encode("utf-8")).hexdigest() 124 | url = "https://useryd.chaoxing.com/apis/user/getUser?token={0}&_time={1}&puid={2}&myPuid={3}&inf_enc={4}".format(self.token, _time, puid, puid, enc) 125 | r = self.s.get(url) 126 | if r.text != "": 127 | o = json.loads(r.text) 128 | if o['result'] == 1: 129 | self.uid = o['msg']['puid'] 130 | return o['msg'] 131 | 132 | return None 133 | 134 | # 获取课程列表 135 | def get_course_list(self): 136 | if self.uid == "": 137 | self.get_user_info() 138 | r = self.s.get("http://mooc1-api.chaoxing.com/mycourse/backclazzdata?view=json&rss=1") 139 | if r.text != "": 140 | o = json.loads(r.text) 141 | if o['result'] == 1: 142 | ret = [] 143 | for item in o['channelList']: 144 | for item2 in item['content']['course']['data']: 145 | ret.insert(0, { 146 | "id": item['id'], 147 | "courseId": item2['id'], 148 | "courseName": item2['name'], 149 | "cpi": item['cpi'], 150 | "cataName": item['cataName'], 151 | "clazzid": item['key'], 152 | "isstart": item['content']['isstart'], 153 | "state": item['content']['state'] 154 | }) 155 | 156 | return ret 157 | return None 158 | 159 | # 获取课程目录 160 | def get_course_cata(self, clazzid, cpi): 161 | 162 | url = "https://mooc1-api.chaoxing.com/gas/clazz?id={0}&personid={1}&fields=id,bbsid,classscore,isstart,allowdownload,chatid,name,state,isthirdaq,isfiled,information,discuss,visiblescore,begindate,coursesetting.fields(id,courseid,hiddencoursecover,hiddenwrongset,coursefacecheck),course.fields(id,name,infocontent,objectid,app,bulletformat,mappingcourseid,imageurl,knowledge.fields(id,name,indexOrder,parentnodeid,status,layer,label,begintime,endtime,attachment.fields(id,type,objectid,extension).type(video)))&view=json" \ 163 | .format(clazzid, cpi) 164 | 165 | r = self.s.get(url) 166 | 167 | if r.text != "": 168 | o = json.loads(r.text) 169 | 170 | ret = {} 171 | for item in o['data'][0]['course']['data'][0]['knowledge']['data']: 172 | if not item['layer'] in ret: 173 | ret[item['layer']] = [] 174 | ret[item['layer']].insert(len(ret[item['layer']]), { 175 | 'id': item['id'], 176 | 'parentnodeid': item['parentnodeid'], 177 | 'name': item['name'], 178 | 'label': item['label'], 179 | 'data': { 180 | "clickcount": 0, 181 | "finishcount": 0, 182 | "totalcount": 0, 183 | "openlock": 0, 184 | "unfinishcount": 0 185 | } 186 | }) 187 | 188 | 189 | nodes = [] 190 | for item in o['data'][0]['course']['data'][0]['knowledge']['data']: 191 | if item['layer'] > 1: 192 | nodes.insert(0, item['id']) 193 | taskInfo = self.get_task_finish_status(o['data'][0]['id'], cpi, nodes, o['data'][0]['course']['data'][0]['id']) 194 | for key, value in taskInfo.items(): 195 | for key2, value2 in ret.items(): 196 | for key3, value3 in enumerate(value2): 197 | if str(value3['id']) == key: 198 | ret[key2][key3]['data']['clickcount'] = value['clickcount'] 199 | ret[key2][key3]['data']['finishcount'] = value['finishcount'] 200 | ret[key2][key3]['data']['totalcount'] = value['totalcount'] 201 | ret[key2][key3]['data']['openlock'] = value['openlock'] 202 | ret[key2][key3]['data']['unfinishcount'] = value['unfinishcount'] 203 | 204 | return ret 205 | 206 | return None 207 | 208 | # 获取任务完成数量 209 | def get_task_finish_status(self, clazzid, cpi, nodes, courseid): 210 | 211 | headers = { 212 | 'Content-Type': "application/x-www-form-urlencoded", 213 | 'Accept-Encoding': 'gzip, deflate, br', 214 | 'Origin': "http://i.mooc.chaoxing.com/", 215 | 'Referer': "http://passport2.chaoxing.com/login?fid=&refer=http://i.mooc.chaoxing.com" 216 | } 217 | 218 | node = ','.join([str(i) for i in nodes]) 219 | 220 | data = obj2str({ 221 | 'clazzid': clazzid, 222 | 'userid': self.uid, 223 | 'view': 'json', 224 | 'cpi': cpi, 225 | 'nodes': node, 226 | 'courseid': courseid 227 | }) 228 | 229 | r = self.s.post("https://mooc1-api.chaoxing.com/job/myjobsnodesmap", headers=headers, data=data) 230 | if r.text != "": 231 | return json.loads(r.text) 232 | return None 233 | 234 | # 获取子节点任务分页 235 | def get_task_page(self, id, courseid): 236 | 237 | _time = str(get_timestamp()) 238 | enc = "token={0}&_time={1}&DESKey={2}".format( 239 | self.token, _time, "Z(AfY@XS") 240 | enc = hashlib.md5(enc.encode("utf-8")).hexdigest() 241 | 242 | url = "https://mooc1-api.chaoxing.com/gas/knowledge?id={0}&courseid={1}&fields=name,id,card.fields(id,title,cardorder,description)&view=json&token={2}&_time={3}&inf_enc={4}" \ 243 | .format(id, courseid, self.token, _time, enc) 244 | 245 | r = self.s.get(url) 246 | 247 | if r.text != "": 248 | o = json.loads(r.text) 249 | if len(o['data']) != 0: 250 | return o['data'][0]['card']['data'] 251 | 252 | return None 253 | 254 | # 获取分页的分级 255 | # 有时一个分页不止一个任务点 256 | def get_task_page_level(self, clazzid, courseId, knowledgeid, cpi, n): 257 | 258 | url = "https://mooc1-api.chaoxing.com/knowledge/cards?clazzid={0}&courseid={1}&knowledgeid={2}&num={3}&isPhone=1&control=true&cpi={4}" \ 259 | .format(clazzid, courseId, knowledgeid, n, cpi) 260 | 261 | r = self.s.get(url) 262 | 263 | mArg = re.findall(r'mArg = (.*?)\;', r.text) 264 | 265 | for d in mArg: 266 | if d != "\"\"": 267 | mArg = d 268 | break 269 | try: 270 | mArg = json.loads(mArg) 271 | except TypeError: 272 | return None 273 | except json.JSONDecodeError: 274 | return None 275 | 276 | return mArg 277 | 278 | # 获取课程资源信息 279 | def get_course_data(self, objectid): 280 | 281 | uri = "https://mooc1-1.chaoxing.com/ananas/status/%s?k=2041&flag=normal&_dc=%d" % ( 282 | objectid, get_timestamp()) 283 | 284 | r = self.s.get(uri) 285 | if r.text != "": 286 | return json.loads(r.text) 287 | return None 288 | 289 | # 上报数据; 用于表示用户正在播放视频 290 | def update_log_video(self, reportUrl, clazzId, playingTime, duration, dtoken, objectId, otherInfo, jobId, userid): 291 | clipTime = "0_%s" % duration 292 | enc = "[{0}][{1}][{2}][{3}][{4}][{5}][{6}][{7}]".format( 293 | clazzId, userid, jobId, objectId, playingTime * 1000, "d_yHJ!$pdA~5", duration * 1000, clipTime) 294 | 295 | uri = "{0}{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}".format( 296 | reportUrl, "/", dtoken, "?clazzId=", clazzId, "&playingTime=", playingTime, "&duration=", duration, "&clipTime=", clipTime, "&objectId=", objectId, "&otherInfo=", otherInfo, "&jobid=", jobId, "&userid=", userid, "&isdrag=", 0, "&view=pc", "&enc=", hashlib.md5(enc.encode(encoding='UTF-8')).hexdigest(), "&rt=", 0.9, "&dtype=Video", "&_t=", get_timestamp()) 297 | 298 | r = self.s.get(uri) 299 | 300 | if r.text != "": 301 | ret = json.loads(r.text) 302 | return ret['isPassed'] 303 | 304 | return None 305 | 306 | # 数据上报完成; 用于pptx 307 | def updata_log_ppt(self, jobId, knowledgeid, courseId, clazzid, jtoken): 308 | 309 | uri = "https://mooc1-1.chaoxing.com/ananas/job/document?jobid=%s&knowledgeid=%s&courseid=%s&clazzid=%s&jtoken=%s&_dc=%d" % ( 310 | jobId, knowledgeid, courseId, clazzid, jtoken, get_timestamp()) 311 | 312 | r = self.s.get(uri) 313 | if r.text != "": 314 | o = json.loads(r.text) 315 | print(o['msg']) 316 | return o['status'] 317 | return None -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 超星学习通自助执行程序 2 | > *说明*:**本程序仅供学习和参考,禁止用于商业或违法犯罪用途,请于24小时内删除!** 3 | 4 | 5 | *配置* 6 | 7 | > 运行前请先将文件 **config.default.yml** 拷贝后的文件重命名为 **config.yml** 8 | > 再修改 **config.yml** 内的配置信息 9 | 10 | 11 | 12 | *运行* 13 | 14 | > ⚠️ 运行前请先确认环境是否为 ***Python 3.8*** 15 | 16 | ```shell 17 | git clone https://github.com/shanling2016/ChaoXing && cd ChaoXing/ 18 | ``` 19 | > 安装依赖库 20 | 21 | ```shell 22 | pip3 install -r requirements.txt 23 | ``` 24 | > 运行程序 25 | ```shell 26 | # 带提示运行 27 | python3 main.py 28 | 29 | # 输出课程列表 30 | python3 main.py --list 31 | 32 | # 执行课程任务 33 | python3 main.py --id <课程列表序号id> 34 | ``` 35 | 36 | 37 | 38 | ### 关于作者 39 | 40 | 作者还只是 19 级的大学生啦~ 41 | 42 | 酱紫~ 43 | -------------------------------------------------------------------------------- /Util.py: -------------------------------------------------------------------------------- 1 | # /usr/bin/python3 2 | # coding=utf8 3 | 4 | """ 5 | 工具集 6 | """ 7 | 8 | import time 9 | 10 | # 获取时间戳 11 | def get_timestamp(): 12 | timestamp = time.time() 13 | return int(timestamp * 1000) 14 | 15 | # json转字符 16 | def obj2str(obj): 17 | ret = "" 18 | for key in obj: 19 | if ret == "": 20 | ret = key + "=" + str(obj[key]) 21 | else: 22 | ret = ret + "&" + key + "=" + str(obj[key]) 23 | return ret 24 | 25 | def print_list(obj, isExit = True): 26 | if len(obj) == 0: 27 | return 28 | print("------------------------------------") 29 | print("| id | 课程名称") 30 | index = 0 31 | for item in obj: 32 | print("| %2d | %-s" % (index, item['courseName'])) 33 | index = index + 1 34 | print("------------------------------------") 35 | if isExit: 36 | print("| 退出请输入 -1") 37 | print("------------------------------------") 38 | 39 | def print_tree(obj): 40 | if len(obj) == 0: 41 | return 42 | 43 | index = 0 44 | kv = {} 45 | print_list = [] 46 | for key, value in obj.items(): 47 | index2 = 0 48 | for key2, value2 in enumerate(value): 49 | head_list = [ " " for i in range(key) ] 50 | endi = len(obj[1]) - 1 51 | if index == 0 and index2 + 1 == len(obj[1]): 52 | head_list[0] = "└" 53 | elif index == 0 and index2 == 0: 54 | head_list[0] = "┌" 55 | elif key == 1: 56 | head_list[0] = "├" 57 | elif value2['parentnodeid'] != obj[1][endi]['id']: 58 | head_list[0] = "|" 59 | if key > 1: 60 | if key2 + 1 >= len(value) or value2['parentnodeid'] != value[key2+1]['parentnodeid']: 61 | head_list[key-1] = "└" 62 | else: 63 | head_list[key-1] = "├" 64 | head = " ".join(head_list) 65 | 66 | if key == 1: 67 | print_list.insert(len(print_list), { 68 | "title": "%s %s、%s" % (head, value2['label'], value2['name']), 69 | "data": [] 70 | }) 71 | kv[value2['id']] = len(print_list) 72 | else: 73 | length = len(print_list[kv[value2['parentnodeid']] - 1]['data']) 74 | if value2['data']['totalcount'] == 0: 75 | emoji = "🔒" 76 | elif value2['data']['unfinishcount'] == value2['data']['totalcount']: 77 | emoji = "❌ %d" % value2['data']['totalcount'] 78 | elif value2['data']['unfinishcount'] == 0: 79 | emoji = "✅" 80 | else: 81 | emoji = "⏳ %d" % value2['data']['unfinishcount'] 82 | print_list[kv[value2['parentnodeid']] - 1]['data'].insert(length, { 83 | "title": "%s %s、%s (%s)" % (head, value2['label'], value2['name'], emoji) 84 | }) 85 | 86 | index2 = index2 + 1 87 | index = index + 1 88 | for item in print_list: 89 | print(item['title']) 90 | for item2 in item['data']: 91 | print(item2['title']) -------------------------------------------------------------------------------- /config.default.yml: -------------------------------------------------------------------------------- 1 | # ______ ______ __ 2 | # / \ / \ | \ 3 | # | $$$$$$\ ______ _______ | $$$$$$\ \$$ ______ 4 | # | $$ \$$ / \ | \ | $$_ \$$| \ / \ 5 | # | $$ | $$$$$$\| $$$$$$$\| $$ \ | $$| $$$$$$\ 6 | # | $$ __ | $$ | $$| $$ | $$| $$$$ | $$| $$ | $$ 7 | # | $$__/ \| $$__/ $$| $$ | $$| $$ | $$| $$__| $$ 8 | # \$$ $$ \$$ $$| $$ | $$| $$ | $$ \$$ $$ 9 | # \$$$$$$ \$$$$$$ \$$ \$$ \$$ \$$ _\$$$$$$$ 10 | # | \__| $$ 11 | # \$$ $$ 12 | # \$$$$$$ 13 | # Title: 程序运行配置 14 | # Warning: 本程序仅供学习和参考,禁止用于商业或违法犯罪用途! 15 | 16 | # 网课网站的账号信息 17 | member: 18 | # 账号 19 | user: xxx 20 | 21 | # 密码 22 | pass: xxx 23 | 24 | # 保存 Cookies 25 | # 当登陆成功后,保存 Cookies 到本地,当下次运行时 26 | # 若 Cookies 有效, 则使用 Cookies 进行登陆 27 | saveCookies: false 28 | 29 | # 【学小易】搜题App 的账号信息 30 | store: 31 | # 账号 32 | user: xxx 33 | 34 | # 密码 35 | pass: xxx -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | # /usr/bin/python3 2 | # coding=utf-8 3 | 4 | import os 5 | import re 6 | import sys 7 | import yaml 8 | import json 9 | import math 10 | import time 11 | import getopt 12 | import random 13 | 14 | from ChaoXing import ChaoXing 15 | from alive_progress import alive_bar 16 | from Util import print_list, print_tree, get_timestamp 17 | 18 | # 初始化网课操作对象 19 | obj = ChaoXing() 20 | 21 | # 配置文件常量 22 | config = {} 23 | 24 | # 读取配置并登陆 25 | def login(): 26 | 27 | try: 28 | global config 29 | # 读取配置文件 30 | with open("config.yml", "r", encoding='utf-8') as f: 31 | data = f.read() 32 | # 加载配置文件 33 | config = yaml.safe_load(data) 34 | except IOError: 35 | print("❌ 初始化时出现错误:没找到配置文件!") 36 | exit(-1) 37 | except yaml.YAMLError: 38 | print("❌ 初始化时出现错误:配置文件异常!") 39 | exit(-2) 40 | 41 | # 登陆MChaoXing平台 42 | # 先判断有没有缓存Cookie 43 | if os.path.exists("cookies.json"): 44 | with open("cookies.json", "r", encoding='utf-8') as f: 45 | js = f.read() 46 | # 设置 Cookies 47 | obj.set_cookie(js) 48 | 49 | # 取一下数据,查看 Cookies 是否有效 50 | if len(obj.s.cookies.items()) == 0 or obj.get_user_info() == None: 51 | # 清空Cookies 52 | obj.s.cookies.clear() 53 | # 登陆 54 | if obj.login_m(str(config['member']['user']), str(config['member']['pass'])): 55 | if config['saveCookies']: 56 | # 获取 Cookies 57 | ck = json.dumps(obj.s.cookies.items()) 58 | # 保存到文件 59 | f = open("cookies.json", "w", encoding='utf-8') 60 | f.write(ck) 61 | f.close() 62 | else: 63 | print("🚫 登陆失败!") 64 | exit(-3) 65 | 66 | # 获取课程列表 67 | def getCourseList(): 68 | # 登陆 69 | login() 70 | # 获取 71 | course = obj.get_course_list() 72 | # 输出 73 | print_list(course, False) 74 | 75 | # 遍历目录执行自动化操作 76 | def eachProcessList(course, cata, cpi, clazzid, courseId): 77 | 78 | # 遍历目录; 判断是否有需要进行的课程 79 | # 定义个索引 80 | for key, item in cata.items(): 81 | # 只取子节点 82 | if key > 1: 83 | for key, item2 in enumerate(item): 84 | # 查询剩余任务数量 85 | if item2['data']['unfinishcount'] <= 0: 86 | continue 87 | # 取任务节点任务分页 88 | page = obj.get_task_page(item2['id'], courseId) 89 | # 遍历分页 90 | for item3 in page: 91 | mArg = obj.get_task_page_level(clazzid, courseId, item2['id'], cpi, item3['cardorder']) 92 | # 如果提取失败, 则跳转到下一个任务 93 | if mArg == None: 94 | continue 95 | # 提取任务点 96 | # 除文字分页外,其他类型的分页均存在数据 97 | for item4 in mArg['attachments']: 98 | # 判断该任务点的完成状态 99 | finish = not ("job" in item4 and item4['job']) 100 | # 如果完成; 就跳转到下一个任务 101 | if finish: 102 | continue 103 | 104 | # 没完成; 就给模拟操作完成 105 | # 先获取任务的类型 106 | task_type = item4['type'] 107 | 108 | print("\n💼 任务类型: %s" % task_type) 109 | 110 | if task_type == "video": 111 | # 获取视频任务的对象ID 112 | objectId = item4['objectId'] 113 | # 获取视频的详细信息 114 | c_data = obj.get_course_data(objectId) 115 | # 获取视频的长度; 单位秒 116 | duration = c_data['duration'] 117 | print("📺 视频类任务 《%s - %s [%s]》" % (item2['name'], item3['title'], c_data['filename'])) 118 | print("⏰ 视频时长: %.2f 分钟" % (duration / 60)) 119 | print("⏳ 正在自动完成……") 120 | 121 | # 开始进行模拟上报数据 122 | # 计数变量 123 | index = 0 124 | # 上报间隔时间 125 | delay = 30 126 | 127 | # 进度条 128 | with alive_bar(duration) as bar: 129 | while True: 130 | # 加个判断; 避免数据上报的时间溢出视频本身的长度 131 | if index * delay > duration: 132 | times = duration 133 | else: 134 | times = index * delay 135 | 136 | c_res = obj.update_log_video(mArg['defaults']['reportUrl'], mArg['defaults']['clazzId'], times, c_data['duration'], c_data['dtoken'], objectId, item4['otherInfo'], item4['jobid'], mArg['defaults']['userid']) 137 | if c_res and index * delay > duration: 138 | break 139 | 140 | if duration - times < delay: 141 | items = range(duration - times) 142 | for item in items: 143 | bar() 144 | time.sleep(1) 145 | else: 146 | items = range(delay) 147 | for item in items: 148 | bar() 149 | time.sleep(1) 150 | index = index + 1 151 | # 输出; 跳转到下一个循环 152 | print("🎉 视频 任务完成!") 153 | continue 154 | elif task_type == 'document': 155 | print("📽 文档/课件 观看任务") 156 | # 上报数据 157 | obj.updata_log_ppt(item4['jobid'], str(mArg['defaults']['knowledgeid']), str(mArg['defaults']['courseid']), str(mArg['defaults']['clazzId']), item4['jtoken']) 158 | # 输出; 跳转到下一个循环 159 | print("🎉 文档/课件 任务完成!") 160 | elif task_type == "workid": 161 | print("📃 测验 《%s - %s》" % (item2['name'], item3['title'])) 162 | print("⚠️ 已自动跳过!") 163 | pass 164 | else: 165 | print("❌ 不支持的任务类型!") 166 | print("⚠️ 已自动跳过!") 167 | 168 | time.sleep(2) 169 | 170 | print("\n🎉 你已完成了本课的所有任务!") 171 | 172 | # 执行自动化代码 173 | def chaoxingAuto(i): 174 | # 登陆 175 | login() 176 | # 获取 177 | course = obj.get_course_list() 178 | # 验证 179 | try: 180 | # 转换 181 | id = int(i) 182 | except ValueError: 183 | print("🚫 您输入的数据不符合规范!") 184 | exit(-4) 185 | if id >= len(course) or id < 0: 186 | print("🚫 课程id不存在!") 187 | exit(-5) 188 | 189 | # 输出选中的课程名称 190 | print("\n<%s>" % course[id]['courseName']) 191 | 192 | # 获取课程目录 193 | cata = obj.get_course_cata(course[id]['clazzid'], course[id]['cpi']) 194 | # 执行自动化 195 | eachProcessList(course, cata, course[id]['cpi'], course[id]['clazzid'], course[id]['courseId']) 196 | 197 | # 执行默认程序 198 | def chaoxingDefault(): 199 | 200 | # 登陆 201 | login() 202 | 203 | print("✅ 登陆成功!") 204 | print("⏳ 正在获取课程列表……") 205 | course = obj.get_course_list() 206 | 207 | # 输出 208 | print_list(course) 209 | 210 | while True: 211 | # 异常输入判断 212 | try: 213 | # 要求输入 214 | id = int(input("课程id: ")) 215 | except ValueError: 216 | print("🚫 您输入的数据不符合规范!") 217 | continue 218 | if id == -1: 219 | exit(0) 220 | if id >= len(course) or id < 0: 221 | print("🚫 课程id不存在!") 222 | continue 223 | break 224 | # 输出选中的课程名称 225 | print("\n<%s>" % course[id]['courseName']) 226 | # 获取课程目录 227 | cata = obj.get_course_cata(course[id]['clazzid'], course[id]['cpi']) 228 | # 输出目录 229 | print_tree(cata) 230 | # 执行自动化 231 | eachProcessList(course, cata, course[id]['cpi'], course[id]['clazzid'], course[id]['courseId']) 232 | 233 | if __name__ == "__main__": 234 | 235 | try: 236 | opts, args = getopt.getopt(sys.argv[1:], "i:l", ["id=", "list"]) 237 | except getopt.GetoptError: 238 | print( 239 | """usage: main.py --id 240 | --list ...get course list""" 241 | ) 242 | exit(2) 243 | 244 | for opt, arg in opts: 245 | if opt in ("-h", "--help"): 246 | print('main.py --id \ 247 | --list') 248 | exit(-1) 249 | elif opt in ("-i", "--id"): 250 | chaoxingAuto(arg) 251 | exit(0) 252 | elif opt in ("-l", "--list"): 253 | getCourseList() 254 | exit(0) 255 | 256 | chaoxingDefault() -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | requests==2.25.1 2 | alive_progress==1.6.2 3 | PyYAML==5.4.1 4 | --------------------------------------------------------------------------------