Skip to content

Commit

Permalink
version alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
N0x1s committed Jun 24, 2020
1 parent 43fa213 commit 575fd84
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 121 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 N0x1s
Copyright (c) 2020 n0x1s (Ilyass El Fatimy)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
72 changes: 51 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@
# data_b64
# b64!
[![Python 3.7.1](https://img.shields.io/badge/Python-3.7.1-green.svg)](http://www.python.org/download/)

convert offline/online data to base64 clean, fast and easy

data_b64 is a python module that convert media to base64
- auto detect media type -> bytes - online media - on desk media
- easy to use


### Installation

data_b64 requires [python3](https://python.org/) to run.

Install the dependencies.

```sh
$ pip install data_b64
```

License
what b64 do for you?
----
* clean, fast and easy
* convert data (image, video, text) to base64 from URL or local or b'string'
* convert base64 to data (image, video, text)
* auto-detect source type all you have to do is inserting the source
* support proxies, custom success code
## install
You can either install from pypi or the source code
1) Using pip
```bash
pip install b64
```
2) from the source code
```bash
git clone https://github.com/n0x1s/data_b64
cd data_b64
python setup.py install
```
## How to use

Let us suppose you have a group of data that you want to convert to base64 or the opposite
```python
>>> from b64 import B64
### some examples ####
>>> online_image = 'https://avatars3.githubusercontent.com/u/1'
>>> local_image = 'test.jpg'
>>> string = 'hello'.encode()
>>> online_base64 = 'https://pastebin.com/raw/mYPrvzDz'
>>> local_base64 = 'example.txt'
>>> b64_string = b'aGVsbG8='
```
1) converting online data
```python
>>> B64(online_image).data # converting image to base64
> b'iVBORw0KGgoAAAANSUhEUgAAAcwAAAHMCAYAAABY25iGA...'
>>> B64('https://pastebin.com/raw/mYPrvzDz', to='string', codes=[200, 201]).data # you can set the success http codes or use proxies
> b'Hello'
```
2) converting local data
```python
>>> B64(local_image).data # converting image to base64
> b'iVBORw0KGgoAAAANSUhEUgAAAcwAAAHMCAYAAABY25iGA...'
>>> B64('example.txt', to='string').data # converting local base64 string to bytes
> b'Hello'
```
## Todo
I will try to maintain this respiratory and update or add new things to it you are welcome to contribute :relaxed:

MIT


**Free Software, Hell Yeah!**
And, as always have a beautiful day!
1 change: 0 additions & 1 deletion __init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
from data_b64.core import Media2B64
1 change: 1 addition & 0 deletions b64/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .core import B64
36 changes: 36 additions & 0 deletions b64/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from b64 import utils
import base64
from urllib import parse
from cached_properties import Property as property


class B64:
""" a class to convert online/offline media to base64/text"""
__version__ = '0.4a'

def __init__(self, source, to='base64', *args, **kwargs):
self.source = source
self.to = to
self._args, self._kwargs = args, kwargs

def convert(self):
""" a method to handle conversation between base64 and bytes """
func = base64.b64encode if self.to == 'base64' else base64.b64decode
return func(self.file_bytes)

@property
def file_bytes(self):
""" cached property for file bytes """
if isinstance(self.source, bytes):
if not self.source:
raise utils.FileNotFound
return self.source
elif parse.urlparse(self.source).scheme:
r = utils.download_file(self.source, *self._args, **self._kwargs)
del self._args, self._kwargs
return r
return utils.read_file(self.source)

@property
def data(self):
return self.convert()
52 changes: 52 additions & 0 deletions b64/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import os
import requests


class FileNotFound(Exception):
""" Exception for file not found """

def __init__(self):
msg = '''
File Not Found, please check your path, permission, and headers
And Try Again'''
super().__init__(msg)


def download_file(source, codes=(200, ), chunk=1024, *args, **kwargs) -> bytes:
""" a function to download files from internet to bytes """
file = bytes()
try:
r = requests.get(source, stream=True, *args, **kwargs)
except Exception as e:
raise FileNotFound
if r.status_code not in codes:
raise FileNotFound
while True:
r.raw.decode_content = True
part = r.raw.read(chunk)
if not part:
break
file += part
return file


def read_file(source: str) -> bytes:
""" read local file bytes """
if os.path.exists(source) and os.path.isfile(source):
with open(source, 'rb') as fobj:
return fobj.read()
raise FileNotFound


def exists(source) -> bool:
""" check if the source exists """
dtype = self._detect_type(source)
if dtype == 'local':
return os.path.exists(source) and os.path.isfile(source)
elif dtype == 'remote':
try:
r = request.urlopen(source)
except Exception as e:
return False
return r.status in code
return bool(source)
98 changes: 0 additions & 98 deletions data_b64/core.py

This file was deleted.

24 changes: 24 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from distutils.core import setup
setup(
name='b64',
packages=['b64'],
version='0.1',
license='MIT',
description='convert local/online data to base64 fast, easy and clean',
author='n0x1s',
author_email='[email protected]',
url='https://github.com/n0x1s/data_b64',
download_url='https://github.com/n0x1s/b64/archive/0.1.tar.gz',
keywords=['base64', 'image to base64', 'video to base64',
'base64 convert'],
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
],
)

0 comments on commit 575fd84

Please sign in to comment.