Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
extr3mis authored Jun 25, 2020
1 parent 279e605 commit e05fb70
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 0 deletions.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# imgbb

<img src='https://img.shields.io/badge/Python-3.5+-blue'> <img src='https://img.shields.io/badge/license-MIT-green'> <img src='https://img.shields.io/badge/async-enabled-blue'>

A simple tool enabling you to asynchronously upload images to <a href='https://www.imgbb.com'>imgbb</a>.
_Requires Python 3.5+_

# Installation

Install this package using `pip`.
```console
$ pip install -e git+https://github.com/extr3mis/imgbb
```

# Usage
Import the package using the given import statement.
```console
$ from imgbb.client import Client
```
Now you can make a `Client` object and initialize it with your own API key from imgbb.
```console
$ myclient = Client(api_key_here)
```
Now you can use your `Client` to upload an image using the `Client.post()` method. Since `Client.post()` is a `coroutine` make sure to `await` it. Catch the response from the request in `request`.
```console
$ response = await myclient.post('/path_to_image/image.jpg','name')
```
Now you can get the URL to the image you've just uploaded using `response['data']['url']`.
```console
$ URL = response['data']['url']
```

# Quick Example
```py
from imgbb.client import Client
import os
import aiohttp
import asyncio
key = os.getenv('IMGBB_API_KEY')
session = aiohttp.ClientSession()
myclient = Client(key,session)

async def upload(image,name):
response = await Client.post(image,name)
url = response['data']['url']
print(f'Uploaded image URL: {url}')

if __name__=='__main__':
asyncio.run(upload('image.jpg','Cool picture'))
```
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import setuptools
22 changes: 22 additions & 0 deletions examples/simple_upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#Imports
from imgbb.client import Client
import os
import aiohttp
import asyncio

#Setting up required vars
key = os.getenv('IMGBB_API_KEY')

#Creating a session to upload images
session = aiohttp.ClientSession()

#Making a Client() object to interact with imgbb
myclient = Client(key,session)

async def upload(image,name):
response = await Client.post(image,name) #Posting to imgbb and collecting response
url = response['data']['url']
print(f'Uploaded image URL: {url}')

if __name__=='__main__':
asyncio.run(upload('image.jpg','Cool picture')) #Runs in an async context
1 change: 1 addition & 0 deletions imgbb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import setuptools
32 changes: 32 additions & 0 deletions imgbb/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import asyncio
import aiohttp
import base64
URL = 'https://api.imgbb.com/1/upload'
class Client():
"""The default interface to interact with Imgbb."""
def __init__(self,key,session=None):
self.key = key
if session is None:
self.session = aiohttp.ClientSession()
else: self.session=session
async def post(self,filename,name):
"""Post using a filename like 'image.jpg'"""
with open(filename,'rb') as f:
payload = {
"key": self.key,
"image": f.read(),
"name": name
}
async with self.session.post(URL, data=payload) as response:
rj = await response.json()
return rj
async def postbyte(self,fp,name):
"""Use when you want to upload a `bytes` object."""
payload = {
"key": self.key,
"image": fp,
"name": name
}
async with self.session.post(URL, data=payload) as response:
rj = await response.json()
return rj
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aiohttp==3.6.2
80 changes: 80 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from setuptools import setup, find_packages # Always prefer setuptools over distutils
from codecs import open # To use a consistent encoding
from os import path

here = path.abspath(path.dirname(__file__))

# Get the long description from the relevant file
setup(
name='imgbb',

# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# http://packaging.python.org/en/latest/tutorial.html#version
version='1.1.0',

description='Unofficial Imgbb python library with async and await.',
long_description='',

# The project's main homepage.
url='https://github.com/extr3mis/imgbb',

# Author details
author='extr3mis',
author_email='bitechlasana@gmail.com',

# Choose your license
license='MIT',

# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
classifiers=[
# How mature is this project? Common values are
# 3 - Alpha
# 4 - Beta
# 5 - Production/Stable
'Development Status :: 3 - Alpha',

# Indicate who your project is intended for
'Intended Audience :: Developers',

# Pick your license as you wish (should match "license" above)
'License :: OSI Approved :: MIT License',

# Specify the Python versions you support here. In particular, ensure
# that you indicate whether you support Python 2, Python 3 or both.
'Programming Language :: Python :: 3.8'
'Programming Language :: Python :: 3.7'
'Programming Language :: Python :: 3.6'
'Programming Language :: Python :: 3.5'

],

# What does your project relate to?
keywords=['api', 'imgbb', 'client'],

# You can just specify the packages manually here if your project is
# simple. Or you can use find_packages().
packages=find_packages(),

# List run-time dependencies here. These will be installed by pip when your
# project is installed. For an analysis of "install_requires" vs pip's
# requirements files see:
# https://packaging.python.org/en/latest/technical.html#install-requires-vs-requirements-files
install_requires=['asyncio','aiohttp'],

# If there are data files included in your packages that need to be
# installed, specify them here. If using Python 2.6 or less, then these
# have to be included in MANIFEST.in as well.
package_data={},

# Although 'package_data' is the preferred approach, in some case you may
# need to place data files outside of your packages.
# see http://docs.python.org/3.4/distutils/setupscript.html#installing-additional-files
# In this case, 'data_file' will be installed into '<sys.prefix>/my_data'
data_files=[],

# To provide executable scripts, use entry points in preference to the
# "scripts" keyword. Entry points provide cross-platform support and allow
# pip to create the appropriate form of executable for the target platform.
entry_points={},
)

0 comments on commit e05fb70

Please sign in to comment.