def _do_login(self, verification_code): #headers = {'Referer': URL.SITE} post_data = {'studentId': self.student_id, 'password': self.password, 'Submit2': 'Submit', 'rand': verification_code} page = self.__session.post(URL.LOGIN, data=post_data) save_to_file(page.content, self.RESULT_FILE) return len(page.content) < 2000
def _add_drop_course(self, course_code, xklb, url): #headers = {'Referer': 'http://xk.fudan.edu.cn/xk/input.jsp', 'Connection': 'keep-alive',} post_data = {'token': self.__token, 'selectionId': course_code, 'xklb': xklb, 'rand': self.__verification_code} result = self.__session.post(url, data=post_data) save_to_file(result.content, self.RESULT_FILE) return self._get_result(result.content)
def get_course_panel(self): panel = self.__session.get(URL.COURSE_PANEL) save_to_file(panel.content, self.PANEL_FILE, 'course panel html saved') self.__token = self._get_token(panel.content) img = self.__session.get(URL.COURSE_PANEL_IMAGE + self.__token) save_to_file(img.content, self.IMAGE_FILE, 'image saved') print 'please input verification code' Image.open(self.IMAGE_FILE).show() self.__verification_code = raw_input()
def login(self): self.__session.get(URL.SITE) img = self.__session.get(URL.LOGIN_IMAGE) save_to_file(img.content, self.IMAGE_FILE, 'please input verification code manually') Image.open(self.IMAGE_FILE).show() verification_code = raw_input() if self._do_login(verification_code): print 'login successful' else: raise Exception('login failed') return panel.Panel(self.__session)
def get_assets(self): if self.metadata: self.get_meta() print('Verifying assets..') assets_dir = os.path.join(self.client_root, 'assets') assets_versions = os.path.join(assets_dir, 'indexes') ensure_dir(assets_versions) asset_index = self.metadata['assetIndex'] assets_file = os.path.join(assets_versions, '%s.json' % (asset_index['id'])) if not os.path.exists(assets_file): r = requests.get(asset_index['url'], stream=True) try: save_to_file_sha1(assets_file, r, asset_index['sha1']) except Exception: print('Failed to download assets!') raise with open(assets_file) as json_data: assets = json.load(json_data) for key, data in assets['objects'].items(): first = data['hash'][0:2] asset_url = 'http://resources.download.minecraft.net/%s/%s' % ( first, data['hash']) asset_dir = os.path.join(assets_dir, 'objects', first) ensure_dir(asset_dir) asset_file = os.path.join(asset_dir, data['hash']) if os.path.exists(asset_file): continue r = requests.get(asset_url, stream=True) try: save_to_file(asset_file, r) except Exception as e: print('Failed to download asset %s!' % (key)) raise e print('All assets verified.')
def get_meta(self): ensure_dir(self.version_directory) metafile = os.path.join(self.version_directory, '%s.json' % self.version_name) if not os.path.exists(metafile): print('Grabbing metadata from Minecraft Downloads CDN...') # Get version metadata r = requests.get( 'https://s3.amazonaws.com/Minecraft.Download/versions/%s/%s.json' % (self.mcver, self.mcver), stream=True) # Save the file save_to_file(metafile, r) with open(metafile) as json_data: self.metadata = json.load(json_data)
def install(self): if not self.metadata: self.get_meta() rfile = os.path.join(self.version_directory, '%s.jar' % (self.version_name)) print(self.metadata['downloads']) if not os.path.exists(rfile): print('Downloading version jar...') rv = requests.get(self.metadata['downloads']['client']['url'], stream=True) save_to_file(rfile, rv) else: print('Skipping version jar download..') self.get_libraries() self.get_assets()
def artifact(self, artifact, backup=None): lib_directory = os.path.join(self.client_root, 'libraries') artifact_path = os.path.join(lib_directory, artifact['path']) # Ensure the directory exists directory = artifact_path.split('/') del directory[-1] directory = '/'.join(directory) ensure_dir(directory) # Make sure it doesn't already exist if os.path.exists(artifact_path): print('Artifact %s exists, skipping..' % artifact['path']) return True print('Downloading artifact %s' % (artifact['path'])) url_dl = artifact['url'] if 'lzma' in artifact and artifact['lzma'] == True: url_dl += '.pack.xz' artifact_path += '.pack.xz' r = requests.get(url_dl, stream=True) if 'sha1' in artifact and not 'lzma' in artifact: f = save_to_file_sha1(artifact_path, r, artifact['sha1']) else: f = save_to_file(artifact_path, r) if 'lzma' in artifact and artifact['lzma'] == True: # Only unpack if we're a Forge client if getattr(self, "unpack_lzma", None): self.unpack_lzma(f) return artifact_path
def install_forge(self): if os.path.exists(self.version_directory): return # Create a temporary files directory self.tmp_dir = 'tmp' ensure_dir(self.tmp_dir) # Make sure all the components are ready. self.install() forge_path = mvn + 'net/minecraftforge/forge/{name}/forge-{name}-universal.jar'.format( name=self.forge_name) forge_target = os.path.join(self.tmp_dir, forge_path.split('/')[-1]) r = requests.get(forge_path, stream=True) save_to_file(forge_target, r) print('Extracting forge archive ...') zip_ref = zipfile.ZipFile(forge_target, 'r') zip_ref.extractall(self.tmp_dir) zip_ref.close() print('Reading forge metadata ...') metafile = os.path.join(self.tmp_dir, 'version.json') with open(metafile) as json_data: self.forge_metadata = json.load(json_data) if not self.forge_metadata['inheritsFrom'] == self.metadata['id']: raise ValueError( 'Versions mismatch! This forge version was made for Minecraft %s!' % (self.forge_metadata['inheritsFrom'])) print('Creating pretty libraries ...') libs = [] # Make libraries readable by the MinecraftClient class. # Ensures that .pack.xz files get unpacked properly as well. for lib in self.forge_metadata['libraries']: nsplit = lib['name'].split(':') if 'serverreq' in lib and lib['serverreq'] == True and ( 'clientreq' in lib and lib['clientreq'] == False): continue if 'clientreq' in lib and lib['clientreq'] == False: continue # Lib Path builder fpath = nsplit[0].split('.') fpath.append(nsplit[1]) fpath.append(nsplit[2]) fpath.append(nsplit[1] + '-' + nsplit[2] + '.jar') fpath = '/'.join(fpath) # Lib URL builder url = None if 'url' in lib: if nsplit[1] == 'forge': # A hack around, need to download the universal jar! forgePath = nsplit[0].split('.') forgePath.append(nsplit[1]) forgePath.append(nsplit[2]) forgePath.append(nsplit[1] + '-' + nsplit[2] + '-universal.jar') url = lib['url'] + '/'.join(forgePath) else: url = lib['url'] + fpath else: url = mclib + fpath # The artifacts with two checksums are .pack.xz files sha1 = None requiresLZMA = False if 'checksums' in lib: sha1 = lib['checksums'] if len(sha1) > 1: sha1 = None requiresLZMA = True else: sha1 = lib['checksums'][0] libs.append({ "name": lib['name'], "downloads": { "artifact": { "lzma": requiresLZMA, "url": url, "path": fpath, "sha1": sha1 } } }) print('Library paths finished, saving to metadata ...') self.metadata['libraries'] += libs self.metadata['id'] = self.forge_metadata['id'] self.metadata['minecraftArguments'] = self.forge_metadata[ 'minecraftArguments'] self.metadata['mainClass'] = self.forge_metadata['mainClass'] self.save_metadata() self.get_libraries() print('Cleaning up ...') self.clean_up()
def get_course_table(self): courses = self.__session.get(URL.COURSE_TABLE) save_to_file(courses.content, self.TABLE_FILE, 'course table saved')