def send_message(text, user=TG_SENDTO): try: BOT.send_message(user, text, parse_mode="Markdown") except: # try again try: BOT.send_message(user, text, parse_mode="Markdown") except: traceback_string = traceback.format_exc() print(traceback_string) print("! Error: failed to post message to Telegram!") write_log_warning(*traceback_string.splitlines()) write_log_warning("Failed to post message to Telegram!")
def check_one(cls, disable_pagecache=False): if isinstance(cls, str): cls_str = cls cls = {cls_.__name__: cls_ for cls_ in CHECK_LIST}.get(cls_str) if not cls: raise Exception("Can not found '%s' from CHECK_LIST!" % cls_str) cls_obj = cls() if disable_pagecache: cls_obj.enable_pagecache = False try: cls_obj.do_check() except exceptions.ReadTimeout: print_and_log("%s check failed! Timeout." % cls_obj.fullname, level="warning") except (exceptions.SSLError, exceptions.ProxyError): print_and_log("%s check failed! Proxy error." % cls_obj.fullname, level="warning") except exceptions.ConnectionError: print_and_log("%s check failed! Connection error." % cls_obj.fullname, level="warning") except exceptions.HTTPError as error: print_and_log("%s check failed! %s." % (cls_obj.fullname, error), level="warning") except: traceback_string = traceback.format_exc() print(traceback_string) write_log_warning(*traceback_string.splitlines()) print_and_log("%s check failed!" % cls_obj.fullname, level="warning") else: if cls_obj.is_updated() or FORCE_UPDATE: print_and_log( "%s has update: %s" % (cls_obj.fullname, cls_obj.info_dic["LATEST_VERSION"]), custom_prefix=">", ) try: cls_obj.after_check() except: traceback_string = traceback.format_exc() print("\n%s\n! Something wrong when running after_check!" % traceback_string) write_log_warning(*traceback_string.splitlines()) write_log_warning( "%s: Something wrong when running after_check!" % cls_obj.fullname) cls_obj.write_to_database() if ENABLE_SENDMESSAGE: cls_obj.send_message() else: print("- %s no update" % cls_obj.fullname) if not LESS_LOG: write_log_info("%s no update" % cls_obj.fullname) return True
def send_message(text, user=TG_SENDTO): for _ in range(10): try: BOT.send_message(user, text, parse_mode="Markdown", timeout=TIMEOUT) except (requests.exceptions.SSLError, requests.exceptions.ProxyError): continue except: traceback_string = traceback.format_exc() print("\n%s\n! Error: failed to post message to Telegram!" % traceback_string) write_log_warning(*traceback_string.splitlines()) write_log_warning("Failed to post message to Telegram!") break else: print("! F**k GFW!") write_log_warning("F**k GFW!")
def check_one(cls): if isinstance(cls, str): for item in CHECK_LIST: if item.__name__ == cls: cls = item break else: raise Exception("Can not found '%s' from CHECK_LIST!" % cls) cls_obj = cls() print("- Checking", cls_obj.fullname, "...", end="") try: cls_obj.do_check() except Exception as error: if isinstance(error, exceptions.ReadTimeout): print("\n! Check failed! Timeout.") write_log_warning("%s check failed! Timeout." % cls_obj.fullname) elif isinstance(error, (exceptions.SSLError, exceptions.ProxyError)): print("\n! Check failed! Proxy error.") write_log_warning("%s check failed! Proxy error." % cls_obj.fullname) elif isinstance(error, ErrorCode): print("\n! Check failed! Error code: %s." % error) write_log_warning("%s check failed! Error code: %s." % (cls_obj.fullname, error)) else: traceback_string = traceback.format_exc() print("\n%s\n! Check failed!" % traceback_string) write_log_warning(*traceback_string.splitlines()) write_log_warning("%s check failed!" % cls_obj.fullname) if DEBUG_ENABLE: if input("* Continue?(Y/N) ").upper() != "Y": _abort_by_user() return False if cls_obj.is_updated() or FORCE_UPDATE: print("\n> New build:", cls_obj.info_dic["LATEST_VERSION"]) write_log_info("%s has updates: %s" % (cls_obj.fullname, cls_obj.info_dic["LATEST_VERSION"])) try: cls_obj.after_check() except: traceback_string = traceback.format_exc() print("\n%s\n! Something wrong when running after_check!" % traceback_string) write_log_warning(*traceback_string.splitlines()) write_log_warning("%s: Something wrong when running after_check!" % cls_obj.fullname) cls_obj.write_to_database() if (ENABLE_SENDMESSAGE and not DONT_POST) or FORCE_UPDATE: send_message(cls_obj.get_print_text()) else: print(" no update") write_log_info("%s no update" % cls_obj.fullname) return True
def _abort(text): print(" - %s" % text) write_log_warning(str(text)) sys.exit(1)