Skip to content

Commit

Permalink
v1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Vombit committed Apr 9, 2024
1 parent 57194c6 commit eac6821
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 29 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
/env
output
TEMP
123.json
db.sqlite3
todo
trash
test.ipynb
.vscode/settings.json
db1.sqlite3
db*.sqlite3
dev.json
main.json
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@
> 3) The ability to share files
> 4) resume the download
> 5) check the availability of chunks
> 6) percentage of document load
> 6) percentage of document load
> 7) pause mode
> 8) ~~checking if the chunk is downloaded, do not download it again~~
2 changes: 1 addition & 1 deletion bin/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</svg>
</div>
<div class="version noselect">
v 1.0.1
v 1.0.2
</div>
<div class="line_block">
<div onclick="window.PyHandler.open_donation()" class="bymecoffee" title="By me coffee">
Expand Down
63 changes: 53 additions & 10 deletions bin/modules/additional_functions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import sys
import threading
import time
from bin.modules.file_manager import FileManager
from bin.modules.db_manager import DBManager
from numpy import array_split
Expand All @@ -21,27 +20,37 @@ def bot_upload(file_hash: str, chunks: list, bot: object):
try:
lock.acquire(True)
db.add_chunk(file_hash, chunk_file_hash, chunk.split("_")[1], chunk_file_id)
except Exception:
print(Exception)
finally:
lock.release()

# while True:
# try:
# os.remove(chunk_path)
# break
# except Exception:
# time.sleep(0.3)
print(f'chunks uploaded for bot:{bot.bot_token}')
for chunk in chunks:
chunk_path = f"{fm.split_chunks}/{chunk}"
try:
os.remove(chunk_path)
except Exception:
pass
print(Exception)
print(f'chunks deleted for bot:{bot.bot_token}')


def bot_download(chunks: list, bot: object):
def bot_download(chunks: list, bot: object, bots):
for chunk in chunks:
path_chunk = f"{fm.loaded_chunks}/{chunk[1]}_{chunk[3]}"

if os.path.isfile(path_chunk):
hash = fm.get_file_hash(path_chunk)
if chunk[1] == hash:
continue

loaded_file = bot.download_document(chunk[4])
if not loaded_file:
for bot in bots:
loaded_file = bot.download_document(chunk[4])
if loaded_file:
break

# print(loaded_file)
# Save the chunk to a file and remove it when done
with open(path_chunk, "wb") as new_file:
new_file.write(loaded_file)
Expand Down Expand Up @@ -72,3 +81,37 @@ def check_healh_bot(bot: object) -> bool:
if bot.send_document(test_file):
return True
return False


class Manager:
def __init__(self):
pass

def download(args):
pass

def continue_download(args):
pass

def upload(file_hash: str, chunks: list, bots: object):
for chunk in chunks:
chunk_path = f"{fm.split_chunks}/{chunk}"

chunk_file_id = bots.send_document(chunk_path)
chunk_file_hash = fm.get_file_hash(chunk_path)

try:
lock.acquire(True)
db.add_chunk(file_hash, chunk_file_hash, chunk.split("_")[1], chunk_file_id)
finally:
lock.release()

for chunk in chunks:
chunk_path = f"{fm.split_chunks}/{chunk}"
try:
os.remove(chunk_path)
except Exception:
print(Exception)

def continue_upload(args):
pass
8 changes: 8 additions & 0 deletions bin/modules/db_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ def get_file_by_name(self, name: str) -> list:

return self.cursor.fetchall()

def get_chunk(self, file_hash: str) -> str:
"""
Get chunk info
"""
self.cursor.execute(f"SELECT * FROM chunks WHERE hash = '{file_hash}'")

return self.cursor.fetchall()

def get_chunks(self, main_file_hash: str) -> list:
"""
Get all chunks from the 'chunks'
Expand Down
7 changes: 5 additions & 2 deletions bin/modules/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ def get_file_hash(self, file_path: str) -> str:

return hash_md5.hexdigest()

def merge_chunks(self, filename: str) -> None:
def merge_chunks(self, filename: str, filehash: str) -> None:
"""
Merge all loaded chunks with the given
filename and save to output path.
Args:
filename (str): The name of the file to merge.
"""
chunks = [file for file in os.listdir(self.loaded_chunks)]
chunks = []
for file in os.listdir(self.loaded_chunks):
if filehash in file:
chunks.append(file)
chunks = sorted(chunks, key=lambda x: int(x.split("_")[1]))

try:
Expand Down
24 changes: 14 additions & 10 deletions bin/modules/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class TelegramBot:
"""
test
class for telegram bot
"""

def __init__(self, bot_token: str = None, chat_id: str = None) -> None:
Expand Down Expand Up @@ -56,17 +56,22 @@ def send_document(self, file_path: str) -> str:
data = {
"chat_id": self.chat_id,
}
# data = {"chat_id": str(self.chat_id), "document": (open(file_path, "rb"))}
response = requests.post(url, files=files, data=data)
for _ in range(10):
if response.status_code == 200:
result = response.json()
if result["ok"]:
file_id = result["result"]["document"]["file_id"]

return file_id
try:
if response.status_code == 200:
result = response.json()
if result["ok"]:
file_id = result["result"]["document"]["file_id"]

return file_id
except Exception:
print(Exception)

response = requests.post(url, files=files, data=data)
else:
return {}
return ""

def download_document(self, file_id: str) -> str:
"""
Expand All @@ -90,8 +95,7 @@ def download_document(self, file_id: str) -> str:
f"https://api.telegram.org/file/bot{self.bot_token}/{file_url}"
)
file = requests.get(url_file)

return file.content
response = requests.get(url)
else:
return {}
return ""
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def _async_downloader(self, file_name: str):
threads = []
for i, chunks in enumerate(split_chinks):
my_thread = threading.Thread(
target=bot_download, args=(chunks, self.t_bots[i])
target=bot_download, args=(chunks, self.t_bots[i], self.t_bots)
)
threads.append(my_thread)
my_thread.start()
Expand All @@ -163,7 +163,7 @@ def _async_downloader(self, file_name: str):
# Merge all chunks and update the progress bar in JavaScript
view.page().runJavaScript("openPopup('merging files...')")
view.page().runJavaScript("changeProgress(60)")
fm.merge_chunks(file_name)
fm.merge_chunks(file_name, file_hash)
view.page().runJavaScript("openPopup('uploaded!')")
view.page().runJavaScript("changeProgress(100)")
time.sleep(2)
Expand Down

0 comments on commit eac6821

Please sign in to comment.