Skip to content

Commit

Permalink
updater
Browse files Browse the repository at this point in the history
  • Loading branch information
noes14155 committed Aug 19, 2023
1 parent ea098eb commit 3ff027c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
4 changes: 3 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import os
import random

from updater import SelfUpdating
from io import BytesIO
from aiogram import Bot, Dispatcher, types
from aiogram.types import ChatType
Expand All @@ -15,6 +15,7 @@
from replit_detector import ReplitFlaskApp

service = bot_service.BotService()
updater = SelfUpdating('noes14155/Telegrambot-with-GPT4free')
storage = MemoryStorage()
bot = Bot(token=service.BOT_TOKEN)
owner_id = service.BOT_OWNER_ID
Expand Down Expand Up @@ -260,6 +261,7 @@ async def main():


if __name__ == "__main__":
updater.check_for_update()
replit_app = ReplitFlaskApp()
replit = replit_app.run()
if not replit:
Expand Down
1 change: 1 addition & 0 deletions temp
Submodule temp added at ea098e
70 changes: 70 additions & 0 deletions updater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import hashlib
import os
import shutil
import git
import requests
class SelfUpdating:

def __init__(self, repo_url, branch="master"):
self.repo_url = f'/~https://github.com/{repo_url}'
self.repo_name = repo_url
self.branch = branch
self.current_version = self.get_current_version()

def check_for_update(self):
latest_tag = self.get_latest_tag_from_github(self.repo_name)

if latest_tag != self.current_version:
print(f"New version {latest_tag} available! Updating...")
self.update()
else:
print(f"Already on latest version {self.current_version}")

def update(self):
temp_dir = "./temp/"
if not os.path.exists(temp_dir):
git.Git(".").clone(self.repo_url, temp_dir)

# Checkout latest commit
repo = git.Repo(temp_dir)
repo.git.checkout('master')
repo.git.pull()

# Walk through temp dir
changed_files = []
for root, dirs, files in os.walk(temp_dir):
if '.git' in dirs:
dirs.remove('.git')
for file in files:
file_path = os.path.join(root, file)
# Calculate hash of file
current_hash = hashlib.sha256(open(file_path, "rb").read()).hexdigest()
# Construct destination path
destination_path = os.path.join(os.getcwd(), file_path.split(temp_dir, 1)[1])
if os.path.exists(destination_path):
# Calculate hash of existing file
existing_hash = hashlib.sha256(open(destination_path, "rb").read()).hexdigest()
else:
existing_hash = ""
# Only overwrite if hashes don't match
if current_hash != existing_hash:
shutil.copyfile(file_path, destination_path)
changed_files.append(file)
# Delete temp dir
os.removedirs(temp_dir)
# Update version
self.current_version = self.get_current_version()

def get_current_version(self):
# Return current version somehow
return "0.3"

def get_latest_tag_from_github(self,repo_url):
api_url = f"https://api.github.com/repos/{repo_url}/releases/latest"
response = requests.get(api_url)
if response.ok:
release = response.json()
return release["name"]
else:
print(f"Error fetching latest release: {response}")
return "None"

0 comments on commit 3ff027c

Please sign in to comment.