Skip to content

Commit

Permalink
🍻Fixed hashes updating task
Browse files Browse the repository at this point in the history
  • Loading branch information
bth123 committed Dec 23, 2024
1 parent 5d9c370 commit 0b6c369
Showing 1 changed file with 2 additions and 18 deletions.
20 changes: 2 additions & 18 deletions cogs/minecraft/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ def __init__(self, bot):
self.update_files_list.start()

async def get_files_list(self, branches=("data", "assets")):
global logs_channel
await logs_channel.send("getting files...")
async with ClientSession(headers=GITHUB_HEADERS) as session:
current_files = {}
await logs_channel.send("Client session started")
for branch in branches:
async with session.get(f"https://api.github.com/repos/misode/mcmeta/git/trees/{branch}?recursive=1") as response:
if response.status == 200:
Expand All @@ -41,37 +38,27 @@ async def get_files_list(self, branches=("data", "assets")):
for item in tree if item["type"] == "blob"
})
else:
await logs_channel.send(f"Resoponse error idk {response.status} {response}")
raise Exception(f"Response error")
await logs_channel.send("`for branch in bracnhes` finished")
try:
current_files.pop(".gitattributes")
except:pass
await logs_channel.send("returning the files")
return current_files

@tasks.loop(minutes=6)
async def update_files_list(self):
global latest_version, files, logs_channel
logs_channel = await self.bot.fetch_channel(LOGS_CHANNEL_ID)
await logs_channel.send(f'latest_known_snapshot: `{db.find_one({"_id": "latest_known_snapshot"})["_"]}`, latest_version: `{latest_version}`')
global latest_version, files
if (newer_version:=db.find_one({"_id": "latest_known_snapshot"})["_"]) != latest_version:
await logs_channel.send(f"`newer_version`({newer_version}) != `latest_version`({latest_version})")
if (latest_files := versions_pathes.find_one({"_id": newer_version.replace('.', '_')})):
files = latest_files["_"]
await logs_channel.send("files already exist")
else:
await logs_channel.send("files are not exist rn")
try:
files = await self.get_files_list()
await self.update_versions_hashes(newer_version)
latest_version = newer_version
await logs_channel.send("all the stuff finished yeaa")
versions_pathes.insert_one({"_id": latest_version.replace('.', '_'), "_": files})
except:pass

async def update_versions_hashes(self, newer_version=None):
await logs_channel.send("started updating versions hashes")
versions_hashes = db.find_one({"_id": "versions_hashes"})
if versions_hashes:
versions_hashes = versions_hashes["_"]
Expand All @@ -82,7 +69,6 @@ async def update_versions_hashes(self, newer_version=None):
while True:
url = f"https://api.github.com/repos/misode/mcmeta/commits?sha={branch}&per_page=100&page={page}"
async with ClientSession(headers=GITHUB_HEADERS) as session:
await logs_channel.send(f"client session started, curr page is `{page}`")
async with session.get(url) as response:
if response.status == 200:
data = await response.json()
Expand All @@ -92,12 +78,10 @@ async def update_versions_hashes(self, newer_version=None):
version = commit["commit"]["message"].split(" ")[-1].replace(".", "_")
versions_hashes[branch][version] = commit["sha"]
else:
await logs_channel.send(f"Failed to fetch commits. Status code: {response.status}")
print(f"Failed to fetch commits. Status code: {response.status}")
break
page += 1
if newer_version and not (newer_version in versions_hashes["data"]):
await logs_channel.send("Mcmeta not updated")
if newer_version and not (newer_version.replace(".", "_") in versions_hashes["data"]):
raise Exception("Mcmeta not updated")
db.update_one({"_id": "versions_hashes"}, {"$set": {"_": versions_hashes}}, upsert=True)

Expand Down

0 comments on commit 0b6c369

Please sign in to comment.