From d7612cc9d12343f9bee53d5e093abe445d35b0b7 Mon Sep 17 00:00:00 2001 From: lunaticsm Date: Fri, 6 Sep 2024 23:01:52 +0000 Subject: [PATCH] add litterbox --- README.md | 98 +++++++++++++++++++++++++++++++++++++++----- setup.py | 2 +- src/catbox/catbox.py | 29 ++++++------- 3 files changed, 104 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index c94a232..9e77eff 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,18 @@ # Catbox Uploader -Catbox Uploader is a simple Python library to upload files and URLs to [Catbox.moe](https://catbox.moe). +Catbox Uploader is a simple Python library to upload files and URLs to [Catbox.moe](https://catbox.moe), including its temporary storage feature, **Litterbox**. ## Installation ```bash -pip install catbox_uploader +pip install catbox-uploader ``` ## Usage -### Upload a file: +### Upload a File to Catbox + +You can upload files to **Catbox.moe** using the `upload_file` method. The file will be stored permanently. ```python from catbox import CatboxUploader @@ -20,7 +22,29 @@ link = uploader.upload_file('path/to/your/image.png') print(f'Uploaded file link: {link}') ``` -### Upload a URL: +### Upload a File to Litterbox (Temporary Storage) + +Litterbox allows you to upload files for a temporary period, after which the files will be deleted automatically. Use the `upload_to_litterbox` method to upload files with a specified expiration time. + +**Available expiration times**: +- `1h`: 1 hour +- `12h`: 12 hours +- `24h`: 24 hours +- `72h`: 3 days + +Example of uploading a file to Litterbox for 24 hours: + +```python +from catbox import CatboxUploader + +uploader = CatboxUploader() +link = uploader.upload_to_litterbox('path/to/your/image.png', time='24h') +print(f'Uploaded file link (available for 24 hours): {link}') +``` + +### Upload a URL to Catbox + +If you want to upload a file via a URL to **Catbox**, you can use the `upload_url` method. ```python from catbox import CatboxUploader @@ -30,16 +54,70 @@ link = uploader.upload_url('https://example.com/image.png') print(f'Uploaded URL link: {link}') ``` -### Error Handling: +## Error Handling + +The library comes with built-in exception handling to manage common errors such as timeouts, connection issues, or HTTP errors. + +### Handling Timeout + +If the upload takes too long and exceeds the specified timeout, a `TimeoutError` will be raised. ```python -from catbox import CatboxUploader, CatboxError, FileNotFoundError +from catbox import CatboxUploader, TimeoutError uploader = CatboxUploader() try: - link = uploader.upload_file('path/to/your/file.png') -except FileNotFoundError as e: - print(f"File error: {str(e)}") + link = uploader.upload_file('path/to/your/image.png', timeout=10) + print(f'Uploaded file link: {link}') +except TimeoutError: + print("The upload took too long and timed out.") +``` + +### Handling Connection Issues + +If there's a problem connecting to the **Catbox.moe** or **Litterbox** server, a `ConnectionError` will be raised. + +```python +from catbox import CatboxUploader, ConnectionError + +uploader = CatboxUploader() +try: + link = uploader.upload_file('path/to/your/image.png') + print(f'Uploaded file link: {link}') +except ConnectionError: + print("Failed to connect to the server.") +``` + +### Handling HTTP Errors + +In case of HTTP errors (such as 404 or 500), an `HTTPError` will be raised. + +```python +from catbox import CatboxUploader, HTTPError + +uploader = CatboxUploader() +try: + link = uploader.upload_file('path/to/your/image.png') + print(f'Uploaded file link: {link}') +except HTTPError as he: + print(f"HTTP error occurred: {he}") +``` + +### Other Errors + +For other unexpected errors, a general `CatboxError` will be raised. + +```python +from catbox import CatboxUploader, CatboxError + +uploader = CatboxUploader() +try: + link = uploader.upload_file('path/to/your/image.png') + print(f'Uploaded file link: {link}') except CatboxError as e: - print(f"Catbox upload error: {str(e)}") + print(f"An error occurred: {e}") ``` + +## License + +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. diff --git a/setup.py b/setup.py index d356145..9a367d7 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='catbox_uploader', - version='0.3', + version='1.5', packages=find_packages(where="src"), package_dir={"": "src"}, install_requires=['requests'], diff --git a/src/catbox/catbox.py b/src/catbox/catbox.py index 025f4a4..793e866 100644 --- a/src/catbox/catbox.py +++ b/src/catbox/catbox.py @@ -4,6 +4,7 @@ class CatboxUploader: def __init__(self): self.api_url = 'https://catbox.moe/user/api.php' + self.litterbox_url = 'https://litterbox.catbox.moe/resources/internals/api.php' def upload_file(self, file_path, timeout=30): """ @@ -29,27 +30,27 @@ def upload_file(self, file_path, timeout=30): except requests.exceptions.RequestException as e: raise CatboxError(f"An error occurred: {str(e)}") - def upload_url(self, url, timeout=30): + def upload_to_litterbox(self, file_path, time='1h', timeout=30): """ - Upload a URL to Catbox and return the link. + Upload file to Litterbox (temporary storage) and return the link. - :param url: URL of the file to upload. + :param file_path: Path to the file to upload. + :param time: Duration for which the file will be available. Options: '1h', '12h', '24h', '72h'. :param timeout: Timeout in seconds for the upload request. - :return: URL of the uploaded file on Catbox. + :return: URL of the uploaded file on Litterbox. """ try: - data = { - 'reqtype': 'urlupload', - 'url': url - } - response = requests.post(self.api_url, data=data, timeout=timeout) - response.raise_for_status() - return response.text.strip() + with open(file_path, 'rb') as file: + files = {'fileToUpload': file} + data = {'reqtype': 'fileupload', 'time': time} + response = requests.post(self.litterbox_url, files=files, data=data, timeout=timeout) + response.raise_for_status() + return response.text.strip() except requests.exceptions.Timeout: - raise TimeoutError(f"Upload request timed out after {timeout} seconds.") + raise TimeoutError(f"Upload to Litterbox timed out after {timeout} seconds.") except requests.exceptions.ConnectionError: - raise ConnectionError("Failed to connect to Catbox. The server might be down.") + raise ConnectionError("Failed to connect to Litterbox. The server might be down.") except requests.exceptions.HTTPError as http_err: raise HTTPError(f"HTTP error occurred: {http_err}") except requests.exceptions.RequestException as e: - raise CatboxError(f"An error occurred: {str(e)}") + raise CatboxError(f"An error occurred: {str(e)}") \ No newline at end of file