Exemple #1
0
    def unSub_Weahter(user, city, lang):
        UserHandler.logger.info(
            "User <%s> is un-subscribing the city <%s> weather" % (user, city))
        # QUERY
        user_sub_result = DBHandler().select(
            "SELECT Cities from WeatherSub WHERE Open_ID = '%s'" % user)
        content = ""
        if int(user_sub_result[0]) > 0:
            old_cities = user_sub_result[1][0][0].split()
            if city not in old_cities:
                content = "未订阅<%s>天气" % city
            else:
                old_cities.remove(city)
                if len(old_cities) > 0:
                    content = Resource.getMsg(
                        "UnSubWea", lang) % (city, " ".join(old_cities))
                    update_sql = "UPDATE WeatherSub SET Cities = '%s' WHERE Open_ID = '%s'" % (
                        " ".join(old_cities), user)
                    DBHandler().update(update_sql)
                else:
                    content = Resource.getMsg("UnSubAllWea", lang) % city
                    delete_sql = "DELETE from WeatherSub WHERE Open_ID = '%s'" % (
                        user)
                    DBHandler().delete(delete_sql)

        else:
            content = Resource.getMsg("NoSub", lang)
        return content
    def run(self, arg):
        today_match = {"": ""}
        while(1):
            all_user_birth = DBHandler().select("SELECT Open_ID,Birth from UserInfo")
            for user_line in all_user_birth[1]:
                user_id = user_line[0]
                if user_line[1] is not None:
                    user_birth = user_line[1].strftime("%Y-%m-%d")
                    birth_MM_DD = user_birth.split("-", 1)[1]
                else:
                    birth_MM_DD = ""

                now_MM_DD = time.strftime('%m-%d', time.localtime(time.time()))

                if birth_MM_DD == now_MM_DD and today_match.get(user_id, "NoRecord") != birth_MM_DD:
                    today_match[user_id] = now_MM_DD
                    print("match")
                    ret = WeChatHandler().sendMsgViaCust(Resource.getMsg("Birth") + Resource.getMsg("ReplyHappy"),
                                                         "touser", user_id)
                    if int(ret) != 0:
                        print("BirthDayNotifier Cust Msg failed..Use preview")
                        sleep(300)
                        WeChatHandler().sendMsgToOneAsPreview(Resource.getMsg("Birth") + Resource.getMsg("ReplyHappy"),
                                                       "touser", user_id)

                    ActionsExecutor.add_manual_action(user_id, Action(self.check_reply, user_id, "NoHappy"))
                    threading.Timer(3600, self.get_action, args=(user_id,)).start()
            self.logger.debug("Sleep Birth")
            sleep(120)
 def __notify(self, msg):
     if msg != "":
         for user in self.user_list:
             ret = WeChatHandler().sendMsgViaCust(
                 msg + Resource.getMsg("ReYes"), "touser", user)
             if int(ret) != 0:
                 print("HealthyNotifier Cust Msg failed..Use preview")
                 WeChatHandler().sendMsgToOneAsPreview(
                     msg + Resource.getMsg("ReYes"), "touser", user)
             self.user_wait_list.append(user)
             threading.Timer(300, self.clear_wait, (user, )).start()
    def onUnSub(user_open_ID):
        WeChatName = DBHandler().select(
            "SELECT WechatName from UserInfo WHERE Open_ID = '%s'" %
            user_open_ID)[1][0][0]
        if len(WeChatName) > 0:
            msg = Resource.getMsg("UnSub") % WeChatName
        else:
            msg = Resource.getMsg("UnSub") % user_open_ID

        ret = WeChatHandler().sendMsgViaCust(msg)
        if int(ret) != 0:
            print("onUnSub Cust Msg failed..Use preview")
            WeChatHandler().sendMsgToOneAsPreview(msg)
        return "No Action"
 def clear_wait(self, user_open_id):
     if user_open_id in self.user_wait_list:
         self.user_wait_list.remove(user_open_id)
         ret = WeChatHandler().sendMsgViaCust(Resource.getMsg("FiveMins"),
                                              "touser", user_open_id)
         if int(ret) != 0:
             print("HealthyNotifier Cust Msg failed..Use preview")
             WeChatHandler().sendMsgToOneAsPreview(
                 Resource.getMsg("FiveMins"), "touser", user_open_id)
         DBHandler().insert(
             "INSERT into HealthyRecord VALUES (null, '%s', 'N', null)" %
             user_open_id)
     else:
         #has record into DB when user reply the correct message
         pass
    def check_reply(self, user, msg, isTimer='N'):
        reply_msg = ""

        if msg != Resource.getMsg("IHappy"):
            reply_msg = Resource.getMsg("MustHappy") + Resource.get_random_birth_msg()
            ActionsExecutor.add_manual_action(user, Action(self.check_reply, user, "NoHappy"))
        else:
            reply_msg = Resource.getMsg("AlwaysHappy")

        if isTimer == "Y":
            ret = WeChatHandler().sendMsgViaCust(reply_msg, "touser", user)
            if int(ret) != 0:
                print("BirthDayNotifier.get_action  timer")
                WeChatHandler().sendMsgToOneAsPreview(reply_msg, "touser", user)
        self.logger.info("check_reply return %s" % reply_msg)
        return reply_msg
    def onSub(user_open_ID):
        user_dict = WeChatHandler().getUserInfo(user_open_ID)
        user_priority = UserHandler.verify_user(user_open_ID)
        if user_priority == -1:
            user_insert_sql = "INSERT into UserInfo VALUES ('%s', 'N', '%s', null, '%s', null, null, null, '%s', null, " \
                              "null, null, null)" % (
                              user_open_ID, user_dict['nickname'], TypeDef.sex_dict[user_dict['sex']],
                              (user_dict['city'] + ',' + user_dict['province'] + ',' + user_dict[
                                  'country']).replace("\'", "\\\'"))
            DBHandler().insert(user_insert_sql)
        else:
            update_sql = "UPDATE UserInfo SET WechatName = '%s',Sex='%s',Address='%s' WHERE Open_ID = '%s'"\
                         %  (user_dict['nickname'], TypeDef.sex_dict[user_dict['sex']],
                         (user_dict['city'] + ',' + user_dict['province'] + ',' + user_dict['country']).replace("\'", "\\\'"),
                            user_open_ID)
            DBHandler().update(update_sql)

        return_msg = Resource.getMsg("WlcMsg", user_dict['language'])
        '''
        if user_dict['language'] == "zh_CN":
            sql_query = "Select IDX,Media_ID,Title from HistoryArticle"
        else:
            sql_query = "Select IDX,Media_ID,Title from HistoryArticle WHERE Language = 'en'"
        results = DBHandler().select(sql_query)

        for line in results[1]:
            return_msg = return_msg + str(line[0]) + "..." + line[2] + "\n"

        threading.Timer(3, WeChatHandler().sendMsgViaCust,
                        (Resource.getMsg("Menu", user_dict['language']), "touser", user_open_ID)
                        ).start()
       '''
        return return_msg
