SHARE
TWEET

Untitled

a guest May 23rd, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from hashlib import md5
  2. from sqlite3 import connect as sql_conn
  3. from time import localtime, strftime
  4.  
  5.  
  6. class IllegalException(Exception):
  7.     def __init__(self, *args):
  8.         super().__init__(*args)
  9.  
  10.  
  11. class IMEI:
  12.     @staticmethod
  13.     def check(IMEI: str):
  14.         if len(IMEI) != 15:
  15.             return False
  16.         evens = [int(x)*2 for x in IMEI[1:14:2]]
  17.         odds = [int(x) for x in IMEI[0:14:2]]
  18.         evens_sum = [int(y) for x in evens for y in list(str(x))]
  19.         tmp = (10 - sum(evens_sum+odds) % 10) % 10
  20.         return tmp == int(IMEI[-1])
  21.  
  22.     def __new__(cls, IMEI_str: str) -> str:
  23.         if not IMEI.check(IMEI_str):
  24.             raise IllegalException("The IMEI is illegal.")
  25.         return IMEI_str
  26.  
  27.  
  28. class DBFile:
  29.     @staticmethod
  30.     def check(file: str):
  31.         from os import path
  32.         return path.isfile(file)
  33.  
  34.     def __new__(self, file):
  35.         if not DBFile.check(file):
  36.             raise FileNotFoundError("The DataBase does not found.")
  37.         return file
  38.  
  39.  
  40. class QQNum:
  41.     @staticmethod
  42.     def check(num: str):
  43.         return num.isdigit()
  44.  
  45.     def __new__(self, num):
  46.         if not QQNum.check(num):
  47.             raise IllegalException("QQ number must be integer.")
  48.         return num
  49.  
  50.  
  51. class QQDataBase:
  52.     def __init__(self, IMEI=None, DB=None, num=None):
  53.         self.DB = sql_conn(DB)
  54.         self.IMEI = IMEI
  55.         self.qq_num = num
  56.         self.num_to_remark = {}
  57.         self.data_dict = {}  # key = timestamp
  58.  
  59.     def decrypt(self, foo):
  60.         return ''.join([chr(ord(x) ^ ord(self.IMEI[i % 15]))
  61.                         for i, x in enumerate(foo)]) if type(foo) is str else \
  62.             b''.join([bytes([x ^ ord(self.IMEI[i % 15])])
  63.                       for i, x in enumerate(foo)]) if type(foo) is bytes else \
  64.             None
  65.  
  66.     def get_remark(self):
  67.         remarks = self.DB.execute("SELECT uin, remark, name FROM Friends")
  68.         for cor in remarks:
  69.             num = self.decrypt(cor[0])
  70.             remark = self.decrypt(cor[1]) or self.decrypt(cor[2])
  71.             self.num_to_remark[num] = remark
  72.  
  73.     def get_msg(self):
  74.         messages = self.DB.execute(
  75.             "SELECT msgData, senderuin, time FROM mr_friend_{}_New".format(
  76.                 md5(self.qq_num.encode('utf-8')).hexdigest().upper()))
  77.         for msg in messages:
  78.             try:
  79.                 msg_data = self.decrypt(msg[0]).decode('utf-8')
  80.             except UnicodeDecodeError:
  81.                 msg_data = "[图片]"
  82.             uid = self.decrypt(msg[1])
  83.             self.data_dict[msg[2]] = [uid, msg_data]
  84.  
  85.     def export(self):
  86.         with open('{}.txt'.format(self.qq_num), 'a') as f:
  87.             for key in self.data_dict:
  88.                 time = strftime("%Y-%m-%d %H:%M:%S", localtime(key))
  89.                 remark = self.num_to_remark[self.data_dict[key][0]]
  90.                 msg = self.data_dict[key][1]
  91.                 f.write('{}:\t{}\n{}\n'.format(time, remark, msg))
  92.         print('Success! Saved in {}.txt .'.format(self.qq_num))
  93.         exit()
  94.  
  95.     def serialize(self):
  96.         self.get_remark()
  97.         self.get_msg()
  98.         self.export()
  99.  
  100.  
  101. if __name__ == "__main__":
  102.     File, IMEI_, qq_num = [None] * 3
  103.     while True:
  104.         try:
  105.             File = DBFile(input('Please input database name: ')
  106.                           ) if not File else File
  107.             IMEI_ = IMEI(input('Pleass input phone IMEI: ')
  108.                          ) if not IMEI_ else IMEI_
  109.             qq_num = QQNum(input('Please input a QQ number: ')
  110.                            ) if not qq_num else qq_num
  111.             db = QQDataBase(IMEI_, File, qq_num)
  112.             db.serialize()
  113.         except (FileNotFoundError, IllegalException) as e:
  114.             print(e, "Please try again.")
  115.         except Exception as e:
  116.             print(e)
  117.             exit()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top