Exemplo n.º 1
0
Arquivo: main.py Projeto: Heyaowen/AAA
def send_alarm_msg(key):
    """ 发送定时提醒 """
    print('\n启动定时自动提醒...')
    conf = config.get('alarm_info').get('alarm_dict')

    gf = conf.get(key)
    # print(gf)air_quality_city
    is_tomorrow = gf.get('is_tomorrow', False)
    calendar_info = get_calendar_info(gf.get('calendar'), is_tomorrow)
    weather = get_weather_info(gf.get('city_name'), is_tomorrow)
    horoscope = get_constellation_info(gf.get("horescope"), is_tomorrow)
    dictum = get_dictum_info(gf.get('dictum_channel'))
    diff_time = get_diff_time(gf.get('start_date'), gf.get('start_date_msg'))
    air_quality = get_air_quality(gf.get('air_quality_city'))
    global i
    sweet_words = gf.get('sweet_words')[i] + '- 定时提醒'
    send_msg = '\n'.join(x for x in [
        calendar_info, weather, air_quality, horoscope, dictum, diff_time,
        sweet_words
    ] if x)
    # print('\n' + send_msg + '\n')
    if not send_msg or not is_online(): return
    uuid_list = gf.get('uuid_list')
    for uuid in uuid_list:
        time.sleep(1)
        itchat.send(send_msg, toUserName=uuid)
    print('\n定时内容:\n{}\n发送成功...\n\n'.format(send_msg))
    print('自动提醒消息发送完成...\n')
    i += 1
Exemplo n.º 2
0
    def test_all_info(self):
        """
        测试获取提醒的所有信息。
        :return:
        """
        girlfriend_infos = config.get('alarm_info').get('girlfriend_infos')
        for gf in girlfriend_infos:
            is_tomorrow = gf.get('is_tomorrow', False)
            calendar_info = get_calendar_info(gf.get('calendar'), is_tomorrow)
            weather = get_weather_info(gf.get('city_name'), is_tomorrow)
            horoscope = get_constellation_info(gf.get("horescope"),
                                               is_tomorrow)
            dictum = get_dictum_info(gf.get('dictum_channel'))
            diff_time = get_diff_time(gf.get('start_date'),
                                      gf.get('start_date_msg'))
            sweet_words = gf.get('sweet_words')

            air_quality = get_air_quality(gf.get('air_quality_city'))

            send_msg = '\n'.join(x for x in [
                calendar_info, weather, horoscope, air_quality, dictum,
                diff_time, sweet_words
            ] if x)
            print(send_msg)
            print('\n' + '-' * 50 + '\n')
Exemplo n.º 3
0
def send_alarm_msg():
    print('\n获取消息...')
    gf = config.get('alarm_info').get('girlfriend_infos')[0]
    is_tomorrow = gf.get('is_tomorrow', False)
    # calendar_info = get_calendar_info(gf.get('calendar'), gf.get('app_token'), is_tomorrow)
    weather = get_weather_info(gf.get('city_name'), gf.get('app_token'), is_tomorrow)
    horoscope = get_constellation_info(gf.get("horescope"), is_tomorrow)
    dictum = get_dictum_info(gf.get('dictum_channel'))
    diff_time = get_diff_time(gf.get('start_date'), gf.get('start_date_msg'))
    air_quality = get_air_quality(gf.get('air_quality_city'))
    sweet_words = gf.get('sweet_words')

    list_data = []
    for x in [weather, air_quality, horoscope, dictum, diff_time, sweet_words]:
        if x:
            list_data.append(x)
            
    send_msg = '  \n'.join(list_data) # 必须添加两个空格加换行
    print('\n' + send_msg + '\n')
    # pyperclip.copy(send_msg)
    with open('./result.txt', 'w', encoding='utf-8') as f:
        f.write(send_msg)
    form = {
        'title': '每日一句',
        'desp': send_msg
    }
    send_key = os.environ.get('SEND_KEY')
    # print(send_key)
    resp = requests.post('https://sctapi.ftqq.com/{}.send'.format(send_key), form)
    print(resp)
    if resp.status_code == 200:
        print('发送成功!')
Exemplo n.º 4
0
    def test_all_data(self):
        city = '宁波'
        info = find_air_quality(city)
        if info:
            print(info)
            return

        info = get_air_quality(city)
        if info:
            print(info)
            udpate_air_quality(city, info)
