CoolFace
Apppublic

Kfunga/mirror

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
update.py154 linesDownload Raw Back to root
1from dotenv import (2    load_dotenv,3    dotenv_values4)5from logging import (6    ERROR,7    INFO,8    basicConfig,9    error as log_error,10    FileHandler,11    getLogger,12    info as log_info,13    StreamHandler,14)15from os import (16    environ,17    path,18    remove19)20from pymongo.mongo_client import MongoClient21from pymongo.server_api import ServerApi22from subprocess import run as urun23from sys import exit24 25getLogger("pymongo").setLevel(ERROR)26 27if path.exists("Zee_Logs.txt"):28    with open(29        "Zee_Logs.txt",30        "r+"31    ) as f:32        f.truncate(0)33 34if path.exists("rlog.txt"):35    remove("rlog.txt")36 37basicConfig(38    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",39    handlers=[40        FileHandler("Zee_Logs.txt"),41        StreamHandler()42    ],43    level=INFO,44)45 46load_dotenv(47    "config.env",48    override=True49)50 51try:52    if bool(environ.get("_____REMOVE_THIS_LINE_____")):53        log_error("The README.md file there to be read! Exiting now!")54        exit(1)55except:56    pass57 58BOT_TOKEN = environ.get(59    "BOT_TOKEN",60    ""61)62if len(BOT_TOKEN) == 0:63    log_error("BOT_TOKEN variable is missing! Exiting now")64    exit(1)65 66BOT_ID = BOT_TOKEN.split(67    ":",68    169)[0]70 71DATABASE_URL = environ.get(72    "DATABASE_URL",73    ""74)75if len(DATABASE_URL) == 0:76    DATABASE_URL = None77 78if DATABASE_URL is not None:79    try:80        conn = MongoClient(81            DATABASE_URL,82            server_api=ServerApi("1")83        )84        db = conn.zee85        old_config = db.settings.deployConfig.find_one({"_id": BOT_ID})86        config_dict = db.settings.config.find_one({"_id": BOT_ID})87        if old_config is not None:88            del old_config["_id"]89        if (90            old_config is not None91            and old_config == dict(dotenv_values("config.env"))92            or old_config is None93        ) and config_dict is not None:94            environ["UPSTREAM_REPO"] = config_dict["UPSTREAM_REPO"]95            environ["UPSTREAM_BRANCH"] = config_dict["UPSTREAM_BRANCH"]96        conn.close()97    except Exception as e:98        log_error(f"Database ERROR: {e}")99 100UPSTREAM_REPO = environ.get(101    "UPSTREAM_REPO",102    ""103)104if len(UPSTREAM_REPO) == 0:105    UPSTREAM_REPO = "https://github.com/Dawn-India/Z-Mirror"106 107UPSTREAM_BRANCH = environ.get(108    "UPSTREAM_BRANCH",109    ""110)111if len(UPSTREAM_BRANCH) == 0:112    UPSTREAM_BRANCH = "main"113 114if UPSTREAM_REPO is not None:115    if path.exists(".git"):116        urun([117            "rm",118            "-rf",119            ".git"120        ])121 122    update = urun(123        [124            f"git init -q \125                     && git config --global user.email support@z-mirror.com \126                     && git config --global user.name zee \127                     && git add . \128                     && git commit -sm update -q \129                     && git remote add origin {UPSTREAM_REPO} \130                     && git fetch origin -q \131                     && git reset --hard origin/{UPSTREAM_BRANCH} -q"132        ],133        shell=True,134    )135 136    if update.returncode == 0:137        log_info("Successfully updated...")138        log_info("Thanks For Using @Z_Mirror")139    else:140        log_error("Error while getting latest updates.")141        log_error("Check if entered UPSTREAM_REPO is valid or not!")142 143urun(144    [145        "rm",146        "-rf",147        "py_generators",148        "config_sample.env",149        "Dockerfile",150        "LICENSE",151        "README.md",152        "requirements.txt"153    ]154)