Beispiel #1
0
    def __get_log_sig(self):
        url = QQConstants.URL_LOGIN_PAGE
        response = self.context.http_service.get(url)
        if not response:
            raise WebQQException("__get_log_sig failed")
        #logging.info("response of GET_LOG_SIG:%s", response.content)

        # REGXP_LOGIN_SIG
        rexp = 'var g_login_sig=encodeURIComponent\("(.*?)"\)'
        m = re.search(rexp, response.content)
        if not m:
            raise WebQQException("__get_log_sig failed, rexp=%s" % rexp)

        self.login_sig = m.group(1)
        return True
Beispiel #2
0
    def get_group_info(self, group):
        # URL_GET_GROUP_INFO_EXT
        url = "http://s.web2.qq.com/api/get_group_info_ext2"
        parameters = {
            'gcode': group.code,
            'vfwebqq': self.context.qq_session.vfwebqq,
            't': str(int(time.time()))
        }

        response = self.context.http_service.get(url, parameters)
        if not response:
            raise WebQQException("get_group_list failed")
        logging.info("response of GET_GROUP_INFO:%s", response.content)
        data = json.loads(response.text, encoding='utf-8')

        if not data:
            logging.error(
                "get_group_info failed.there is no json data return.")
            return False

        if data["retcode"] != 0:
            logging.error("get group info failed.retcode:%s,errmsg:%s",
                          (data["retcode"], data.get("errmsg")))
            return False

        results = data["result"]
        GroupModule.parse_group_info(results, group)

        return True
Beispiel #3
0
    def get_category_list(self):
        # URL_GET_USER_CATEGORIES
        url = "http://s.web2.qq.com/api/get_user_friends2"

        ptwebqq = self.context.http_service.get_cookie_value('ptwebqq')
        hash_value = QQEncryptor.hash(self.context.account.uin, ptwebqq)
        payload = json.dumps({
            "h": "hello",
            "vfwebqq": self.context.qq_session.vfwebqq,
            "hash": hash_value,
        })
        post_data = "r=%s" % urllib.quote(payload)
        response = self.context.http_service.post(url, post_data)
        if not response:
            raise WebQQException("get_group_list failed")
        logging.info("response of GET_CATEGORY_LIST:%s", response.content)

        data = json.loads(response.text)
        if data["retcode"] != 0:
            logging.error(
                "get group category list failed.retcode:%s,errmsg:%s",
                (data["retcode"], data.get("errmsg")))
            return False

        # return self.parse_response(data)
        return self.parse_response(response.json())
Beispiel #4
0
    def __do_send_message(self, message):
        # ptwebqq = HttpCookies.get_value('ptwebqq')
        # hash_value = QQEncryptor.hash(self.account.uin, ptwebqq)
        text = message.message
        if isinstance(text, unicode):
            text = text.encode('utf8')
        text.replace('"', '\\"')

        #{"to":2754143906,"content":"[\"hi\",[\"font\",{\"name\":\"宋体\",\"size\":10,\"style\":[0,0,0],\"color\":\"000000\"}]]","face":558,"clientid":53999199,"msg_id":68900001,"psessionid":"8368046764001d636f6e6e7365727665725f77656271714031302e3133392e372e31363400002a8d00000143036e0400f00ea2b56d0000000a40315872377232714c646d00000028ac8fad9c82e24709d6a606cb2388fda292f49d8e7f73a36457414274a4d55f36dd204f1f704845be"}
        payload = {
            "content":
            '[\"%s\",[\"font\",{\"name\":\"宋体\",\"size\":10,\"style\":[0,0,0],\"color\":\"000000\"}]]'
            % text,
            "msg_id":
            random.randint(10000000, 99999999),
            "clientid":
            self.context.qq_session.client_id,
            "psessionid":
            self.context.qq_session.session_id
        }

        if message.type == QQMessage.Type.BUDDY_MSG:
            # URL_SEND_BUDDY_MSG
            url = "http://d.web2.qq.com/channel/send_buddy_msg2"
            payload["to"] = int(message.to_user.uin)
            payload["face"] = 558
        elif message.type == QQMessage.Type.GROUP_MSG:
            # URL_SEND_GROUP_MSG
            url = "http://d.web2.qq.com/channel/send_qun_msg2"
            payload["group_uin"] = message.group.gin
            payload["face"] = 558
        elif message.type == QQMessage.Type.DISCUZ_MSG:
            return True
        # req = createHttpRequest("POST", QQConstants.URL_SEND_DISCUZ_MSG);
        # json.put("did", msg.getDiscuz().getDid());
        # json.put("key", session.getCfaceKey());
        # json.put("sig", session.getCfaceSig());
        elif message.type == QQMessage.Type.SESSION_MSG:  # 临时会话消息
            return True
            # req = createHttpRequest("POST", QQConstants.URL_SEND_SESSION_MSG);
            # QQStranger member =  (QQStranger) msg.getTo();
            # json.put("to", member.getUin());
            # json.put("face", 0); // 这个是干嘛的??
            # json.put("group_sig", member.getGroupSig());
            # json.put("service_type", member.getServiceType() + "");
        else:
            print "unknown MsgType: ", message.type
            return True

        json_data = json.dumps(payload)
        post_data = "r=%s&clientid=%s&psessionid=%s" % (
            urllib.quote(json_data), self.context.qq_session.client_id,
            self.context.qq_session.session_id)

        logging.debug("send message.post_data: %s", post_data)
        response = self.context.http_service.post(url, post_data)
        if not response:
            raise WebQQException("send message failed")
        logging.info("response of SEND_MESSAGE:%s", response.content)