Exemple #8
0
    def sub_user_for_weather(user, new_city, lang):
        UserHandler.logger.info(
            "User <%s> is subscribing the city <%s> weather" %
            (user, new_city))
        #QUERY
        user_sub_result = DBHandler().select(
            "SELECT Cities from WeatherSub WHERE Open_ID = '%s'" % user)
        content = ""

        if len(new_city.split()) == 0 and int(user_sub_result[0]) > 0:
            content = Resource.getMsg(
                "WeatherHead", lang) + user_sub_result[1][0][
                    0]  #only one cell here, use [0][0] to visit
        elif int(user_sub_result[0]) == 0 and len(new_city.split()) == 0:
            content = Resource.getMsg("NoSubCity", lang)
        #INSERT
        elif int(user_sub_result[0]) == 0 and len(new_city.split()) != 0:
            weather = WeatherHandler()
            ret = weather.getWeather(new_city, lang)
            if ret != "Failed":
                content = Resource.getMsg("FirstSub", lang) % new_city + ret
                # write sub info into db
                insert_sql = "INSERT into WeatherSub VALUES ('%s', '%s', NULL)" % (
                    user, new_city)
                DBHandler().insert(insert_sql)
            else:
                content = Resource.getMsg("WrongCity", lang)
        #UPDATE
        elif int(user_sub_result[0]) > 0 and len(new_city.split()) != 0:
            old_cities = user_sub_result[1][0][0].split()
            if len(old_cities) > 5:
                content = Resource.getMsg("MAXCityLimit", lang)
            elif new_city in old_cities:
                content = Resource.getMsg("Subbed", lang) % new_city
            else:
                weather = WeatherHandler()
                ret = weather.getWeather(
                    new_city,
                    WeChatHandler().getUserInfo(user)['language'])
                if ret != "Failed":
                    old_cities.append(new_city)
                    content = Resource.getMsg(
                        "SubbedMore", lang) % " ".join(old_cities) + ret
                    update_sql = "UPDATE WeatherSub SET Cities = '%s' WHERE Open_ID = '%s'" % (
                        " ".join(old_cities), user)
                    DBHandler().update(update_sql)
                else:
                    content = Resource.getMsg("WrongCity", lang)
        else:
            content = Resource.getMsg("UnKnownIssue", lang)
        return content
