Closed yusefgharib closed 2 weeks ago
Files to Check:
main.py: The main file of the program. promogames.py: The file containing logic for promo actions. utilities.py: The utility file with helper functions. Program Functionality:
Check for Updates:
The program compares the contents of local files (main.py, promogames.py, utilities.py) with the corresponding files on GitHub. If the contents of a local file differ from the contents of the file on GitHub, the file is marked as needing an update. Download Updates:
If updates are detected, the program downloads new versions of the files from GitHub and replaces the local files. Restart the Program:
After successfully downloading updates, the program restarts to apply the changes. Logging via Telegram:
The program sends messages to Telegram about the progress of operations (e.g., errors, successful updates, or the need for a restart).
import os
import sys
import requests
import time
# Constants
LOCAL_FILE_MAIN = "main.py"
GITHUB_RAW_URL_MAIN = "https://raw.githubusercontent.com/masterking32/MasterHamsterKombatBot/main/main.py"
LOCAL_FILE_PROMO = "promogames.py"
GITHUB_RAW_URL_PROMO = "https://raw.githubusercontent.com/masterking32/MasterHamsterKombatBot/main/promogames.py"
LOCAL_FILE_UTIL = "utilities.py"
GITHUB_RAW_URL_UTIL = "https://raw.githubusercontent.com/masterking32/MasterHamsterKombatBot/main/utilities.py"
RESTART_DELAY = 2 # Delay in seconds before restarting
# Telegram Bot API constants
BOT_TOKEN = 'YOUR_BOT_TOKEN_HERE'
CHAT_ID = 'YOUR_CHAT_ID_HERE'
TELEGRAM_API_URL = f'https://api.telegram.org/bot{BOT_TOKEN}/sendMessage'
def send_telegram_message(message):
try:
response = requests.post(TELEGRAM_API_URL, data={
'chat_id': CHAT_ID,
'text': message
})
if response.status_code != 200:
print(f"Error sending message to Telegram: {response.status_code} {response.text}")
except Exception as e:
print(f"Error sending message to Telegram: {e}")
def get_local_file_contents(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
contents = file.readlines()
except UnicodeDecodeError:
# If UTF-8 fails, try another encoding
with open(file_path, 'r', encoding='mbcs') as file:
contents = file.readlines()
# Exclude the update logic section
start_marker = "# BEGIN_UPDATE_LOGIC"
end_marker = "# END_UPDATE_LOGIC"
in_update_section = False
filtered_contents = []
for line in contents:
if start_marker in line:
in_update_section = True
if not in_update_section:
filtered_contents.append(line)
if end_marker in line:
in_update_section = False
return "".join(filtered_contents)
def get_github_file_contents(url):
try:
response = requests.get(url)
response.raise_for_status()
return response.text
except requests.RequestException as e:
send_telegram_message(f"Error fetching file from GitHub: {e}")
raise
def check_for_updates():
try:
updates_needed = []
# Check main.py
local_contents_main = get_local_file_contents(LOCAL_FILE_MAIN)
github_contents_main = get_github_file_contents(GITHUB_RAW_URL_MAIN)
if local_contents_main != github_contents_main:
updates_needed.append(LOCAL_FILE_MAIN)
# Check promogames.py
local_contents_promo = get_local_file_contents(LOCAL_FILE_PROMO)
github_contents_promo = get_github_file_contents(GITHUB_RAW_URL_PROMO)
if local_contents_promo != github_contents_promo:
updates_needed.append(LOCAL_FILE_PROMO)
# Check utilities.py
local_contents_util = get_local_file_contents(LOCAL_FILE_UTIL)
github_contents_util = get_github_file_contents(GITHUB_RAW_URL_UTIL)
if local_contents_util != github_contents_util:
updates_needed.append(LOCAL_FILE_UTIL)
return updates_needed
except Exception as e:
send_telegram_message(f"Error checking for updates: {e}")
return []
def download_update(file_name, url):
try:
github_contents = get_github_file_contents(url)
with open(file_name, 'w', encoding='utf-8') as file:
file.write(github_contents)
return True
except Exception as e:
send_telegram_message(f"Error downloading update for {file_name}: {e}")
return False
def restart_program():
try:
send_telegram_message("Restarting the program...")
time.sleep(RESTART_DELAY) # Optional delay before restarting
python = sys.executable
os.execl(python, python, *sys.argv)
except Exception as e:
send_telegram_message(f"Error restarting the program: {e}")
def update_check():
try:
updates_needed = check_for_updates()
if updates_needed:
send_telegram_message(f"New versions available for the following files: {updates_needed}")
all_updates_successful = True
for file_name in updates_needed:
url = {
LOCAL_FILE_MAIN: GITHUB_RAW_URL_MAIN,
LOCAL_FILE_PROMO: GITHUB_RAW_URL_PROMO,
LOCAL_FILE_UTIL: GITHUB_RAW_URL_UTIL
}[file_name]
if not download_update(file_name, url):
all_updates_successful = False
send_telegram_message(f"Failed to download update for {file_name}.")
if all_updates_successful:
send_telegram_message("All updates successfully downloaded. Restarting the program...")
restart_program()
else:
send_telegram_message("Some updates failed. Please check the logs.")
else:
send_telegram_message("No updates found.")
except Exception as e:
send_telegram_message(f"Error checking updates: {e}")
# Start the update check
update_check()
if you havnt installed via git cli you are kind of stuck for automatic updates then though.
if you havnt installed via git cli you are kind of stuck for automatic updates then though.
It's not hard to switch to Git.
Just create a copy of your config file, install Git, and clone the project with
git clone https://github.com/masterking32/MasterHamsterKombatBot
then copy your config file into the MasterHamsterKombatBot folder and run the project.
for update just run git pull origin main
if you havnt installed via git cli you are kind of stuck for automatic updates then though.
It's not hard to switch to Git. Just create a copy of your config file, install Git, and clone the project with
git clone https://github.com/masterking32/MasterHamsterKombatBot
then copy your config file into the MasterHamsterKombatBot folder and run the project.for update just run
git pull origin main
I have 5+ copies of the program running at the same time from separate folders so I dont think that would work for me.
if you havnt installed via git cli you are kind of stuck for automatic updates then though.
It's not hard to switch to Git. Just create a copy of your config file, install Git, and clone the project with
git clone https://github.com/masterking32/MasterHamsterKombatBot
then copy your config file into the MasterHamsterKombatBot folder and run the project. for update just rungit pull origin main
I have 5+ copies of the program running at the same time from separate folders so I dont think that would work for me.
Create a bash/bat file for that.
cd MasterHamsterKombatBot1
git pull origin main
cd ../MasterHamsterKombatBot2
git pull origin main
cd ../MasterHamsterKombatBot3
git pull origin main
Instead of checking everyday if there is an update and manually overwriting the code, this will allow for automatic updates