Beispiel #5
0
    def channel_login(self):
        # URL_CHANNEL_LOGIN
        url = "http://d.web2.qq.com/channel/login2"
        if not self.context.qq_session.client_id:
            self.context.qq_session.client_id = str(random.randint(1, 99999999))

        payload = json.dumps({
            "status": "online",
            "ptwebqq": self.context.http_service.get_cookie_value('ptwebqq'),
            "clientid": self.context.qq_session.client_id,
            "psessionid": self.context.qq_session.session_id
        })
        post_data = "r=%s" % urllib.quote(payload)
        response = self.context.http_service.post(url, post_data)
        if not response:
            raise WebQQException("__channel_login failed")
        logging.info("response of CHANNEL_LOGIN:%s", response.content)

        try:
            data = json.loads(response.text, encoding='utf-8')
            if data["retcode"] != 0:
                info = "channel login failed! errcode=%s, errmsg=%s" % (data["retcode"], data["errmsg"])
                logging.error(info)
                raise WebQQException(info)

            ret = data["result"]
            self.context.account.uin = str(ret["uin"])
            self.context.account.fake_qq = str(ret["uin"])
            self.context.account.status = ret["status"]
            self.context.qq_session.session_id = ret["psessionid"]
            self.context.qq_session.vfwebqq = ret["vfwebqq"]
            self.context.qq_session.state = QQSession.State.ONLINE
            self.context.qq_session.index = ret["index"]
            self.context.qq_session.port = ret["port"]
        except ValueError:
            logging.exception("channel login failed json format error")
            raise WebQQException("channel login failed json format error")
Beispiel #6
0
    def __check_verify(self):
        user_name = self.context.account.user_name
        url = QQConstants.URL_CHECK_VERIFY.format(user_name, self.login_sig, random.random())
        response = self.context.http_service.get(url)
        if not response:
            raise WebQQException("__check_verify failed")
        logging.info("response of CHECK_VERIFY:%s", response.content)

        # REGXP_CHECK_VERIFY
        regxp = "ptui_checkVC\('(.*?)','(.*?)','(.*?)'(,\s*'(.*?)')?\)"
        m = re.search(regxp, response.content)

        self.verify_code = m.group(2)
        self.context.account.uin_hex = m.group(3).replace("\\x", "")
        self.need_verify = m.group(1) == '1'
Beispiel #7
0
    def __get_captcha_image(self):
        # URL_GET_CAPTCHA
        url = "http://captcha.qq.com/getimage";
        parameters = {
            'aid': QQConstants.APPID,
            'r': str(random.random()),
            'uin': str(self.context.account.uin)
        }

        response = self.context.http_service.get(url, parameters)
        if not response:
            raise WebQQException("__get_captcha_image failed")

        config = Config()
        captcha_image = config.get("qq_adapter", "captcha_image")
        with open(captcha_image, 'wb') as outfile:
            outfile.write(response.content)
Beispiel #8
0
    def __web_login(self):
        # URL_UI_LOGIN
        url = "https://ssl.ptlogin2.qq.com/login"
        parameters = self.__construct_web_login_parameters()
        response = self.context.http_service.get(url, parameters)
        if not response:
            raise WebQQException("__web_login failed")
        logging.info("response of WEB_LOGIN:%s", response.content)

        # REGXP_LOGIN
        regxp = "ptuiCB\('(\d+)','(\d+)','(.*?)','(\d+)','(.*?)', '(.*?)'\)"

        m = re.search(regxp, response.content)
        result = m.group(1)
        url = m.group(3)
        reason = m.group(5)

        return result, url, reason
