def _get_dmm_tokens(self): """解析DMM的登录页面,获取dmm_token和token,返回dmm_token和token的值。 :return: tuple """ response = yield from self._request(self.urls['login'], method='GET', data=None, timeout_message='连接DMM登录页失败') html = yield from response.text() m = self.patterns['dmm_token'].search(html) if m: self.dmm_token = m.group(1) else: raise OOIAuthException('获取DMM token失败') m = self.patterns['token'].search(html) if m: self.token = m.group(1) else: raise OOIAuthException('获取token失败') #print(str(self.dmm_token)) #print(str(self.token)) return self.dmm_token, self.token
def _get_osapi_url(self): """登录DMM账号,并转到《舰队collection》游戏页面,获取内嵌游戏网页的地址。 :return: str """ del self.headers['DMM_TOKEN'] del self.headers['X-Requested-With'] data = { 'login_id': self.login_id, 'password': self.password, 'token': self.token, self.idKey: self.login_id, self.pwdKey: self.password } response = yield from self._request(self.urls['auth'], method='POST', data=data, timeout_message='连接DMM认证网页失败') html = yield from response.text() m = self.patterns['reset'].search(html) if m: raise OOIAuthException('DMM强制要求用户修改密码') response = yield from self._request( self.urls['game'], timeout_message='连接舰队collection游戏页面失败') html = yield from response.text() m = self.patterns['osapi'].search(html) if m: self.osapi_url = m.group(1) else: raise OOIAuthException('用户名或密码错误,请重新输入') return self.osapi_url
def _get_api_token(self): """根据用户所在服务器IP和用户自身的ID,从DMM处获得用户的api_token、api_starttim,并生成游戏FLASH的地址 :return: tuple """ url = self.urls['get_entry'] % (self.world_ip, self.owner, int(time.time()*1000)) data = {'url': url, 'httpMethod': 'GET', 'authz': 'signed', 'st': self.st, 'contentType': 'JSON', 'numEntries': '3', 'getSummaries': 'false', 'signOwner': 'true', 'signViewer': 'true', 'gadget': 'http://203.104.209.7/gadget.xml', 'container': 'dmm'} response = self._request(self.urls['make_request'], method='POST', data=data, timeout_message='Connection timeout when requesting token for entering the Chinju-fu. ' 'Maybe the official sever is under maintaince.') html = response.text svdata = json.loads(html[27:]) if svdata[url]['rc'] != 200: raise OOIAuthException('Server error when parsing token for entering the Chinju-fu.') svdata = json.loads(svdata[url]['body'][7:]) if svdata['api_result'] != 1: raise OOIAuthException('Server error when parsing token for entering the Chinju-fu.') self.api_token = svdata['api_token'] self.api_starttime = svdata['api_starttime'] self.entry = self.urls['entry'] % (self.world_ip, self.api_token, self.api_starttime) return self.api_token, self.api_starttime, self.entry
def _get_api_token(self): """根据用户所在服务器IP和用户自身的ID,从DMM处获得用户的api_token、api_starttim,并生成游戏FLASH的地址 :return: tuple """ url = self.urls['get_flash'] % (self.world_ip, self.owner, int(time.time()*1000)) data = {'url': url, 'httpMethod': 'GET', 'authz': 'signed', 'st': self.st, 'contentType': 'JSON', 'numEntries': '3', 'getSummaries': 'false', 'signOwner': 'true', 'signViewer': 'true', 'gadget': 'http://203.104.209.7/gadget.xml', 'container': 'dmm'} response = yield from self._request(self.urls['make_request'], method='POST', data=data, timeout_message='调查提督进入镇守府的口令失败') html = yield from response.text() svdata = json.loads(html[27:]) if svdata[url]['rc'] != 200: raise OOIAuthException('调查提督进入镇守府的口令失败') svdata = json.loads(svdata[url]['body'][7:]) if svdata['api_result'] != 1: raise OOIAuthException('调查提督进入镇守府的口令时发生错误') self.api_token = svdata['api_token'] self.api_starttime = svdata['api_starttime'] self.flash = self.urls['flash'] % (self.world_ip, self.api_token, self.api_starttime) return self.api_token, self.api_starttime, self.flash
def _get_osapi_url(self): """登录DMM账号,并转到《舰队collection》游戏页面,获取内嵌游戏网页的地址。 :return: str """ del self.session.headers['http-dmm-token'] del self.session.headers['X-Requested-With'] data = {'login_id': self.login_id, 'password': self.password, 'token': self.token, 'idKey': self.login_id, 'pwKey': self.password} response = self._request(self.urls['auth'], method='POST', data=data, timeout_message='Connection timeout for DMM login page.') html = response.text m = self.patterns['reset'].search(html) if m: raise OOIAuthException('DMM requests a password change.') response = self._request(self.urls['game'], timeout_message='Connection timeout for DMM game page.') html = response.text m = self.patterns['osapi'].search(html) if m: self.osapi_url = m.group(1) else: raise OOIAuthException('Wrong username or password.') return self.osapi_url
def _request(self, url, method='GET', data=None, timeout_message='连接失败', timeout=10): """使用asyncio.wait_for包装过的会话向远端服务器发起请求。 `url`为请求的URL地址,`method`为请求的方法, `data`为发起POST请求时的数据,`timeout_message`为请求超时后抛出异常所带的信息, `timeout`为超时时间,单位为秒。 :param url: str :param method: str :param data: dict :param timeout_message: str :param timeout: int :return: generator """ try: response = yield from asyncio.wait_for( self.session.request(method, url, data=data, headers=self.headers), timeout) return response except asyncio.TimeoutError: raise OOIAuthException(timeout_message)
def _get_ajax_token(self): """根据在DMM登录页获得的dmm_token和token,发起一个AJAX请求,获取第二个token以及idKey和pwdKey。 :return: tuple """ self.headers.update({ 'Origin': 'https://accounts.dmm.com', 'Referer': self.urls['login'], 'http-dmm-token': self.dmm_token, 'X-Requested-With': 'XMLHttpRequest' }) data = {'token': self.token} response = yield from self._request(self.urls['ajax'], method='POST', data=data, timeout_message='DMM登录页AJAX请求失败') j = yield from response.json() #print(str(j)) #print(str(j['body']['token'])) try: self.token = j['body']['token'] self.idKey = j['body']['login_id'] self.pwdKey = j['body']['password'] except Exception: raise OOIAuthException('DMM修改登录机制了,请通知管理员处理') return self.token, self.idKey, self.pwdKey
def _get_dmm_tokens(self): """解析DMM的登录页面,获取dmm_token和token,返回dmm_token和token的值。 :return: tuple """ response = self._request(self.urls['login'], method='GET', data=None, timeout_message='Connection Timeout for DMM token.') html = response.text m = self.patterns['dmm_token'].search(html) if m: self.dmm_token = m.group(1) else: raise OOIAuthException('Failed to fetch DMM token, are you in Japan?') m = self.patterns['token'].search(html) if m: self.token = m.group(1) else: raise OOIAuthException('Failed to fetch token.') return self.dmm_token, self.token
def _get_world(self): """解析游戏内嵌网页地址,从DMM处获得用户所在服务器的ID和IP地址。 :return: tuple """ qs = parse_qs(urlparse(self.osapi_url).query) self.owner = qs['owner'][0] self.st = qs['st'][0] url = self.urls['get_world'] % (self.owner, int(time.time() * 1000)) self.headers['Referer'] = self.osapi_url response = yield from self._request(url, timeout_message='调查提督所在镇守府失败') html = yield from response.text() svdata = json.loads(html[7:]) if svdata['api_result'] == 1: self.world_id = svdata['api_data']['api_world_id'] self.world_ip = self.world_ip_list[self.world_id - 1] else: raise OOIAuthException('调查提督所在镇守府时发生错误') return self.world_id, self.world_ip, self.st
def _request(self, url, method='GET', data=None, timeout_message='Connection Timeout.', timeout=10): """使用 requests.get() 包装过的会话向远端服务器发起请求。 `url`为请求的URL地址,`method`为请求的方法, `data`为发起POST请求时的数据,`timeout_message`为请求超时后抛出异常所带的信息, `timeout`为超时时间,单位为秒。 :param url: str :param method: str :param data: dict :param timeout_message: str :param timeout: int :return: generator """ try: response = self.session.request(method=method, url=url, data=data, timeout=timeout) return response except requests.Timeout: raise OOIAuthException(timeout_message)
def _get_world(self): """解析游戏内嵌网页地址,从DMM处获得用户所在服务器的ID和IP地址。 :return: tuple """ qs = parse_qs(urlparse(self.osapi_url).query) self.owner = qs['owner'][0] self.st = qs['st'][0] url = self.urls['get_world'] % (self.owner, int(time.time()*1000)) self.session.headers['Referer'] = self.osapi_url response = self._request(url, timeout_message='Connection timeout when looking for Chinju-fu.') html = response.text svdata = json.loads(html[7:]) if svdata['api_result'] == 1: self.world_id = svdata['api_data']['api_world_id'] self.world_ip = self.world_ip_list[self.world_id-1] else: raise OOIAuthException('Server error when looking for Chinju-fu. Maybe the official sever is under ' 'maintaince.') return self.world_id, self.world_ip, self.st
def _get_ajax_token(self): """根据在DMM登录页获得的dmm_token和token,发起一个AJAX请求,获取第二个token以及idKey和pwdKey。 :return: tuple """ self.session.headers.update({'Origin': 'https://accounts.dmm.com', 'Referer': self.urls['login'], 'http-dmm-token': self.dmm_token, 'X-Requested-With': 'XMLHttpRequest'}) data = {'token': self.token} response = self._request(self.urls['ajax'], method='POST', data=data, timeout_message='Connection Timeout for AJAX token.') j = response.json() try: self.token = j['body']['token'] self.idKey = j['body']['login_id'] self.pwdKey = j['body']['password'] except Exception: raise OOIAuthException('DMM has changed its login method, please contact your administrator.') return self.token, self.idKey, self.pwdKey