Guest User

Untitled

a guest
Feb 12th, 2017
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.49 KB | None | 0 0
  1. import requests
  2. import json
  3. import const
  4. from array import *
  5. import base64
  6. import hmac,hashlib
  7. import pprint
  8. import time
  9. import urllib.parse
  10.  
  11. class ICQClient(object):
  12.     CLIENT_LOGIN_URL = 'https://api.login.icq.net/auth/clientLogin'
  13.     CLIENT_MESSAGE_URL = 'http://api.icq.net/im/sendIM'
  14.     CLIENT_BASE = 'https://rapi.icq.net'
  15.     CLIENT_API_BASE = 'http://api.icq.net'
  16.     CONNECTION_TIMEOUT = 60 * 1000
  17.     DEV_ID = 'ic1Ytjc4pxslFTEL'
  18.     CLIENT_NAME = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
  19.     CLIENT_VERSION = '1.0'
  20.    
  21.     sessionKey = ''
  22.    
  23.    
  24.     def __init__(self, login, password):
  25.         self.login = login
  26.         self.password = password
  27.        
  28.     def _session_settings(self):
  29.         STARTS_SESSION = {
  30.             'f': 'json',
  31.             'language': 'ru',
  32.             'events': 'mchat,userAddedToBuddyList,hist,imState,buddylist',
  33.             'includePresenceFields': 'aimId,friendly,state,ssl',
  34.             'sessionTimeout': '2592000',
  35.             'ts': round(time.time())
  36.         }
  37.         return STARTS_SESSION
  38.        
  39.     def _make_login_params(self):
  40.         login_params = {
  41.             's': self.login,
  42.             'pwd': self.password,
  43.             'language': 'ru',
  44.             'time': time.time()+1000,
  45.             'idType': 'ICQ',
  46.             'tokenType':'longterm',
  47.             'k': 'ic17mFHiwr52TKrx',
  48.             'devId': '***',
  49.             'f':'json'
  50.         }
  51.    
  52.         return login_params
  53.    
  54.     def do_login(self):
  55.         try:
  56.             print('Login')
  57.             response = requests.post(self.CLIENT_LOGIN_URL, data=self._make_login_params(),timeout=self.CONNECTION_TIMEOUT)
  58.             print(response.text)
  59.             if response.status_code == 200:
  60.    
  61.                 res_content = json.loads(response.text)
  62.                 session_params = self._session_settings()
  63.                 session_params['k'] = 'ic17mFHiwr52TKrx'
  64.                 session_params['a'] = res_content['response']['data']['token']['a']
  65.                 session_params['view'] = 'online'
  66.                 session_params['invisible'] = 'false'
  67.                 session_params['mobile'] = 0
  68.                
  69.                 key = bytes(self.password, 'UTF-8')
  70.                 message = bytes(res_content['response']['data']['sessionSecret'], 'UTF-8')
  71.                 digester = hmac.new(key, message, hashlib.sha256)
  72.                 signature1 = digester.digest()
  73.                 signature2 = base64.urlsafe_b64encode(signature1)    
  74.                
  75.                 self.sessionKey = str(signature2, 'UTF-8')
  76.                 url = self.makeSignedUrl(self.CLIENT_API_BASE + '/aim/startSession', session_params, self.sessionKey, 'GET')
  77.                 response_session = requests.get(url)
  78.                 print(response_session.text)
  79.                 #return res_content.get('statusCode', 0), res_content.get('data', {})
  80.             else:
  81.                 print('server return ' + response.status_code)
  82.                 return const.INTERNAL_ERROR_CODE, {}
  83.         except requests.exceptions.Timeout:
  84.             print('Timeout reached while connecting to server')
  85.             #logging.info(traceback.format_exc())
  86.             return const.INTERNAL_ERROR_CODE, {}
  87.        
  88.     def startSession(self):
  89.        
  90.         response = requests.get(self.CLIENT_API_BASE + '/aim/startSession', params=self._session_settings(), timeout=self.CONNECTION_TIMEOUT)
  91.         pprint.pprint(response)
  92.        
  93.     def makeSignedUrl(self, url, params, sessionKey, method):
  94.         sorted(params)
  95.         params = urllib.parse.urlencode(params)
  96.  
  97.         i = method + '&' + urllib.parse.unquote(url) + '&' + urllib.parse.unquote(params)
  98.        
  99.         key = bytes(self.sessionKey, 'UTF-8')
  100.         message = bytes(i, 'UTF-8')
  101.         digester = hmac.new(key, message, hashlib.sha256)
  102.         signature1 = digester.digest()
  103.         signature2 = base64.urlsafe_b64encode(signature1)    
  104.         params += '&sig_sha256=' + str(signature2, 'UTF-8')
  105.         print(url + '?' + params)
  106.         return url + '?' + params;
  107.            
  108.        
  109.     def do_send(self, uin, message):
  110.         try:
  111.             print('Send msg')
  112.             payload = {
  113.                 t: uin,
  114.                 message: message,
  115.                 notifyDelivery: 0
  116.             }
  117.             response = requests.post(self.CLIENT_BASE, data=payload, timeout=self.CONNECTION_TIMEOUT)
  118.             print(response)
  119.         except:
  120.             print('ex')
Add Comment
Please, Sign In to add comment