Beispiel #9
0
    def get_friend_info(self, user):
        # URL_GET_FRIEND_INFO
        url = "http://s.web2.qq.com/api/get_friend_info2"
        parameters = {
            'tuin': str(user.uin),
            'verifysession': "",
            'code': '',
            'vfwebqq': self.context.qq_session.vfwebqq,
            't': str(int(time.time()))
        }

        response = self.context.http_service.get(url, parameters)
        if not response:
            raise WebQQException("get_friend_info failed")
        logging.info("response of GET_FRIEND_INFO:%s", response.content)

        data = response.json()
        UserModule.__parse_user_info(data, user)
Beispiel #10
0
    def login(self):
        self.__get_log_sig()
        self.__check_verify()

        if self.need_verify:
            self.verify_code = self.__read_verify_code('为了保证您账号的安全,请输入验证码中字符继续登录')

        web_login_ret = self.__web_login()
        while web_login_ret[0] == '4':
            self.verify_code = self.__read_verify_code(web_login_ret[2])
            web_login_ret = self.__web_login()

        if web_login_ret[0] != '0':
            raise WebQQException("Login failed. retcode:%d", web_login_ret[0] )

        self.__check_login_sig(web_login_ret[1])

        self.channel_login()
        return True
Beispiel #11
0
    def get_group_list(self):
        # URL_GET_GROUP_NAME_LIST
        url = "http://s.web2.qq.com/api/get_group_name_list_mask2"

        ptwebqq = self.context.http_service.get_cookie_value('ptwebqq')
        hash_value = QQEncryptor.hash(self.context.account.uin, ptwebqq)
        payload = json.dumps({
            "vfwebqq": self.context.qq_session.vfwebqq,
            "hash": hash_value,
        })
        post_data = "r=%s" % urllib.quote(payload)
        response = self.context.http_service.post(url, post_data)
        if not response:
            raise WebQQException("get_group_list failed")
        logging.info("response of GET_GROUP_LIST:%s", response.content)

        data = json.loads(response.text, encoding='utf-8')
        if data["retcode"] != 0:
            logging.error("get group list failed.retcode:%s,errmsg:%s",
                          (data["retcode"], data.get("errmsg")))
            return False

        results = data["result"]
        group_list = results["gnamelist"]  # 群列表
        group_mask_list = results[
            "gmasklist"]  # 禁止接收群消息标志:正常 0, 接收不提醒 1, 完全屏蔽 2
        for g in group_list:
            group = QQGroup()
            group.gin = g['gid']
            group.code = g["code"]
            group.flag = g["flag"]
            group.name = to_str(g['name'])
            self.context.store.add_group(group)

        for mask in group_mask_list:
            gid = mask['gid']
            mask = mask['mask']
            group = self.context.store.get_group_by_gin(gid)
            if group:
                group.mask = mask

        return True
Beispiel #12
0
    def get_user_account(self, user):
        # URL_GET_USER_ACCOUNT
        url = "http://s.web2.qq.com/api/get_friend_uin2"
        parameters = {
            'tuin': str(user.uin),
            'verifysession': "",
            'gid': 0,
            'code': '',
            'vfwebqq': self.context.qq_session.vfwebqq,
            't': str(int(time.time()))
        }

        response = self.context.http_service.get(url, parameters)
        if not response:
            raise WebQQException("get_user_account failed")
        logging.info("response of GET_USER_ACCOUNT:%s", response.content)
        data = response.json()
        if data["retcode"] == 0:
            result = data["result"]
            user.qq = to_str(str(result["account"]))
Beispiel #13
0
    def __get_response(self):
        # URL_POLL_MSG
        url = "http://d.web2.qq.com/channel/poll2"

        ptwebqq = self.context.http_service.get_cookie_value('ptwebqq')
        hash_value = QQEncryptor.hash(self.context.account.uin, ptwebqq)
        payload = json.dumps({
            "clientid": self.context.qq_session.client_id,
            "psessionid": self.context.qq_session.session_id,
            "key": 0,
            "ids": []
        })
        post_data = "r=%s&clientid=%s&psessionid=%s" % (
            urllib.quote(payload), self.context.qq_session.client_id,
            self.context.qq_session.session_id)

        response = self.context.http_service.post(url, post_data)
        if not response:
            raise WebQQException("poll message failed")
        logging.debug("response of POLL_MESSAGE:%s", response.content)

        return response
Beispiel #14
0
 def __check_login_sig(self, url):
     response = self.context.http_service.get(url)
     if not response:
         raise WebQQException("__check_login_sig failed")
     logging.info("response of CHECK_LOGIN_SIG:%s", response.content)