Exemplo n.º 5
0
def handle_group_helper(msg):
    """
    处理群消息
    :param msg:
    :return:
    """
    uuid = msg.fromUserName  # 群 uid
    ated_uuid = msg.actualUserName  # 艾特你的用户的uuid
    ated_name = msg.actualNickName  # 艾特你的人的群里的名称
    text = msg['Text']  # 发送到群里的消息。

    # 自己通过手机端微信发出的消息不作处理
    if ated_uuid == config.get('wechat_uuid'):
        return

    conf = config.get('group_helper_conf')
    if not conf.get('is_open'):
        return

    # 如果开启了 『艾特才回复』,而群用户又没有艾特你。不处理消息
    if conf.get('is_at') and not msg.isAt:
        return

    is_all = conf.get('is_all', False)
    user_uuids = conf.get('group_black_uuids') if is_all else conf.get(
        'group_white_uuids')
    # 开启回复所有群,而用户是黑名单,不处理消息
    if is_all and uuid in user_uuids:
        return

    # 未回复所有群,而用户不是白名单,不处理消息
    if not is_all and uuid not in user_uuids:
        return
    # 去掉 at 标记
    text = re.sub(at_compile, '', text)

    # 如果是帮助设置
    helps = re.findall(help_complie, text, re.I)
    if helps:
        retext = help_group_content.format(ated_name=ated_name)
        itchat.send(retext, uuid)
        return

    # 是否是明天,用于日历,天气,星座查询
    is_tomorrow = re.findall(tomorrow_compile, text)
    if is_tomorrow:
        is_tomorrow = True
        htext = re.sub(tomorrow_compile, '', text)
    else:
        is_tomorrow = False
        htext = text

    htext = re.sub(punct_complie, '', htext)  # 去句末的标点

    # 已开启天气查询,并包括天气关键词
    if conf.get('is_weather'):
        if re.findall(weather_compile, htext, re.I):
            city = re.sub(weather_clean_compile, '', text,
                          flags=re.IGNORECASE).strip()

            if not city:  # 如果只是输入城市名
                # 从缓存数据库找最后一次查询的城市名
                city = find_user_city(ated_uuid)
            if not city:  # 缓存数据库没有保存,通过用户的资料查城市
                city = get_city_by_uuid(ated_uuid)
            if not city:
                retext = weather_null_msg.format(ated_name=ated_name)
                itchat.send(retext, uuid)
                return

            _date = datetime.now().strftime('%Y-%m-%d')
            weather_info = find_weather(_date, city)
            if weather_info:
                retext = common_msg.format(ated_name=ated_name,
                                           text=weather_info)
                itchat.send(retext, uuid)
                return

            weather_info = get_weather_info(city)
            if weather_info:
                # print(ated_name, city, retext)
                retext = common_msg.format(ated_name=ated_name,
                                           text=weather_info)
                itchat.send(retext, uuid)

                data = {
                    '_date': _date,
                    'city_name': city,
                    'weather_info': weather_info,
                    'userid': ated_uuid,
                    'last_time': datetime.now()
                }
                udpate_weather(data)
                # userid,city_name,last_time,group_name udpate_weather_city
                data2 = {
                    'userid': ated_uuid,
                    'city_name': city,
                    'last_time': datetime.now()
                }
                udpate_user_city(data2)
                return
            else:
                retext = weather_error_msg.format(ated_name=ated_name,
                                                  city=city)
                itchat.send(retext, uuid)
                return
            return

    # 已开启日历,并包含日历
    if conf.get('is_calendar'):
        if re.findall(calendar_complie, htext, flags=re.IGNORECASE):

            calendar_text = re.sub(calendar_complie, '', htext).strip()
            if calendar_text:  # 日历后面填上日期了
                dates = re.findall(calendar_date_compile, calendar_text)
                if not dates:
                    retext = calendar_error_msg.format(ated_name=ated_name)
                    itchat.send(retext, uuid)
                    return

                _date = '{}-{:0>2}-{:0>2}'.format(*dates[0])  # 用于保存数据库
                rt_date = '{}{:0>2}{:0>2}'.format(*dates[0])  # 用于查询日历
            else:  # 日历 后面没有日期,则默认使用今日。
                _date = datetime.now().strftime('%Y-%m-%d')
                rt_date = datetime.now().strftime('%Y%m%d')

            # 从数据库缓存中记取内容
            cale_info = find_perpetual_calendar(_date)
            if cale_info:
                retext = common_msg.format(ated_name=ated_name, text=cale_info)
                itchat.send(retext, uuid)
                return

            # 取网络数据
            cale_info = get_rtcalendar(rt_date)
            if cale_info:
                retext = common_msg.format(ated_name=ated_name, text=cale_info)
                itchat.send(retext, uuid)
                update_perpetual_calendar(_date, cale_info)  # 保存数据到数据库
                return
            else:  # 查询无结果
                retext = calendar_no_result_msg.format(ated_name=ated_name,
                                                       _date=_date)
                itchat.send(retext, uuid)
            return

    # 垃圾分类查询
    if conf.get('is_rubbish'):
        if re.findall(rubbish_complie, htext, re.I):
            key = re.sub(rubbish_complie, '', htext,
                         flags=re.IGNORECASE).strip()
            if not key:
                retext = rubbish_null_msg.format(ated_name=ated_name)
                itchat.send(retext, uuid)
                return

            _type = find_rubbish(key)
            if _type:
                retext = rubbish_normal_msg.format(ated_name=ated_name,
                                                   name=key,
                                                   _type=_type)
                itchat.send(retext, uuid)
                return
            _type, return_list, other = get_atoolbox_rubbish(key)
            if _type:
                retext = rubbish_normal_msg.format(ated_name=ated_name,
                                                   name=key,
                                                   _type=_type)
                itchat.send_msg(retext, uuid)
            elif other:
                retext = rubbish_other_msg.format(ated_name=ated_name,
                                                  name=key,
                                                  other=other)
                itchat.send_msg(retext, uuid)
            else:
                retext = rubbish_nothing_msg.format(ated_name=ated_name,
                                                    name=key)
                itchat.send_msg(retext, uuid)
            if return_list:
                update_rubbish(return_list)  # 保存数据库
            return

    if conf.get('is_moviebox'):
        if re.findall(moviebox_complie, htext, re.I):
            moviebox_text = re.sub(moviebox_complie, '', htext).strip()
            if moviebox_text:  # 日历后面填上日期了
                dates = re.findall(calendar_date_compile, moviebox_text)
                if not dates:
                    retext = calendar_error_msg.format(ated_name=ated_name)
                    itchat.send(retext, uuid)
                    return
                _date = '{}{:0>2}{:0>2}'.format(*dates[0])
            else:  # 日历 后面没有日期,则默认使用今日。
                _date = datetime.now().strftime('%Y%m%d')
            # 从数据库缓存中记取内容
            mb_info = find_movie_box(_date)
            if mb_info:
                retext = common_msg.format(ated_name=ated_name, text=mb_info)
                itchat.send(retext, uuid)
                return

            is_expired = False
            cur_date = datetime.now().date()
            query_date = datetime.strptime(_date, '%Y%m%d').date()

            if query_date < cur_date:
                is_expired = True

            # 取网络数据
            mb_info = get_maoyan_movie_box(_date, is_expired)
            if mb_info:
                retext = common_msg.format(ated_name=ated_name, text=mb_info)
                itchat.send(retext, uuid)
                update_movie_box(_date, mb_info, is_expired)  # 保存数据到数据库
                return
            else:  # 查询无结果
                retext = moiebox_no_result_msg.format(ated_name=ated_name,
                                                      _date=_date)
                itchat.send(retext, uuid)
            return

    # 处理订单号
    if conf.get('is_express'):
        express_list = re.findall(express_complie, htext, re.I)
        if express_list:
            express_code = express_list[0]
            db_data = find_express(express_code, uuid)
            shipper_code, shipper_name = '', ''
            if db_data:
                if not db_data['is_forced_update']:
                    info = db_data['info']
                    retext = common_msg.format(ated_name=ated_name, text=info)
                    itchat.send(retext, uuid)
                    return
                shipper_code = db_data['shipper_code']
                shipper_name = db_data['shipper_name']

            data = get_express_info(express_code,
                                    shipper_name=shipper_name,
                                    shipper_code=shipper_code)
            if data:
                info = data['info']
                retext = common_msg.format(ated_name=ated_name, text=info)
                itchat.send(retext, uuid)
                update_express(data, uuid)
                return
            else:
                print('未查询到此订单号的快递物流轨迹。')
                return

    # 处理空气质量查询号
    if conf.get('is_air_quality'):
        if re.findall(air_compile, htext, re.I):
            city = re.sub(air_clean_compile, '', text,
                          flags=re.IGNORECASE).strip()
            if not city:  # 如果只是输入城市名
                # 从缓存数据库找最后一次查询的城市名
                city = find_user_city(ated_uuid)
            if not city:  # 缓存数据库没有保存,通过用户的资料查城市
                city = get_city_by_uuid(ated_uuid)
            if not city:
                retext = air_city_null_msg.format(ated_name=ated_name)
                itchat.send(retext, uuid)
                return

            info = find_air_quality(city)
            if info:
                retext = common_msg.format(ated_name=ated_name, text=info)
                itchat.send(retext, uuid)
                return
            info = get_air_quality(city)
            if info:
                retext = common_msg.format(ated_name=ated_name, text=info)
                itchat.send(retext, uuid)

                udpate_air_quality(city, info)
                data2 = {
                    'userid': ated_uuid,
                    'city_name': city,
                    'last_time': datetime.now()
                }
                udpate_user_city(data2)
                return
            else:
                retext = air_error_msg.format(ated_name=ated_name, city=city)
                itchat.send(retext, uuid)
                return
            return

    # 其他结果都没有匹配到,走自动回复的路
    if conf.get('is_auto_reply'):
        reply_text = get_bot_info(text, ated_uuid)  # 获取自动回复
        if reply_text:  # 如内容不为空,回复消息
            reply_text = common_msg.format(ated_name=ated_name,
                                           text=reply_text)
            itchat.send(reply_text, uuid)
            print('回复{}:{}'.format(ated_name, reply_text))
        else:
            print('自动回复失败\n')
Exemplo n.º 6
0
    def test_db_get_and_save_data(self):

        city = '天津'
        info = get_air_quality(city)
        if info:
            udpate_air_quality(city, info)
Exemplo n.º 7
0
    def test_db_get_data(self):

        city = '天津'
        cc = get_air_quality(city)
        if cc:
            print(cc)