def add_friends_msg(msg): """ 监听添加好友请求 为了自动同意好友请求 """ conf = config.get('auto_reply_info') IS_AUTO_ADD_FRIEND = conf.get('is_auto_add_friend') add_friend_keys = ''.join(conf.get('auto_add_friend_keywords')) note_first_meet_text = '''等你等的好辛苦,很高心您的加入! ''' # 好友成功后的第一句话 add_friend_compile = re.compile('|'.join(i.strip() for i in re.split(r'[,,]+', add_friend_keys) if i), re.I) # 通过关键词同意加好友请求 itchat.get_friends(update=True) # 更新好友数据。 if not IS_AUTO_ADD_FRIEND: # 如果是已关闭添加好友功能,则直接返回 return userid = msg['RecommendInfo']['UserName'] nickname = msg['RecommendInfo']['NickName'] content = msg['RecommendInfo']['Content'] # 获取验证消息 if add_friend_compile.findall(content): time.sleep(random.randint(1, 2)) # 随机休眠(1~3)秒,用于防检测机器人 itchat.add_friend(**msg['Text']) # 同意加好友请求 time.sleep(random.randint(1, 2)) itchat.send(note_first_meet_text, userid) # 给刚交的朋友发送欢迎语句 note = '已添加好友:{}'.format(nickname) set_system_notice(note) else: note = '添加好友失败:用户「{}」 发来的验证消息「{}」。'.format(nickname, content) set_system_notice(note)
def pdd_share_text(group_name: str, group_material_id: str, app_key: str, secret_key: str, p_id: str): ''' :param group_name: :param material_id: :return: ''' try: offset = str(random.randint(1, 295)) # top.goods.list.query 好像只有300个商品 limit = str(random.randint(3, 5)) # client = PddApiClient(app_key=app_key, secret_key=secret_key) resp = client.call("pdd.ddk.top.goods.list.query", { "offset": offset, "limit": limit, "p_id": p_id }) except Exception as e: print(e) set_system_notice(f'''offset: {offset},\nlimit:{limit}\n\n发现问题''') pdd_share_text(group_name, group_material_id, app_key, secret_key, secret_key, p_id) for data in json.loads(resp.text)['top_goods_list_get_response']['list']: goods_id = data['goods_id'] goods_name = data['goods_name'] search_id = data['search_id'] goods_thumbnail_url = data['goods_thumbnail_url'] min_normal_price = int(data['min_normal_price']) # 原价 min_group_price = int(data['min_group_price']) # 折扣价 coupon_discount = int(data['coupon_discount']) # 券价 if min_group_price < min_normal_price: cal_price = min_group_price else: cal_price = min_normal_price cal_price_str = str(cal_price)[:len(str(cal_price)) - 2] if len( str(cal_price)[:len(str(cal_price)) - 2] ) > 0 else '0' + '.' + str(cal_price)[len(str(cal_price)) - 2:] price = str(cal_price - coupon_discount)[:len(str(cal_price - coupon_discount))-2] \ if len(str(cal_price - coupon_discount)[:len(str(cal_price - coupon_discount))-2]) > 0 else '0'+ '.' \ + str(cal_price - coupon_discount)[len(str(cal_price - coupon_discount))-2:] short_url = promotion_url_generate(app_key=app_key, secret_key=secret_key, p_id=p_id, goods_id_list=int(goods_id), search_id=search_id) groups = itchat.search_chatrooms(name=f'''{group_name}''') for room in groups: room_name = room['UserName'] time.sleep(random.randint(1, 5)) filename = save_pic(goods_thumbnail_url, goods_id) # 发送图片 itchat.send('@img@%s' % (f'''{filename}'''), room_name) time.sleep(random.randint(1, 3)) itchat.send( f''' {goods_name} \n【现价】¥{cal_price_str}\n【内部价】¥{price}\n-----------------\n抢购地址:\n{short_url}''', room_name) del_pic(filename)
def init_data(): """ 初始化微信所需数据 """ set_system_notice('登录成功') itchat.get_friends(update=True) # 更新好友数据。 itchat.get_chatrooms(update=True) # 更新群聊数据。 conf = config.get('group_helper_conf') group_name_list = conf.get('group_name_white_list') init_chatsroom(group_name_list) init_wechat_config() # 初始化所有配置内容 init_alarm() print('初始化完成,开始正常工作。')
def handle_friends_message(msg): """ 处理好友信息 """ try: # 自己通过手机微信发送给别人的消息(文件传输助手除外)不作处理。 if msg['FromUserName'] == config.get( 'wechat_uuid') and msg['ToUserName'] != FILEHELPER: return conf = config.get('auto_reply_info') if not conf.get('is_auto_reply'): return # 获取发送者的用户id uuid = FILEHELPER if msg['ToUserName'] == FILEHELPER else msg[ 'FromUserName'] is_all = conf.get('is_auto_reply_all') auto_uuids = conf.get( 'auto_reply_black_uuids') if is_all else conf.get( 'auto_reply_white_uuids') # 开启回复所有人,当用户是黑名单,不回复消息 if is_all and uuid in auto_uuids: return # 关闭回复所有人,当用户不是白名单,不回复消息 if not is_all and uuid not in auto_uuids: return receive_text = msg.text # 好友发送来的消息内容 # 好友叫啥,用于打印 nick_name = FILEHELPER if uuid == FILEHELPER else msg.user.nickName reply_text = get_auto_reply(receive_text, uuid) # 获取自动回复 if reply_text: # 如内容不为空,回复消息 time.sleep(random.randint(1, 2)) # 休眠一秒,保安全。想更快的,可以直接注释。 prefix = conf.get('auto_reply_prefix', '') # 前缀 if prefix: reply_text = '{}{}'.format(prefix, reply_text) suffix = conf.get('auto_reply_suffix', '') # 后缀 if suffix: reply_text = '{}{}'.format(reply_text, suffix) itchat.send(reply_text, toUserName=uuid) else: set_system_notice( f'''自动回复失败:\n『{nick_name}』发来信息:{receive_text} \n''') except Exception as exception: print(str(exception))
def promotion_url_generate(app_key:str, secret_key:str, ad_book_id: str, comm_code: int, mert_code:str): client = api.StorepromotionurlQueryRequest() client.setAppInfo(app_key, secret_key) client.setDomainInfo("open.suning.com", "80") client.adBookId = ad_book_id client.commCode = comm_code client.mertCode = mert_code client.urlType = '2' try: resp = client.getResponse() short_url = resp['sn_responseContent']['sn_body']['queryStorepromotionurl']['wapExtendUrl'] short_url = short_url.replace('%3A%2F%2F', '://').replace('%2F', '/') except Exception as e: print(e) set_system_notice(f'''comm_code: {comm_code},\nmert_code:{mert_code}\nad_book_id:{ad_book_id}\n\n无法获取推广连接''') short_url = "" return short_url
def promotion_url_generate(app_key: str, secret_key: str, p_id: str, goods_id_list: int, search_id: str): client = PddApiClient(app_key=app_key, secret_key=secret_key) resp = client.call( "pdd.ddk.goods.promotion.url.generate", { "goods_id_list": f'''[{goods_id_list}]''', "search_id": search_id, "p_id": p_id }) try: short_url = json.loads( resp.text)['goods_promotion_url_generate_response'][ 'goods_promotion_url_list'][0]['mobile_short_url'] except Exception as e: print(e) set_system_notice( f'''goods_id_list: {goods_id_list},\nsearch_id:{search_id}\np_id:{p_id}\n\n无法获取连接''' ) short_url = "" return short_url
def pdd_share_text(group_name: str, group_material_id: str, app_key: str, secret_key: str, ad_book_id: str): ''' :param group_name: :param material_id: :return: ''' try: offset = str(random.randint(1, 295)) limit = str(random.randint(3, 5)) client = api.RecommendcommodityQueryRequest() client.setDomainInfo("openpre.cnsuning.com", "80") client.setAppInfo(app_key, secret_key) client.couponMark = '1' client.pageIndex = offset client.size = limit resp = client.getResponse() except Exception as e: print(e) set_system_notice(f'''苏宁:offset: {offset},\nlimit:{limit}\n\n发现问题''') pdd_share_text(group_name, group_material_id, app_key, secret_key, secret_key, ad_book_id)
def sn_share_text(group_name: str, group_material_id: str, app_key:str, secret_key:str, ad_book_id: str): ''' :param group_name: :param material_id: :return: ''' try: offset = str(random.randint(1, 71)) limit = str(random.randint(5, 10)) print(f'''offset:{offset},limit:{limit}''') client = api.RecommendcommodityQueryRequest() client.setDomainInfo("open.suning.com", "80") client.setAppInfo(app_key, secret_key) client.couponMark = '1' client.pageIndex = offset client.size = limit resp = client.getResponse()['sn_responseContent']['sn_body']['queryRecommendcommodity'] for data in resp: title = data['commodityInfo']['commodityName'] # 商品名称 commodityCode = data['commodityInfo']['commodityCode'] # 商品编码 supplierCode = data['commodityInfo']['supplierCode'] # 店铺编码 sellingPoint = data['commodityInfo']['sellingPoint'] # 卖点 snPrice = data['commodityInfo']['snPrice'] # 原价 commodityPrice = data['commodityInfo']['commodityPrice'] # 内部价 baoyou = data['commodityInfo']['baoyou'] # 内部价 if baoyou == 1: sellingPoint = f'''包邮 {sellingPoint} ''' images_count = 0 for image in data['commodityInfo']['pictureUrl']: images_count += 1 if images_count > 3: ## 3个以上图片就不发了 pass else: image_url = image['picUrl'].replace('_200w_200h_4e','') print(image_url) groups = itchat.search_chatrooms(name=f'''{group_name}''') for room in groups: room_name = room['UserName'] time.sleep(random.randint(5, 10)) filename = save_pic(image_url, commodityCode) itchat.send('@img@%s' % (f'''{filename}'''), room_name) del_pic(filename) pgPrice = data['pgInfo']['pgPrice'] # 拼购价 pgNum = data['pgInfo']['pgNum'] # 拼购价 couponValue = data['couponInfo']['couponValue'] # 优惠券面额 bounsLimit = data['couponInfo']['bounsLimit'] # 使用下限(满多少可用) afterCouponPrice = data['couponInfo']['afterCouponPrice'] # 使用下限(满多少可用) sn_share_url = promotion_url_generate(app_key, secret_key, ad_book_id, commodityCode, supplierCode.zfill(10)) if pgPrice == '': #不是拼购单 if couponValue == '': # 没有券 if float(snPrice) == float(commodityPrice): share_text = f'''{sellingPoint}\n【苏宁】{title}\n——————————\n 【爆款价】¥{commodityPrice}\n抢购地址:\n{sn_share_url}''' else: share_text = f'''{sellingPoint}\n【苏宁】{title}\n——————————\n 【原价】¥{snPrice}\n【爆款价】¥{commodityPrice}\n抢购地址:\n{sn_share_url}''' else: # 有券 bounsLimit = float(bounsLimit) couponValue = float(couponValue) commodityPrice = float(commodityPrice) if commodityPrice >= bounsLimit: # 如果商品满足满用券下限 if float(snPrice) == float(commodityPrice): share_text = f'''{sellingPoint}\n【苏宁】{title}\n领券再减{data['couponInfo']['couponValue']}元!\n——————————\n 【券后内部价】¥{round(float(commodityPrice-couponValue),2)}\n抢购地址:\n{sn_share_url}''' else: share_text = f'''{sellingPoint}\n【苏宁】{title}\n领券再减{data['couponInfo']['couponValue']}元!\n——————————\n 【原价】¥{snPrice}\n【券后内部价】¥{round(float(commodityPrice-couponValue),2)}\n抢购地址:\n{sn_share_url}''' else: buy_count = int(bounsLimit // commodityPrice + 1) if float(snPrice) == float(commodityPrice): if float(commodityPrice)*buy_count - float(data['couponInfo']['couponValue']) <=0: share_text = f'''{sellingPoint}\n【苏宁】{title}\n——————————\n 【爆款价】¥{afterCouponPrice}\n部分地区用户可领券再减,以实际优惠为准![哇][哇]\n抢购地址:\n{sn_share_url}''' else: share_text = f'''{sellingPoint}\n【苏宁】{title}\n——————————\n 【爆款价】¥{commodityPrice}\n拍{buy_count}件,用券再减{data['couponInfo']['couponValue']}元!{buy_count}件约{round(float(commodityPrice)*buy_count - float(data['couponInfo']['couponValue']),2)}元!\n抢购地址:\n{sn_share_url}''' else: if float(commodityPrice) * buy_count - float(data['couponInfo']['couponValue']) <= 0: share_text = f'''{sellingPoint}\n【苏宁】{title}\n——————————\n 【原价】¥{snPrice}\n【爆款价】¥{afterCouponPrice}\n部分地区用户可领券再减,具体以实际优惠为准![哇][哇]\n抢购地址:\n{sn_share_url}''' else: share_text = f'''{sellingPoint}\n【苏宁】{title}\n——————————\n 【原价】¥{snPrice}\n【爆款价】¥{commodityPrice}\n拍{buy_count}件,用券再减{data['couponInfo']['couponValue']}元!{buy_count}件约{round(float(commodityPrice)*buy_count - float(data['couponInfo']['couponValue']),2)}元!\n抢购地址:\n{sn_share_url}''' else: # 拼购单 if couponValue == '': # 没有券 if float(snPrice) == float(commodityPrice): share_text = f'''{sellingPoint}\n【苏宁{pgNum}人拼购】{title}\n——————————\n 【拼购价】¥{pgPrice}\n抢购地址:\n{sn_share_url}''' else: share_text = f'''{sellingPoint}\n【苏宁{pgNum}人拼购】{title}\n——————————\n 【原价】¥{snPrice}\n【拼购价】¥{pgPrice}\n抢购地址:\n{sn_share_url}''' else: # 有券 bounsLimit = float(bounsLimit) pgPrice = float(pgPrice) if pgPrice >= bounsLimit:# 如果拼购价格满足满用券下限 if float(snPrice) == float(pgPrice): share_text = f'''{sellingPoint}\n【苏宁{pgNum}人拼购】{title}\n领券再减{data['couponInfo']['couponValue']}元!\n——————————\n 【券后拼购价】¥{pgPrice}\n抢购地址:\n{sn_share_url}''' else: share_text = f'''{sellingPoint}\n【苏宁{pgNum}人拼购】{title}\n领券再减{data['couponInfo']['couponValue']}元!\n——————————\n 【原价】¥{snPrice}\n【券后拼购价】¥{pgPrice}\n抢购地址:\n{sn_share_url}''' else: buy_count = int(bounsLimit // pgPrice + 1) if float(snPrice) == float(commodityPrice): if float(commodityPrice) * buy_count - float(data['couponInfo']['couponValue']) <= 0: share_text = f'''{sellingPoint}\n【苏宁{pgNum}人拼购】{title}\n——————————\n 【爆款价】¥{afterCouponPrice}\n部分地区用户可领券再减,以实际优惠为准![哇][哇]\n抢购地址:\n{sn_share_url}''' else: share_text = f'''{sellingPoint}\n【苏宁{pgNum}人拼购】{title}\n——————————\n 【爆款价】¥{commodityPrice}\n拍{buy_count}件,用券再减{data['couponInfo']['couponValue']}元!{buy_count}件约{round(float(commodityPrice)*buy_count - float(data['couponInfo']['couponValue']),2)}元!\n抢购地址:\n{sn_share_url}''' else: if float(commodityPrice) * buy_count - float(data['couponInfo']['couponValue']) <= 0: share_text = f'''{sellingPoint}\n【苏宁{pgNum}人拼购】{title}\n——————————\n 【原价】¥{snPrice}\n【爆款价】¥{afterCouponPrice}\n部分地区用户可领券再减,以实际优惠为准![哇][哇]\n抢购地址:\n{sn_share_url}''' else: share_text = f'''{sellingPoint}\n【苏宁{pgNum}人拼购】{title}\n——————————\n 【原价】¥{snPrice}\n【爆款价】¥{commodityPrice}\n拍{buy_count}件,用券再减{data['couponInfo']['couponValue']}元!{buy_count}件约{round(float(commodityPrice)*buy_count - float(data['couponInfo']['couponValue']),2)}元!\n抢购地址:\n{sn_share_url}''' groups = itchat.search_chatrooms(name=f'''{group_name}''') for room in groups: room_name = room['UserName'] time.sleep(random.randint(3, 5)) print(share_text) itchat.send(share_text, room_name) except Exception as e: print(e) set_system_notice(f'''苏宁:offset: {offset},\nlimit:{limit}\n\n发现问题''') sn_share_text(group_name, group_material_id, app_key, secret_key, ad_book_id)
def jingfen_query(group_name: str, group_material_id: str, app_key: str, secret_key: str, site_id: str, suo_mi_token: str): ''' 方法效率不咋地,不管了 https://union.jd.com/openplatform/api/10421 :return: ''' info = [] try: page_no = str(random.randint(1, 25)) page_size = str(random.randint(3, 5)) # 不建议发很多,图片接口会跪 client = JdApiClient(app_key=app_key, secret_key=secret_key) resp = client.call( "jd.union.open.goods.jingfen.query", { "goodsReq": { "sort": "desc", "pageSize": page_size, "pageIndex": page_no, "eliteId": group_material_id } }) except Exception as e: print(e) set_system_notice( f'''page_no: {page_no},\npage_size:{page_size}\n, eliteId:{group_material_id}\n发现问题''' ) jingfen_query(group_name, group_material_id, app_key, secret_key, site_id, suo_mi_token) # pprint.pprint(json.loads(resp.json()['jd_union_open_goods_jingfen_query_response']['result'])) for data in json.loads( resp.json()['jd_union_open_goods_jingfen_query_response'] ['result'])['data']: print(data) sku_name = data['skuName'] ## 商品全名 sku_id = data['skuId'] ## 商品 sku material_url = f'''http://{(data['materialUrl'])}''' ## 商品url couponInfos = data['couponInfo'] ## 优惠券列表 # 查找最优优惠券 coupon_link = "" discount = 0 share_text = "" lowest_price_type = data['priceInfo']['lowestPriceType'] ## 什么类型 is_coupon = False for couponInfo in couponInfos['couponList']: if 'isBest' in couponInfo: if int(couponInfo['isBest']) == 1: discount = couponInfo['discount'] ## 优惠券额度 coupon_link = couponInfo['link'] ## 优惠券领取地址 is_coupon = True else: discount = couponInfo['discount'] ## 优惠券额度 coupon_link = couponInfo['link'] ## 优惠券领取地址 is_coupon = True if is_coupon: # 如果有券 if lowest_price_type == 3: # 秒杀 price = data['seckillInfo']['seckillOriPrice'] # 原价 lowest_price = data['priceInfo']['lowestCouponPrice'] # 秒杀价 duanzhi = tb_share_text(app_key, secret_key, material_url, coupon_link, site_id, suo_mi_token) share_text = f'''【秒杀】{sku_name}\n——————————\n 【原价】¥{price}\n 【券后秒杀价】¥{lowest_price}\n抢购地址:{duanzhi}''' elif lowest_price_type == 2: # 拼购 price = data['priceInfo']['price'] # 原价 lowest_price = data['priceInfo']['lowestCouponPrice'] # 用券拼购 duanzhi = tb_share_text(app_key, secret_key, material_url, coupon_link, site_id, suo_mi_token) share_text = f'''【拼购】{sku_name}\n——————————\n 【原价】¥{price}\n 【券后拼购价】¥{lowest_price}\n抢购地址:{duanzhi}''' else: price = data['priceInfo']['price'] ## 商品价格 lowest_price = data['priceInfo']['lowestCouponPrice'] duanzhi = tb_share_text(app_key, secret_key, material_url, coupon_link, site_id, suo_mi_token) share_text = f'''【京东】{sku_name}\n——————————\n 【爆款价】¥{price}\n 【用卷价】¥{lowest_price}\n抢购地址:{duanzhi}''' else: ## 如果没有券 if lowest_price_type == 3: # 秒杀 price = data['seckillInfo']['seckillOriPrice'] # 原价 lowest_price = data['seckillInfo']['seckillPrice'] # 秒杀价 duanzhi = tb_share_text(app_key, secret_key, material_url, coupon_link, site_id, suo_mi_token) share_text = f'''【秒杀】{sku_name}\n——————————\n 【原价】¥{price}\n 【秒杀价】¥{lowest_price}\n抢购地址:{duanzhi}''' elif lowest_price_type == 2: # 拼购 price = data['priceInfo']['price'] # 原价 lowest_price = data['priceInfo']['lowestPrice'] # 用券拼购 duanzhi = tb_share_text(app_key, secret_key, material_url, coupon_link, site_id, suo_mi_token) share_text = f'''【拼购】{sku_name}\n——————————\n 【原价】¥{price}\n 【拼购价】¥{lowest_price}\n抢购地址:{duanzhi}''' else: lowest_price = data['priceInfo']['price'] # 得到短址 duanzhi = tb_share_text(app_key, secret_key, material_url, coupon_link, site_id, suo_mi_token) share_text = f'''【京东】{sku_name}\n——————————\n 【爆款价】¥{lowest_price}\n抢购地址:{duanzhi}''' ## 获取 images image_list = [] images_count = 0 for image in data['imageInfo']['imageList']: images_count += 1 if images_count > 3: ## 3个以上图片就不发了 pass else: image_url = image['url'] filename = save_pic(image_url, sku_id) groups = itchat.search_chatrooms(name=f'''{group_name}''') for room in groups: room_name = room['UserName'] time.sleep(random.randint(5, 10)) itchat.send('@img@%s' % (f'''{filename}'''), room_name) del_pic(filename) # print(image_url) groups = itchat.search_chatrooms(name=f'''{group_name}''') for room in groups: room_name = room['UserName'] time.sleep(random.randint(3, 5)) itchat.send(share_text, room_name)