NiceLeeのBlog 用爱发电 bilibili~

Python 使用分享接口实现QQ信息发送(三)

2020-06-09
nIceLee

阅读:


接上一篇

一个登录措施

因为一时兴起,现在也母鸡服务器端cookie的持续生效时间,但从本地看到有很多cookie的生存期为会话,也有几个短的只有3天左右。感觉需要想办法延长一点。

尝试了PC端QQ的授权登录,需要参数url、QQ号clientuin、加密的验证参数clientkey
这些都可以通过过滤https://ssl.ptlogin2.qq.com/jump?clientuin得到(url也就是这个加后面一长串参数,目测参数都是几个固定的,也就没再分开整了)。

  • 当然,这只是一个补救措施,有使用限制,建议cookie失效时使用:
    • 本机的QQ必须在线
    • 本地局域网肯定毫无问题,但如果上传到云上可能由于异地登录检测而失效(尚未测试)
# coding=utf-8
import requests
import re
import time


def login(url, clientuin, clientkey):
    '''
     登录,获取cookies
    '''
    session = requests.session()
    headers = {
        'Host': 'ssl.ptlogin2.qq.com',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0',
        'Accept': '*/*',
        'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
        'Accept-Encoding': 'gzip, deflate, br',
        'Connection': 'keep-alive',
        'Referer': 'https://xui.ptlogin2.qq.com/',
        'Pragma': 'no-cache',
        'Cache-Control': 'no-cache',
        'TE': 'Trailers',
        'Cookie': 'clientuin=%s; clientkey=%s'%(clientuin, clientkey)
    }
    result = session.request(method='GET', url=url, headers=headers).text
    print(result)
    searchObj = re.search(r"(https://.*?)',", result)
    if searchObj:
        url = searchObj.group(1)
        headers = {
            'Host': 'ptlogin2.connect.qq.com',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
            'Accept-Encoding': 'gzip, deflate, br',
            'Connection': 'keep-alive',
            'Referer': 'https://xui.ptlogin2.qq.com/',
            'Pragma': 'no-cache',
            'Cache-Control': 'no-cache',
            'TE': 'Trailers',
        }
        result = session.request(method='GET', url=url, headers=headers, allow_redirects=False).text
        cookies = requests.utils.dict_from_cookiejar(session.cookies)
        print(cookies)
        cookie_str = ''
        for key in cookies:
            cookie_str += '%s=%s; '%(key, cookies[key])
        cookie_str = cookie_str[:-2]
        session.close()
        return cookie_str
    session.close()
    
def search(cookie, key, stype=0):
    '''
        查找好友
        type 0 普通用户, 1  群
    '''
    data = get_friends(cookie)["result"]
    result = []
    if stype == 0:
        users = data["info"]
        for user in users:
            if key in user["nick"] or ('markname' in user and key in user["markname"]) :
                result.append((user["uin"], user["nick"], data["t"]))
        return result
    else:
        groups = data["groups"]
        for group in groups:
            if key in group["name"] or key in group["gid"] :
                result.append((group["code"], group['name'], data["t"]))
    return result


def get_friends(cookie):
    '''
        获取好友列表
    '''
    url = 'https://cgi.connect.qq.com/qqconnectopen/openapi/get_user_friends?t=%d'%int(round(time.time() * 1000))
    headers = {
        'Host': 'cgi.connect.qq.com',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0',
        'Accept': '*/*',
        'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
        'Accept-Encoding': 'gzip, deflate, br',
        'Content-Type': 'utf-8',
        'Origin': 'https://cgi.connect.qq.com',
        'Connection': 'keep-alive',
        'Referer': 'https://cgi.connect.qq.com/proxy.html?t=20120217001&callback=1&id=1',
        'Pragma': 'no-cache',
        'Cache-Control': 'no-cache',
        'TE': 'Trailers',
        'Cookie': cookie
    }
    data = requests.get(url, timeout=10, headers=headers).json()
    print(data)
    return data

    
def send_msg(cookie, t , title, summary, content, imageUrl, targetUrl, \
             uid, stype=0):
    '''
    发送消息给好友
    '''
    url = "https://cgi.connect.qq.com/qqconnectopen/openapi/send_share"
    headers = {
        'Host': 'cgi.connect.qq.com',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0',
        'Accept': '*/*',
        'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
        'Accept-Encoding': 'gzip, deflate, br',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Origin': 'https://cgi.connect.qq.com',
        'Connection': 'keep-alive',
        'Referer': 'https://cgi.connect.qq.com/proxy.html?t=20120217001&callback=1&id=1',
        'Pragma': 'no-cache',
        'Cache-Control': 'no-cache',
        'TE': 'Trailers',
        'Cookie': cookie
    }
    params = {
        'dest': 'qq',
        'uins': '[{"type":%d,"id":%d}]' % (stype, uid),
        'dmList': '[]',
        'ldw': '534111139', # 疑似固定参数
        't': t,
        'imageUrl': imageUrl,
        'targetUrl': targetUrl,
        'title': title,
        'summary': summary,
        'content': content,
    }
    data = requests.post(url, timeout=10, headers=headers, data=params).json()
    print(data)
    if "msg" in data and "success" == data["msg"]:
        return True
    else:
        return False

    
if __name__ == '__main__':
    # 登录
    url = "https://ssl.ptlogin2.qq.com/jump?clientuin=..."
    clientuin = "xxx"
    clientkey = "xxx"
    cookie = login(url, clientuin, clientkey)
 
    print(cookie)
    # 查找符合条件的好友
    result = search(cookie, key= '小号', stype = 0)
    print(result)
    # 该次查询获取的参数t应该是长期有效的
     
    # 给第一个发送一条信息
    t = result[0][2]
    title = '标题'
    summary = '摘要'
    imageUrl = 'https://s1.hdslb.com/bfs/static/player/img/ploading.gif'  # '预览图'
    targetUrl = 'https://www.bilibili.com'  # '分享链接'
    content = '文字信息' # 为None时不发送
    send_msg(cookie, t, uid=result[0][0], title=title, summary=summary, imageUrl=imageUrl, targetUrl=targetUrl, content=content)
    


内容
隐藏