def _initialize_conn(self): def on_data_received(data): if self._cache_file: self._cache_file.write(data) if data: self._chance = getopt("Retry") try: return self._data_receive(data) except Exception as e: traceback.print_exc(e) if self._curl: self._curl.close() self._curl = pycurl.Curl() self._curl.setopt(self._curl.HTTPHEADER, [ 'Accept: */*', 'Accept-Language:z h-CN,zh;q=0.8,gl;q=0.6,zh-TW;q=0.4', 'Proxy-Connection: keep-alive', 'Content-Type: application/x-www-form-urlencoded', 'Host: %s' % self._host.replace("http://", ""), 'User-Agent: %s' % getopt("UserAgent") ]) self._curl.setopt(self._curl.URL, self._url) self._curl.setopt(self._curl.WRITEFUNCTION, on_data_received) self._curl.setopt(self._curl.TIMEOUT, getopt("Timeout")) self._curl.setopt(self._curl.FOLLOWLOCATION, 1) if getopt("Proxies"): self._curl.setopt(self._curl.PROXY, getopt("Proxies"))
def _initialize_conn(self): def on_data_received(data): if self._cache_file: self._cache_file.write(data) if data: self._chance = getopt("Retry") try: return self._data_receive(data) except Exception as e: traceback.print_exc(e) if self._curl: self._curl.close() self._curl = pycurl.Curl() self._curl.setopt(self._curl.HTTPHEADER, [ 'Accept: */*', 'Accept-Language:z h-CN,zh;q=0.8,gl;q=0.6,zh-TW;q=0.4', 'Proxy-Connection: keep-alive', 'Content-Type: application/x-www-form-urlencoded', 'Host: %s'%self._host.replace("http://", ""), 'User-Agent: %s'%getopt("UserAgent") ]) self._curl.setopt(self._curl.URL, self._url) self._curl.setopt(self._curl.WRITEFUNCTION, on_data_received) self._curl.setopt(self._curl.TIMEOUT, getopt("Timeout")) self._curl.setopt(self._curl.FOLLOWLOCATION, 1) if getopt("Proxies"): self._curl.setopt(self._curl.PROXY, getopt("Proxies"))
def _do_rsa(message): global _rsa_modulus, _rsa_pubkey if _rsa_modulus == -1: _rsa_modulus = getopt("RSAModulus", required = True) _rsa_pubkey = getopt("RSAPubKey", required = True) data = int(message[::-1].encode('hex'), 16) % _rsa_modulus expo = data indx = _rsa_pubkey ret = 1 while indx > 0: if indx % 2 == 1: ret = (ret * expo) % _rsa_modulus expo = (expo * expo) % _rsa_modulus indx /= 2 ret = "%x"%ret return ret.zfill(256)
def on_data_received(data): if self._cache_file: self._cache_file.write(data) if data: self._chance = getopt("Retry") try: return self._data_receive(data) except Exception as e: traceback.print_exc(e)
def __init__(self, path, write_func, header_func): self._host = getopt("Servers")[HttpClient._nextserver] self._url = "%s%s"%(self._host, path) self._path = path HttpClient._nextserver = 1 - HttpClient._nextserver client.Client.__init__(self, path, write_func) self._curl = None self._cache = cache.getcache() self._cache_file = None self._header_func = header_func self._chance = 0
def __init__(self, path, write_func, header_func): self._host = getopt("Servers")[HttpClient._nextserver] self._url = "%s%s" % (self._host, path) self._path = path HttpClient._nextserver = 1 - HttpClient._nextserver client.Client.__init__(self, path, write_func) self._curl = None self._cache = cache.getcache() self._cache_file = None self._header_func = header_func self._chance = 0
def perform(self): block_size = getopt("BlockSize") self._header_func("Content-Type: audio/mpeg\n") self._header_func("Content-Length: %d\n"%os.fstat(self._cache_file.fileno()).st_size) self._header_func("NE-Client-Type: Cached\n") if not block_size: block_size = 1024 while True: next_block = self._cache_file.read(block_size) if not next_block: if self._cache_mode == self._cache.FILE_OPEN and self._cache.isopenstatus(self._path): time.sleep(0.1) else: break self._write_func(next_block) self._cache_file.close()
def encode_id(song_id): """Convert the plain text song id to encoded version song_id the song id to convert """ global _xor_key, _xor_key_len if not _xor_key: _xor_key = bytearray(getopt("XorKey", required = True)) _xor_key_len = len(_xor_key) song_id = bytearray(song_id) curpos = 0 for i in xrange(len(song_id)): song_id[i] ^= _xor_key[i % _xor_key_len] ret = hashlib.md5(song_id) ret = ret.digest().encode("base64").strip().replace('/', '_').replace('+', '-') return ret
def _prepare(self): if not NEMusicSession._base_url: NEMusicSession._base_url = getopt("BaseURL") NEMusicSession._proxies = getopt("Proxies") NEMusicSession._cookies = getopt("CookiePath") NEMusicSession._timeout = getopt("Timeout") NEMusicSession._retry = getopt("Retry") NEMusicSession._cache = getopt("EnableCache") NEMusicSession._headers = [ 'Accept:*/*', 'Accept-Language:zh-CN,zh;q=0.8,gl;q=0.6,zh-TW;q=0.4', 'Connection:keep-alive', 'Content-Type:application/x-www-form-urlencoded', 'Host:music.163.com', 'Referer:http://music.163.com/search/', 'User-Agent:' + getopt("UserAgent") ] if self._cache and not self._cache_object: self._cache_object = cache.APICache(getopt("CacheDir"))
def encode_account(account, password, phone = False): """Encode an account infomation for login account: The account ID, or the phone number password: The password is going to use phone: If this account Id is the phone Number""" global _aes_nonce if not _aes_nonce: _aes_nonce = getopt("AESNonce", required = True) data = {'password' : password, 'rememberLogin' : 'true' } if phone: data['phone'] = account else: data['username'] = account raw_data = json.dumps(data) aes_key = _get_aes_key() enc_data = _do_rsa(_do_aes(_do_aes(raw_data, _aes_nonce), aes_key)) return {'params': enc_data, 'encSecKey': aes_key}
def perform(self): block_size = getopt("BlockSize") self._header_func("Content-Type: audio/mpeg\n") self._header_func("Content-Length: %d\n" % os.fstat(self._cache_file.fileno()).st_size) self._header_func("NE-Client-Type: Cached\n") if not block_size: block_size = 1024 while True: next_block = self._cache_file.read(block_size) if not next_block: if self._cache_mode == self._cache.FILE_OPEN and self._cache.isopenstatus( self._path): time.sleep(0.1) else: break self._write_func(next_block) self._cache_file.close()
def perform(self, retry_attempt = False): self._initialize_conn() if self._current_pos > 0: self._curl.setopt(self._curl.RANGE, "%d-"%self._current_pos) else: if not retry_attempt: self._chance = getopt("Retry") self._cache_file = self._cache.open(self._path) self._curl.setopt(self._curl.HEADERFUNCTION, self._header_func) try: self._curl.perform() except pycurl.error as e: if self._chance > 0: print >> sys.stderr, "[HttpClient] Read failure: %s, take another chance from offset %d (chances left : %d)" % (e, self._current_pos ,self._chance) self._chance -= 1 self.perform(True) else: print >> sys.stderr, "[HttpClient] No more chance for this connection at %d, give up (exception = %s)" % (self._current_pos, e) self._cache.abort(self._path) raise e self._cache.close(self._path)
def perform(self, retry_attempt=False): self._initialize_conn() if self._current_pos > 0: self._curl.setopt(self._curl.RANGE, "%d-" % self._current_pos) else: if not retry_attempt: self._chance = getopt("Retry") self._cache_file = self._cache.open(self._path) self._curl.setopt(self._curl.HEADERFUNCTION, self._header_func) try: self._curl.perform() except pycurl.error as e: if self._chance > 0: print >> sys.stderr, "[HttpClient] Read failure: %s, take another chance from offset %d (chances left : %d)" % ( e, self._current_pos, self._chance) self._chance -= 1 self.perform(True) else: print >> sys.stderr, "[HttpClient] No more chance for this connection at %d, give up (exception = %s)" % ( self._current_pos, e) self._cache.abort(self._path) raise e self._cache.close(self._path)
def getcache(): global _cache if not _cache: _cache = DataCache(getopt("CacheDir")) return _cache