Exemple #9
0
 def user_check_weather(user, city, lang):
     UserHandler.logger.info("User <%s> is checking the city <%s> weather" %
                             (user, city))
     content = ""
     ret = WeatherHandler().getWeather(city, lang)
     if ret != "Failed":
         content = ret
     else:
         content = Resource.getMsg("WrongCity", lang)
     return content
Exemple #10
0
    def run(argc):
        while (1):
            if Utill.night_check_time() == -1 or os.path.exists(
                    "/wechat/data/skip_weather"):
                sleep(60)
                continue
            user_sub_result = DBHandler().select(
                "SELECT Open_ID,Cities from WeatherSub")
            for user_sub in user_sub_result[1]:
                user = user_sub[0]
                user_info = WeChatHandler().getUserInfo(user)
                if user_info['subscribe'] == 0:
                    #User has un-usb our channel
                    continue

                cities = user_sub[1].split()
                if user == "oHBF6wR4kUe4KUNtMMN4J0LKXsPE":
                    sub_weather_text = Resource.getMsg("StupidHead")
                else:
                    sub_weather_text = Resource.getMsg("NormalHead",
                                                       user_info['language'])

                if len(cities) == 0:  #in case empty files
                    continue

                for city in cities:
                    sub_weather_text += WeatherHandler().getWeather(
                        city, user_info['language'])
                    sub_weather_text += "-------------------\n"

                # wechat define 2 types sending way"
                # "touser" : sending wit OpenID
                # "towxname" sending with wechat name
                WeChatHandler().sendMsgToOneAsPreview(sub_weather_text,
                                                      "touser", user)
                PublisherToSub.logger.info("Sent weather to %s. Weather: %s" %
                                           (user, sub_weather_text))
                #end for to send weather for a person
            #sending per 2 hours
            sleep(7200)
 def check_wait(self, user, msg):
     content = ""
     counts = 0
     if user in self.user_wait_list and user == self.user_list[
             0] and msg == "是":
         DBHandler().insert(
             "INSERT into HealthyRecord VALUES (null, '%s', 'Y', null)" %
             user)
         counts = DBHandler().select(
             "SELECT CreateData from HealthyRecord WHERE IsRecord = 'Y' and CreateData > '2017-09' \
         and CreateData < '2017-10' AND Open_ID = '%s'" % user)[0]
         content = Resource.getMsg("RecordFmt") % (
             Resource.getMsg("GodSub"), str(counts))
         self.user_wait_list.remove(user)
         if Utill.is_last_day():
             content = content + Resource.getMsg("BillHealty") % (counts,
                                                                  counts)
     elif user in self.user_wait_list and user == self.user_list[
             1] and msg == "是":
         DBHandler().insert(
             "INSERT into HealthyRecord VALUES (null, '%s', 'Y', null)" %
             user)
         counts = DBHandler().select(
             "SELECT CreateData from HealthyRecord WHERE IsRecord = 'Y' and CreateData > '2017-09' \
         and CreateData < '2017-10' AND Open_ID = '%s'" % user)[0]
         content = Resource.getMsg("RecordFmt") % (
             Resource.getMsg("LingSub"), str(counts))
         self.user_wait_list.remove(user)
         if Utill.is_last_day():
             content = content + Resource.getMsg("BillHealty") % (counts,
                                                                  counts)
     return content
 def __nap_check(self):
     content = ""
     local_time = time.strftime('%H:%M', time.localtime(time.time()))
     if local_time in self.healthy_metrics.get("nap"):
         content = Resource.getMsg("NapTime")
     return content
 def showAboutMe(user_open_ID):
     user_dict = WeChatHandler().getUserInfo(user_open_ID)
     lang = user_dict['language']
     return Resource.getMsg("AboutMe", lang)