From 21c3eb7796150ac1d18f95de0506911c8e279462 Mon Sep 17 00:00:00 2001 From: lunaticsm Date: Sat, 7 Sep 2024 10:14:42 +0000 Subject: [PATCH] userhash --- README.md | 95 ++++++++++++----------- setup.py | 2 +- src/catbox/catbox.py | 123 ++++++++++------------------- src/catbox/helpers.py | 175 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 263 insertions(+), 132 deletions(-) create mode 100644 src/catbox/helpers.py diff --git a/README.md b/README.md index 7173bbd..b83f6a9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Catbox Uploader -Catbox Uploader is a simple Python library to upload files and URLs to [Catbox.moe](https://catbox.moe), including its temporary storage feature, **Litterbox**. +Catbox Uploader is a simple Python library to upload files and URLs to [Catbox.moe](https://catbox.moe), including its temporary storage feature, **Litterbox**, and album management. ## Installation @@ -10,16 +10,23 @@ pip install catbox-uploader ## Usage -### Upload a File to Catbox +### Using Userhash -You can upload files to **Catbox.moe** using the `upload_file` method. The file will be stored permanently. +CatboxUploader supports using a **userhash** directly. You can pass the `userhash` when initializing the `CatboxUploader` and use it for authenticated uploads and album management. + +### Initialize with Userhash ```python from catbox import CatboxUploader -uploader = CatboxUploader() -link = uploader.upload_file('path/to/your/image.png') -print(f'Uploaded file link: {link}') +uploader = CatboxUploader(userhash='your_userhash_here') +``` + +### Upload a File + +```python +link = uploader.upload_file('path/to/your/file.jpg') +print(f'Uploaded file: {link}') ``` ### Upload a File to Litterbox (Temporary Storage) @@ -32,43 +39,50 @@ Litterbox allows you to upload files for a temporary period, after which the fil - `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}') +link = uploader.upload_to_litterbox('path/to/your/file.jpg', time='24h') +print(f'Uploaded file (available for 24 hours): {link}') ``` -### Upload Multiple Files as an Album to Catbox +### Upload Multiple Files as an Album You can upload multiple files as an album to **Catbox.moe** using the `upload_album` method. This allows you to upload several files at once, and it will return the links for all uploaded files. ```python -from catbox import CatboxUploader - -uploader = CatboxUploader() file_paths = ['file1.jpg', 'file2.jpg', 'file3.jpg'] links = uploader.upload_album(file_paths) for link in links: print(f'Uploaded file link: {link}') ``` -### Upload a URL to Catbox +### Create and Manage Albums -If you want to upload a file via a URL to **Catbox**, you can use the `upload_url` method. +#### Create an Album + +You can create an album with uploaded files, a title, and a description using the `create_album` method: ```python -from catbox import CatboxUploader +album_shortcode = uploader.create_album(file_links, "My Album", "This is a test album") +print(f"Album created: https://catbox.moe/c/{album_shortcode}") +``` + +#### Edit an Album + +You can edit an album by changing its title, description, or the files it contains: + +```python +uploader.edit_album(album_shortcode, file_links, "Updated Album Title", "Updated description") +``` + +#### Delete an Album + +You can delete an album by its shortcode: -uploader = CatboxUploader() -link = uploader.upload_url('https://example.com/image.png') -print(f'Uploaded URL link: {link}') +```python +uploader.delete_album(album_shortcode) ``` -## Error Handling +### Error Handling The library comes with built-in exception handling to manage common errors such as timeouts, connection issues, or HTTP errors. @@ -79,10 +93,10 @@ If the upload takes too long and exceeds the specified timeout, a `TimeoutError` ```python from catbox import CatboxUploader, TimeoutError -uploader = CatboxUploader() +uploader = CatboxUploader(userhash='your_userhash_here') try: - link = uploader.upload_file('path/to/your/image.png', timeout=10) - print(f'Uploaded file link: {link}') + link = uploader.upload_file('path/to/your/file.jpg', timeout=10) + print(f'Uploaded file: {link}') except TimeoutError: print("The upload took too long and timed out.") ``` @@ -94,10 +108,10 @@ If there's a problem connecting to the **Catbox.moe** or **Litterbox** server, a ```python from catbox import CatboxUploader, ConnectionError -uploader = CatboxUploader() +uploader = CatboxUploader(userhash='your_userhash_here') try: - link = uploader.upload_file('path/to/your/image.png') - print(f'Uploaded file link: {link}') + link = uploader.upload_file('path/to/your/file.jpg') + print(f'Uploaded file: {link}') except ConnectionError: print("Failed to connect to the server.") ``` @@ -109,29 +123,14 @@ In case of HTTP errors (such as 404 or 500), an `HTTPError` will be raised. ```python from catbox import CatboxUploader, HTTPError -uploader = CatboxUploader() +uploader = CatboxUploader(userhash='your_userhash_here') try: - link = uploader.upload_file('path/to/your/image.png') - print(f'Uploaded file link: {link}') + link = uploader.upload_file('path/to/your/file.jpg') + print(f'Uploaded file: {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"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 e61fb81..cd14449 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='catbox_uploader', - version='1.7', + version='2.0', packages=find_packages(where="src"), package_dir={"": "src"}, install_requires=['requests'], diff --git a/src/catbox/catbox.py b/src/catbox/catbox.py index b817762..de32bf4 100644 --- a/src/catbox/catbox.py +++ b/src/catbox/catbox.py @@ -1,83 +1,40 @@ -import requests -from .exceptions import CatboxError, FileNotFoundError, TimeoutError, ConnectionError, HTTPError - -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): - """ - Upload file to Catbox and return the link. - - :param file_path: Path to the file to upload. - :param timeout: Timeout in seconds for the upload request. - :return: URL of the uploaded file on Catbox. - """ - try: - with open(file_path, 'rb') as file: - files = {'fileToUpload': file} - data = {'reqtype': 'fileupload'} - response = requests.post(self.api_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.") - except requests.exceptions.ConnectionError: - raise ConnectionError("Failed to connect to Catbox. 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)}") - - def upload_to_litterbox(self, file_path, time='1h', timeout=30): - """ - Upload file to Litterbox (temporary storage) and return the link. - - :param file_path: Path to the file to upload. - :param time: Duration for which the file will be available. Options: '1h', '12h', '24h', '72h', '1w'. - :param timeout: Timeout in seconds for the upload request. - :return: URL of the uploaded file on Litterbox. - """ - try: - 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 to Litterbox timed out after {timeout} seconds.") - except requests.exceptions.ConnectionError: - 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)}") - - def upload_album(self, file_paths, timeout=30): - """ - Upload multiple files as an album to Catbox and return their links. - - :param file_paths: List of paths to the files to upload. - :param timeout: Timeout in seconds for the upload request. - :return: List of URLs of the uploaded files on Catbox. - """ - uploaded_links = [] - try: - for file_path in file_paths: - with open(file_path, 'rb') as file: - files = {'fileToUpload': file} - data = {'reqtype': 'fileupload'} - response = requests.post(self.api_url, files=files, data=data, timeout=timeout) - response.raise_for_status() - uploaded_links.append(response.text.strip()) - return uploaded_links - except requests.exceptions.Timeout: - raise TimeoutError(f"Album upload timed out after {timeout} seconds.") - except requests.exceptions.ConnectionError: - raise ConnectionError("Failed to connect to Catbox. 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)}") \ No newline at end of file +from .helpers import upload_file, upload_to_litterbox, upload_album, delete_files, create_album, edit_album, delete_album +from .exceptions import CatboxError + +class CatboxUploader: + def __init__(self, userhash=None): + """ + Initialize CatboxUploader with optional userhash (similar to API key). + + :param userhash: A string containing the userhash for authenticated uploads and album management. + """ + self.userhash = userhash + + def upload_file(self, file_path, timeout=30): + return upload_file(file_path, timeout, self.userhash) + + def upload_to_litterbox(self, file_path, time='1h', timeout=30): + return upload_to_litterbox(file_path, time, timeout) + + def upload_album(self, file_paths, timeout=30): + return upload_album(file_paths, timeout, self.userhash) + + def delete_files(self, files): + if not self.userhash: + raise CatboxError("Userhash is required to delete files.") + return delete_files(files, self.userhash) + + def create_album(self, files, title, description): + if not self.userhash: + raise CatboxError("Userhash is required to create an album.") + return create_album(files, title, description, self.userhash) + + def edit_album(self, shortcode, files, title, description): + if not self.userhash: + raise CatboxError("Userhash is required to edit an album.") + return edit_album(shortcode, files, title, description, self.userhash) + + def delete_album(self, shortcode): + if not self.userhash: + raise CatboxError("Userhash is required to delete an album.") + return delete_album(shortcode, self.userhash) \ No newline at end of file diff --git a/src/catbox/helpers.py b/src/catbox/helpers.py new file mode 100644 index 0000000..ec4ca1a --- /dev/null +++ b/src/catbox/helpers.py @@ -0,0 +1,175 @@ +import requests +from .exceptions import CatboxError, TimeoutError, ConnectionError, HTTPError + +def upload_file(file_path, timeout=30, userhash=None): + """ + Upload file to Catbox. If userhash is provided, the upload will be authenticated. + + :param file_path: Path to the file to upload. + :param timeout: Timeout in seconds for the upload request. + :param userhash: Optional userhash for authenticated upload. + :return: URL of the uploaded file on Catbox. + """ + try: + with open(file_path, 'rb') as file: + files = {'fileToUpload': file} + data = {'reqtype': 'fileupload'} + + if userhash: + data['userhash'] = userhash + + response = requests.post("https://catbox.moe/user/api.php", 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.") + except requests.exceptions.ConnectionError: + raise ConnectionError("Failed to connect to Catbox. 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)}") + +def upload_to_litterbox(file_path, time='1h', timeout=30): + """ + Upload file to Litterbox (temporary storage). + + :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 Litterbox. + """ + try: + with open(file_path, 'rb') as file: + files = {'fileToUpload': file} + data = {'reqtype': 'fileupload', 'time': time} + response = requests.post("https://litterbox.catbox.moe/resources/internals/api.php", files=files, data=data, timeout=timeout) + response.raise_for_status() + return response.text.strip() + except requests.exceptions.Timeout: + raise TimeoutError(f"Upload to Litterbox timed out after {timeout} seconds.") + except requests.exceptions.ConnectionError: + 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)}") + +def upload_album(file_paths, timeout=30, userhash=None): + """ + Upload multiple files as an album to Catbox and return their links. + + :param file_paths: List of paths to the files to upload. + :param timeout: Timeout in seconds for the upload request. + :param userhash: Optional userhash for authenticated upload. + :return: List of URLs of the uploaded files on Catbox. + """ + uploaded_links = [] + try: + for file_path in file_paths: + with open(file_path, 'rb') as file: + files = {'fileToUpload': file} + data = {'reqtype': 'fileupload'} + + if userhash: + data['userhash'] = userhash + + response = requests.post("https://catbox.moe/user/api.php", files=files, data=data, timeout=timeout) + response.raise_for_status() + uploaded_links.append(response.text.strip()) + return uploaded_links + except requests.exceptions.Timeout: + raise TimeoutError(f"Album upload timed out after {timeout} seconds.") + except requests.exceptions.ConnectionError: + raise ConnectionError("Failed to connect to Catbox. 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)}") + +def delete_files(files, userhash): + """ + Delete multiple files from Catbox using userhash. + + :param files: List of filenames to delete from Catbox. + :param userhash: userhash for authenticated deletion. + """ + try: + data = { + 'reqtype': 'deletefiles', + 'userhash': userhash, + 'files': ' '.join(files) + } + response = requests.post("https://catbox.moe/user/api.php", data=data) + response.raise_for_status() + print(f"Deleted files: {files}") + except requests.RequestException as e: + raise CatboxError(f"Failed to delete files: {str(e)}") + +def create_album(files, title, description, userhash): + """ + Create a new album on Catbox with the specified files. + + :param files: List of filenames that have been uploaded to Catbox. + :param title: Title of the album. + :param description: Description of the album. + :param userhash: userhash for authenticated album creation. + :return: Shortcode of the created album. + """ + try: + data = { + 'reqtype': 'createalbum', + 'userhash': userhash, + 'title': title, + 'desc': description, + 'files': ' '.join(files) + } + response = requests.post("https://catbox.moe/user/api.php", data=data) + response.raise_for_status() + return response.text.strip() + except requests.RequestException as e: + raise CatboxError(f"Failed to create album: {str(e)}") + +def edit_album(shortcode, files, title, description, userhash): + """ + Edit an existing album on Catbox. + + :param shortcode: The short alphanumeric code of the album. + :param files: List of filenames to be part of the album. + :param title: Title of the album. + :param description: Description of the album. + :param userhash: userhash for authenticated album editing. + """ + try: + data = { + 'reqtype': 'editalbum', + 'userhash': userhash, + 'short': shortcode, + 'title': title, + 'desc': description, + 'files': ' '.join(files) + } + response = requests.post("https://catbox.moe/user/api.php", data=data) + response.raise_for_status() + print(f"Successfully edited album {shortcode}") + except requests.RequestException as e: + raise CatboxError(f"Failed to edit album: {str(e)}") + +def delete_album(shortcode, userhash): + """ + Delete an album from Catbox. + + :param shortcode: The short alphanumeric code of the album. + :param userhash: userhash for authenticated album deletion. + """ + try: + data = { + 'reqtype': 'deletealbum', + 'userhash': userhash, + 'short': shortcode + } + response = requests.post("https://catbox.moe/user/api.php", data=data) + response.raise_for_status() + print(f"Successfully deleted album {shortcode}") + except requests.RequestException as e: + raise CatboxError(f"Failed to delete album: {str(e)}") \ No newline at end of file