Created
March 5, 2020 10:00
-
-
Save CubicPill/64feb74f0711204caf8f6e856c6776c2 to your computer and use it in GitHub Desktop.
Tencent Lexiang course
This file contains 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
import json | |
import random | |
from urllib import parse | |
import requests | |
from bs4 import BeautifulSoup | |
CLASS_ID = 'CLASS_ID_HERE' | |
COOKIES = 'YOUR_COOKIES_HERE' | |
UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36' | |
course_url = 'https://lexiangla.com/api/v1/classes/' + CLASS_ID | |
study_url = 'https://lexiangla.com/api/v1/classes/' + CLASS_ID + '/courses/{courseid}/study' | |
def main(): | |
session = requests.session() | |
session.headers.update({ | |
'user-agent': UA, | |
'isajax': 'true', | |
'origin': 'https://lexiangla.com', | |
'x-requested-with': 'XMLHttpRequest' | |
}) | |
for c in COOKIES.split(';'): | |
k, v = c.strip().split('=', 1) | |
v = parse.quote(v) | |
session.cookies.set(k, v) | |
page = session.get(course_url, allow_redirects=False) | |
soup = BeautifulSoup(page.content, 'html5lib') | |
course_data = json.loads(soup.body.text) | |
print('Title:', course_data['data']['title']) | |
pass_time = course_data['data']['pass']['succeeded_at'] | |
passed = pass_time is not None | |
if passed: | |
print('Course already passed at', pass_time) | |
return | |
courses_list = list() | |
for ch in course_data['data']['chapters']: | |
chapter = dict() | |
chapter['name'] = ch['name'] | |
chapter['courses'] = list() | |
for c in ch['courses']: | |
chapter['courses'].append({ | |
'title': c['title'], | |
'id': c['id'], | |
'duration': c['duration'], | |
'passed_time': c['pass']['succeeded_at'] if c['pass'] else None | |
}) | |
courses_list.append(chapter) | |
print(session.cookies.get_dict()) | |
session.headers.update({'x-xsrf-token': session.cookies.get('XSRF-TOKEN', domain='.lexiangla.com')}) | |
for ch in courses_list: | |
print('\nChapter:', ch['name']) | |
for c in ch['courses']: | |
print('\n - Course:', c['title']) | |
if c['passed_time'] is not None: | |
print(' Already passed, skip') | |
continue | |
resp = session.put(study_url.format(courseid=c['id']), | |
json={'learn_time': int(c['duration'] + random.randrange(2, 13))}) | |
if resp.json()['code'] == 0: | |
print(' Success!') | |
page = session.get(course_url, allow_redirects=False) | |
soup = BeautifulSoup(page.content, 'html5lib') | |
course_data = json.loads(soup.body.text) | |
pass_time = course_data['data']['pass']['succeeded_at'] | |
if pass_time is not None: | |
print('SUCCESS! Course completed at', pass_time) | |
else: | |
print('Check failed, course not showing completed, check again later') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment