Skip to content

Commit

Permalink
Add Raw URL (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
smashedr authored Jul 5, 2023
1 parent 592fe4e commit 8d066d3
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 42 deletions.
82 changes: 59 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,68 @@
[![Discord](https://img.shields.io/discord/899171661457293343?color=7289da&label=discord&logo=discord&logoColor=white&style=flat)](https://discord.gg/wXy6m2X8wY)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/1eee626c47fa4e6fb8b1ed3efdd3e518)](https://app.codacy.com/gh/cssnr/zipline-cli/dashboard)
[![PyPI](https://img.shields.io/pypi/v/zipline-cli)](https://pypi.org/project/zipline-cli/)
[![](https://repository-images.githubusercontent.com/661201286/8dfadbc8-94c0-4eaa-88bd-7ee351859510)](https://zipline.diced.tech/)
[![Discord](https://img.shields.io/discord/899171661457293343?logo=discord&logoColor=white&label=Discord)](https://discord.gg/wXy6m2X8wY)
[![PyPI](https://img.shields.io/github/issues-raw/cssnr/zipline-cli?logo=github&logoColor=white&label=Issues)](https://github.com/cssnr/zipline-cli/issues)
[![Codacy Badge](https://img.shields.io/codacy/grade/1eee626c47fa4e6fb8b1ed3efdd3e518?logo=codacy&logoColor=white&label=Codacy)](https://app.codacy.com/gh/cssnr/zipline-cli/dashboard)
[![PyPI](https://img.shields.io/pypi/v/zipline-cli?logo=python&logoColor=white&label=PyPi)](https://pypi.org/project/zipline-cli/)
[![](https://repository-images.githubusercontent.com/661201286/8dfadbc8-94c0-4eaa-88bd-7ee351859510)](https://github.com/cssnr/zipline-cli)
# Zipline CLI

Python 3 CLI Uploader for Zipline.
Zipline CLI is currently functional and **Under Active Development**.
Please open a [Feature Request](https://github.com/cssnr/zipline-cli/discussions/new?category=feature-requests)
for new features and submit an [Issue](https://github.com/cssnr/zipline-cli/issues)
for any bugs you find.

* Zipline: [https://zipline.diced.tech/](https://zipline.diced.tech/)
* Zipline Docs: [https://zipline.diced.tech/](https://zipline.diced.tech/)

This is currently a **WIP** and not complete, but has some useful functions.
## Table of Contents

* [Quick Start](#quick-start)
* [Install](#install)
* [CLI Usage](#cli-usage)
* [Environment Variables](#environment-variables)
* [Python API Reference](#python-api-reference)
* [Additional Information](#additional-information)

## Quick Start

```bash
python3 -m pip install zipline-cli
zipline --setup
```

## Install

From PyPi using pip:
```text
```bash
python3 -m pip install zipline-cli
```

From GitHub using pip:
```text
```bash
python3 -m pip install git+https://github.com/cssnr/zipline-cli.git
```

From Source using pip:
```bash
git clone https://github.com/cssnr/zipline-cli.git
python3 -m pip install -e zipline-cli
```

From Source using setuptools:
```text
```bash
git clone https://github.com/cssnr/zipline-cli.git
cd zipline-cli
python3 setup.py install
```

Uninstall:
```text
### Uninstall

To completely remove from any above install methods:
```bash
python3 -m pip uninstall zipline-cli
```

## CLI Usage

You will need a Zipline URL and Token to use the utility.

Setup Zipline URL and Token:
```bash
zipline --setup
Expand All @@ -48,22 +73,27 @@ Upload a File:
zipline test.txt
```

Upload Multiple Files:
```bash
zipline file1.txt file2.txt
```

Create Text File from Input
```bash
cat test.txt | zipline
```

Create Text File from Text
Create Text File from Clipboard
```bash
zipline
# type or paste contents followed by Ctrl+D (Ctrl+Z on Windows)
# Paste or Type contents, followed by a newline, then Ctrl+D (Ctrl+Z on Windows)
```

## Environment Variables

Environment Variables are stored in the `.zipline` file in your home directory.

* Location: `~/.zipline`
* Location: `~/.zipline` or `$HOME/.zipline`

| Variable | Description |
|----------------|-----------------------------------------------------------------------------|
Expand All @@ -72,13 +102,14 @@ Environment Variables are stored in the `.zipline` file in your home directory.
| ZIPLINE_EMBED | Set this enable Embed on your uploads |
| ZIPLINE_EXPIRE | See: https://zipline.diced.tech/docs/guides/upload-options#image-expiration |

You may also override them by exporting the variables in your current environment.
You may override them by exporting the variables in your current environment
or using the corresponding command line arguments. See `-h` for more info.

## API Reference
## Python API Reference

Initialize the class with your Zipline URL.
Everything else is a header passed as a kwarg.
The API does not yet support environment variables.
Initialize the class with your Zipline URL.
Everything else is a header passed as a kwarg.
The API does not yet support environment variables.

Zipline Token/Authorization is a header kwarg and can be passed as follows:
```python
Expand All @@ -97,5 +128,10 @@ print(url)

## Additional Information

> If you have more questions, concerns, or comments?
> Join our [Discord](https://discord.gg/wXy6m2X8wY) for more information...
Still have questions, concerns, or comments?

* [Feature Requests](https://github.com/cssnr/zipline-cli/discussions/categories/feature-requests)
* [Helpdesk Q&A](https://github.com/cssnr/zipline-cli/discussions/categories/helpdesk-q-a)
* [Discord](https://discord.gg/wXy6m2X8wY)

> Zipline Guide: Hit That Fresh Nar Nar: [youtube.com/watch?v=bJHYo2aGWgE](https://www.youtube.com/watch?v=bJHYo2aGWgE)
17 changes: 16 additions & 1 deletion build.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
$ErrorActionPreference = "Stop"

$egg_dir = "zipline_cli.egg-info"
#if (-Not $args[0]) {
# Write-Output "Specify version number with format: x.x.x"
# exit
#}
#$env:ZIPLINE_VERSION = $args[0]
#Write-Output "Version: $env:ZIPLINE_VERSION"

$egg_dir = ".\zipline_cli.egg-info"
if (Test-Path $egg_dir) {
Remove-Item -Force -Recurse $egg_dir
}
if (Test-Path ".\__pycache__") {
Remove-Item -Force -Recurse ".\__pycache__"
}
if (Test-Path ".\build") {
Remove-Item -Force -Recurse ".\build"
}
if (Test-Path ".\dist") {
Remove-Item -Force -Recurse ".\dist"
}

if ($args[0] -eq "clean") {
Write-Output "Clean Only Done. Not Building."
exit
}

python.exe -m build

Write-Output "Success."
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
long_description = f.read()

setup(
version='0.1.3',
name='zipline-cli',
version='0.1.2',
description='Python 3 CLI for Zipline',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
73 changes: 56 additions & 17 deletions zipline.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,85 @@
from typing import Any, Dict, List, Optional, TextIO


class ZipURL(object):
"""
Zipline URL Object
:param file_url: str: Zipline File Display URL
"""

__slots__ = ['url', 'raw']

def __init__(self, file_url: str):
self.url: str = file_url
self.raw: str = self._get_raw(file_url)

def __repr__(self):
return f'<url={self.url} raw={self.raw}>'

def __str__(self):
return self.url

@staticmethod
def _get_raw(url: str) -> str:
try:
s = url.split('/', 4)
return f"{s[0]}//{s[2]}/r/{s[4]}"
except Exception:
return ''


class Zipline(object):
"""
Zipline Python API
:param url: str: Zipline URL
:param base_url: str: Zipline URL
:param kwargs: Zipline Headers
"""
allowed_headers = ['format', 'image_compression_percent', 'expires_at',
'password', 'zws', 'embed', 'max_views', 'uploadtext',
'authorization', 'no_json', 'x_zipline_filename',
'original_name', 'override_domain']

def __init__(self, zipline_url: str, **kwargs):
self.zipline_url: str = zipline_url.rstrip('/')
self.headers: Dict[str, str] = {}
def __init__(self, base_url: str, **kwargs):
self.base_url: str = base_url.rstrip('/')
self._headers: Dict[str, str] = {}
for header, value in kwargs.items():
if header.lower() not in self.allowed_headers:
continue
if value is None:
continue
key = header.replace('_', '-').title()
self.headers[key] = str(value)
self._headers[key] = str(value)

def send_file(self, file_name: str, file_object: TextIO,
overrides: Optional[dict] = None) -> str:
overrides: Optional[dict] = None) -> ZipURL:
"""
Send File to Zipline
:param file_name: str: Name of File for files tuple
:param file_object: TextIO: File to Upload
:param overrides: dict: Header Overrides
:return: str: File URL
"""
url = self.zipline_url + '/api/upload'
url = self.base_url + '/api/upload'
files = {'file': (file_name, file_object)}
headers = self.headers | overrides if overrides else self.headers
headers = self._headers | overrides if overrides else self._headers
r = requests.post(url, headers=headers, files=files)
r.raise_for_status()
return r.json()['files'][0]
return ZipURL(r.json()['files'][0])
# return f'https://example.com/dummy/{file_name}'


def format_output(filename: str, url: ZipURL) -> str:
"""
Format URL Output
:param filename: str: Original or File Name
:param url: ZipURL: ZipURL to Format
:return: str: Formatted Output
"""
if url.raw:
return f'{filename}\n{url}\n{url.raw}'
return f'{filename}\n{url}'


def gen_rand(length: Optional[int] = 4) -> str:
"""
Generate Random Streng at Given length
Expand Down Expand Up @@ -153,22 +192,22 @@ def main() -> None:
content: str = sys.stdin.read().rstrip('\n') + '\n'
text_f: TextIO = io.StringIO(content)
name = f'{gen_rand(8)}.txt'
url: str = zipline.send_file(name, text_f)
print(f'{name} -> {url}')
url: ZipURL = zipline.send_file(name, text_f)
print(format_output(name, url))
sys.exit(0)

exit_code = 1
for filename in args.files:
if not os.path.isfile(filename):
print(f'Warning: File Not Found: {filename}')
for name in args.files:
if not os.path.isfile(name):
print(f'Warning: File Not Found: {name}')
continue
with open(filename) as f:
with open(name) as f:
# name, ext = os.path.splitext(os.path.basename(filename))
# ext = f'.{ext}' if ext else ''
# name = f'{name}-{gen_rand(8)}{ext}'
# url: str = zipline.send_file(name, f)
url: str = zipline.send_file(filename, f)
print(f'{filename} -> {url}')
url: ZipURL = zipline.send_file(name, f)
print(format_output(name, url))
exit_code = 0
sys.exit(exit_code)

Expand Down

0 comments on commit 8d066d3

Please sign in to comment.