NiceLeeのBlog 用爱发电 bilibili~

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

2020-06-09
nIceLee

阅读:


接上一篇

进一步实现

姑且不论cookie的有效时间,感觉参数t的生存时间不会太长,而且由于不是QQ号或者群号,感觉还是有点麻烦的。

现在的需求是,只要cookie和QQ名称/QQ号/群名称/群号,便能够朝目标发送一条信息。

# coding=utf-8
import requests
import time

    
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__':
 
    cookie = "pgv_pvi=xxx; ..."  
    # 查找符合条件的好友
    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)
    

内容
隐藏