Skip to content

Commit

Permalink
add litterbox
Browse files Browse the repository at this point in the history
  • Loading branch information
lunaticsm committed Sep 6, 2024
1 parent ea7cb5f commit d7612cc
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 25 deletions.
98 changes: 88 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
29 changes: 15 additions & 14 deletions src/catbox/catbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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)}")

0 comments on commit d7612cc

Please sign in to comment.