Last active
August 29, 2015 14:06
-
-
Save shuax/6527db2ed2d9e7401b41 to your computer and use it in GitHub Desktop.
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 os | |
import re | |
import zlib | |
import urllib.request | |
from struct import unpack | |
class QQWayIPSeekerUpdate(): | |
@classmethod | |
def __download_meta(cls): | |
url = 'http://update.cz88.net/ip/copywrite.rar' | |
with urllib.request.urlopen(url) as fp: | |
data = fp.read() | |
return cls.__unpack_meta(data) | |
@classmethod | |
def __download_database(cls, path: str, key: int): | |
url = 'http://update.cz88.net/ip/qqwry.rar' | |
with urllib.request.urlopen(url) as fp: | |
data = fp.read() | |
data = cls.__decipher_data(data, key) | |
with open(path, 'wb') as fp: | |
fp.write(data) | |
@classmethod | |
def __unpack_meta(cls, data): | |
# http://microcai.org/2014/05/11/qqwry_dat_download.html | |
sign, version, _, size, _, key, text, link = unpack('<4sIIIII128s128s', data) | |
text = text.rstrip(b'\0').decode('GB18030') | |
return { | |
'version': version, | |
'size': size, | |
'key': key, | |
'text': text, | |
'date': tuple(int(x) for x in re.findall('\d+', text)), | |
'link': link.rstrip(b'\0').decode('GB18030'), | |
} | |
@classmethod | |
def __decipher_data(cls, data: bytes, key: int): | |
data = bytearray(data) | |
for index in range(0, 0x200): | |
key *= 0x805 | |
key += 1 | |
key &= 0xFF | |
data[index] ^= key | |
return zlib.decompress(data) | |
@classmethod | |
def check_update(cls, path: str): | |
meta = cls.__download_meta() | |
return meta['date'] < QQWayIPSeeker(path).meta['date'] | |
@classmethod | |
def update(cls, path: str): | |
if os.path.exists(path): | |
cls.upgrade(path) | |
else: | |
cls.download(path) | |
@classmethod | |
def upgrade(cls, path: str): | |
meta = cls.__download_meta() | |
if meta['date'] < QQWayIPSeeker(path).meta['date']: | |
cls.__download_database(path, meta['key']) | |
@classmethod | |
def download(cls, path: str): | |
meta = cls.__download_meta() | |
cls.__download_database(path, meta['key']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment