From 7c6824726303d76c037ace8b8a4295f1c0f3562e Mon Sep 17 00:00:00 2001 From: I'm Not A Bot #Left_TG <77770753+Itz-fork@users.noreply.github.com> Date: Mon, 20 Sep 2021 15:27:31 +0530 Subject: [PATCH] v1.2 - Async version (unstable) --- README.md | 6 ++ gofile2/async_gofile2.py | 177 ++++++++++++++++++++++----------------- gofile2/gofile2.py | 120 +++++++++++++++----------- requirements.txt | 3 +- 4 files changed, 176 insertions(+), 130 deletions(-) diff --git a/README.md b/README.md index c6df923..9caf1b1 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,15 @@ pip install git+https://github.com/Itz-fork/Gofile2.git # Usage **1. Import [Gofile2](https://github.com/Itz-fork/Gofile2) in your python file** + +**Synchronous version** ```python from gofile2 import Gofile ``` +**Asynchronous version** (Sometimes unstable) +```python +from gofile2 import Async_Gofile +``` **2. Create an instance of Gofile2** ```python diff --git a/gofile2/async_gofile2.py b/gofile2/async_gofile2.py index 16304e4..c19afda 100644 --- a/gofile2/async_gofile2.py +++ b/gofile2/async_gofile2.py @@ -2,7 +2,16 @@ # Re-built by Itz-fork # Project: Gofile2 import aiohttp -from syncer import sync +import requests + +# Function to check if token is valid or not (using request lib as this is just a sync function) +def is_valid_token(url, token): + get_account_resp = requests.get(url=f"{url}getAccountDetails?token={token}&allDetails=true").json() + if get_account_resp["status"] == "error-wrongToken": + raise Exception("WRONG TOKEN") + else: + pass + class Async_Gofile: """ @@ -18,13 +27,10 @@ def __init__(self, token=None): self.r_session = aiohttp.ClientSession() self.token = token if self.token is not None: - c_acc = sync(self.get_Account()) - is_ok = c_acc(check_account=True) - if is_ok is False: - raise Exception("WRONG TOKEN") - else: - pass - + is_valid_token(url=self.api_url, token=self.token) + + async def __close_session(self, session): + await session.close() async def _api_resp_handler(self, response): api_status = response["status"] @@ -46,9 +52,14 @@ async def get_Server(self): None """ async with self.r_session as session: - server_resp = await session.get(f"{self.api_url}getServer") - server_resp = await server_resp.json() - return await self._api_resp_handler(server_resp) + try: + server_resp = await session.get(f"{self.api_url}getServer") + server_resp = await server_resp.json() + await self.__close_session(session) + return await self._api_resp_handler(server_resp) + except Exception as e: + await self.__close_session(session) + raise Exception(f"Cannot Continue due to: {e}") async def get_Account(self, check_account=False): """ @@ -58,21 +69,23 @@ async def get_Account(self, check_account=False): Arguments: check_account (optional) - Boolean. Pass True to check if account exists or not. else it'll return all data of account """ - token = self.token - if token is None: - raise Exception("TOKEN IS NOT DEFINED") async with self.r_session as session: - get_account_resp = await session.get(url=f"{self.api_url}getAccountDetails?token={token}&allDetails=true") - get_account_resp = await get_account_resp.json() - if check_account is True: - if get_account_resp["status"] == "ok": - return True - elif get_account_resp["status"] == "error-wrongToken": - return False + try: + get_account_resp = await session.get(url=f"{self.api_url}getAccountDetails?token={self.token}&allDetails=true") + get_account_resp = await get_account_resp.json() + await self.__close_session(session) + if check_account is True: + if get_account_resp["status"] == "ok": + return True + elif get_account_resp["status"] == "error-wrongToken": + return False + else: + return await self._api_resp_handler(get_account_resp) else: return await self._api_resp_handler(get_account_resp) - else: - return await self._api_resp_handler(get_account_resp) + except Exception as e: + await self.__close_session(session) + raise Exception(f"Cannot Continue due to: {e}") async def upload(self,file: str, folderId: str = None, description: str = None, password: str = None, tags: str = None, expire: int = None): """ @@ -87,26 +100,30 @@ async def upload(self,file: str, folderId: str = None, description: str = None, tags (optional) - Tags for the folder. If multiple tags, seperate them with comma. Not applicable if you specify a folderId expire (optional) - Expiration date of the folder. Must be in the form of unix timestamp. Not applicable if you specify a folderId """ - token = self.token server = self.get_Server()["server"] if password != None and len(password) < 4: raise Exception("passwordLength") async with self.r_session as session: - upload_file = await session.post( - url=f"https://{server}.gofile.io/uploadFile", - data={ - "token": token, - "folderId": folderId, - "description": description, - "password": password, - "tags": tags, - "expire": expire - }, - files={"upload_file": open(file, "rb")} - ) - upload_file = await upload_file.json() - return await self._api_resp_handler(upload_file) + try: + upload_file = await session.post( + url=f"https://{server}.gofile.io/uploadFile", + data={ + "token": self.token, + "folderId": folderId, + "description": description, + "password": password, + "tags": tags, + "expire": expire + }, + files={"upload_file": open(file, "rb")} + ) + upload_file = await upload_file.json() + await self.__close_session(session) + return await self._api_resp_handler(upload_file) + except Exception as e: + await self.__close_session(session) + raise Exception(f"Cannot Continue due to: {e}") async def create_folder(self, parentFolderId, folderName): """ @@ -117,20 +134,22 @@ async def create_folder(self, parentFolderId, folderName): parentFolderId - The parent folder ID folderName - The name of the folder that wanted to create """ - token = self.token - if token is None: - raise Exception("TOKEN IS NOT DEFINED") async with self.r_session as session: - folder_resp = await session.put( - url=f"{self.api_url}createFolder", - data={ - "parentFolderId": parentFolderId, - "folderName": folderName, - "token": token - } - ) - folder_resp = await folder_resp.json() - return await self._api_resp_handler(folder_resp) + try: + folder_resp = await session.put( + url=f"{self.api_url}createFolder", + data={ + "parentFolderId": parentFolderId, + "folderName": folderName, + "token": self.token + } + ) + folder_resp = await folder_resp.json() + await self.__close_session(session) + return await self._api_resp_handler(folder_resp) + except Exception as e: + await self.__close_session(session) + raise Exception(f"Cannot Continue due to: {e}") async def set_folder_options(self, folderId, option, value): """ @@ -147,21 +166,23 @@ async def set_folder_options(self, folderId, option, value): For "expire", must be the expiration date in the form of unix timestamp. For "tags", must be a comma seperated list of tags. """ - token = self.token - if token is None: - raise Exception("TOKEN IS NOT DEFINED") async with self.r_session as session: - set_folder_resp = await session.put( - url=f"{self.api_url}setFolderOptions", - data={ - "token": token, - "folderId": folderId, - "option": option, - "value": value - } - ) - set_folder_resp = await set_folder_resp.json() - return await self._api_resp_handler(set_folder_resp) + try: + set_folder_resp = await session.put( + url=f"{self.api_url}setFolderOptions", + data={ + "token": self.token, + "folderId": folderId, + "option": option, + "value": value + } + ) + set_folder_resp = await set_folder_resp.json() + await self.__close_session(session) + return await self._api_resp_handler(set_folder_resp) + except Exception as e: + await self.__close_session(session) + raise Exception(f"Cannot Continue due to: {e}") async def delete_content(self, contentId): """ @@ -171,16 +192,18 @@ async def delete_content(self, contentId): Arguments: contentId - The ID of the file or folder """ - token = self.token - if token is None: - raise Exception("TOKEN IS NOT DEFINED") async with self.r_session as session: - del_content_resp = await session.delete( - url=f"{self.api_url}deleteContent", - data={ - "contentId": contentId, - "token": token - } - ) - del_content_resp = await del_content_resp.json() - return await self._api_resp_handler(del_content_resp) \ No newline at end of file + try: + del_content_resp = await session.delete( + url=f"{self.api_url}deleteContent", + data={ + "contentId": contentId, + "token": self.token + } + ) + del_content_resp = await del_content_resp.json() + await self.__close_session(session) + return await self._api_resp_handler(del_content_resp) + except Exception as e: + await self.__close_session(session) + raise Exception(f"Cannot Continue due to: {e}") \ No newline at end of file diff --git a/gofile2/gofile2.py b/gofile2/gofile2.py index 1c8bf4f..e49868f 100644 --- a/gofile2/gofile2.py +++ b/gofile2/gofile2.py @@ -41,8 +41,11 @@ def get_Server(self): Arguments: None """ - server_resp = requests.get(f"{self.api_url}getServer").json() - return self._api_resp_handler(server_resp) + try: + server_resp = requests.get(f"{self.api_url}getServer").json() + return self._api_resp_handler(server_resp) + except Exception as e: + raise Exception(f"Cannot Continue due to: {e}") def get_Account(self, check_account=False): """ @@ -55,16 +58,19 @@ def get_Account(self, check_account=False): token = self.token if token is None: raise Exception("TOKEN IS NOT DEFINED") - get_account_resp = requests.get(url=f"{self.api_url}getAccountDetails?token={token}&allDetails=true").json() - if check_account is True: - if get_account_resp["status"] == "ok": - return True - elif get_account_resp["status"] == "error-wrongToken": - return False + try: + get_account_resp = requests.get(url=f"{self.api_url}getAccountDetails?token={token}&allDetails=true").json() + if check_account is True: + if get_account_resp["status"] == "ok": + return True + elif get_account_resp["status"] == "error-wrongToken": + return False + else: + return self._api_resp_handler(get_account_resp) else: return self._api_resp_handler(get_account_resp) - else: - return self._api_resp_handler(get_account_resp) + except Exception as e: + raise Exception(f"Cannot Continue due to: {e}") def upload(self,file: str, folderId: str = None, description: str = None, password: str = None, tags: str = None, expire: int = None): """ @@ -80,23 +86,26 @@ def upload(self,file: str, folderId: str = None, description: str = None, passwo expire (optional) - Expiration date of the folder. Must be in the form of unix timestamp. Not applicable if you specify a folderId """ token = self.token - server = self.get_Server()["server"] if password != None and len(password) < 4: raise Exception("passwordLength") - upload_file = requests.post( - url=f"https://{server}.gofile.io/uploadFile", - data={ - "token": token, - "folderId": folderId, - "description": description, - "password": password, - "tags": tags, - "expire": expire - }, - files={"upload_file": open(file, "rb")} - ).json() - return self._api_resp_handler(upload_file) + try: + server = self.get_Server()["server"] + upload_file = requests.post( + url=f"https://{server}.gofile.io/uploadFile", + data={ + "token": token, + "folderId": folderId, + "description": description, + "password": password, + "tags": tags, + "expire": expire + }, + files={"upload_file": open(file, "rb")} + ).json() + return self._api_resp_handler(upload_file) + except Exception as e: + raise Exception(f"Cannot Continue due to: {e}") def create_folder(self, parentFolderId, folderName): """ @@ -110,15 +119,18 @@ def create_folder(self, parentFolderId, folderName): token = self.token if token is None: raise Exception("TOKEN IS NOT DEFINED") - folder_resp = requests.put( - url=f"{self.api_url}createFolder", - data={ - "parentFolderId": parentFolderId, - "folderName": folderName, - "token": token - } - ).json() - return self._api_resp_handler(folder_resp) + try: + folder_resp = requests.put( + url=f"{self.api_url}createFolder", + data={ + "parentFolderId": parentFolderId, + "folderName": folderName, + "token": token + } + ).json() + return self._api_resp_handler(folder_resp) + except Exception as e: + raise Exception(f"Cannot Continue due to: {e}") def set_folder_options(self, folderId, option, value): """ @@ -138,16 +150,19 @@ def set_folder_options(self, folderId, option, value): token = self.token if token is None: raise Exception("TOKEN IS NOT DEFINED") - set_folder_resp = requests.put( - url=f"{self.api_url}setFolderOptions", - data={ - "token": token, - "folderId": folderId, - "option": option, - "value": value - } - ).json() - return self._api_resp_handler(set_folder_resp) + try: + set_folder_resp = requests.put( + url=f"{self.api_url}setFolderOptions", + data={ + "token": token, + "folderId": folderId, + "option": option, + "value": value + } + ).json() + return self._api_resp_handler(set_folder_resp) + except Exception as e: + raise Exception(f"Cannot Continue due to: {e}") def delete_content(self, contentId): """ @@ -160,11 +175,14 @@ def delete_content(self, contentId): token = self.token if token is None: raise Exception("TOKEN IS NOT DEFINED") - del_content_resp = requests.delete( - url=f"{self.api_url}deleteContent", - data={ - "contentId": contentId, - "token": token - } - ).json() - return self._api_resp_handler(del_content_resp) + try: + del_content_resp = requests.delete( + url=f"{self.api_url}deleteContent", + data={ + "contentId": contentId, + "token": token + } + ).json() + return self._api_resp_handler(del_content_resp) + except Exception as e: + raise Exception(f"Cannot Continue due to: {e}") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 7ff6b3f..a0402cc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ requests -aiohttp -syncer \ No newline at end of file +aiohttp \ No newline at end of file