Skip to content

Instantly share code, notes, and snippets.

@harshitsinghai77
Created August 8, 2021 09:20
Show Gist options
  • Save harshitsinghai77/e9117300e2db6a63538ee51a9d9cad88 to your computer and use it in GitHub Desktop.
Save harshitsinghai77/e9117300e2db6a63538ee51a9d9cad88 to your computer and use it in GitHub Desktop.
Imgbb upload image via image url
import sys
from urllib import request, parse
import json
API_KEY = 'API_KEY'
IMG_UPLOAD = 'https://api.imgbb.com/1/upload'
# total arguments
n = len(sys.argv)
def upload_image(img_url):
post_body = {
'key': API_KEY,
'image': img_url
}
data = parse.urlencode(post_body).encode()
req = request.Request(IMG_UPLOAD, data=data) # this will make the method "POST"
resp = request.urlopen(req)
raw_data = resp.read()
encoding = resp.info().get_content_charset('utf8') # JSON default
data = json.loads(raw_data.decode(encoding))
print(data['data']['url'])
if n < 2:
print("No arguments found")
else:
img_url = sys.argv[1]
upload_image(img_url=img_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment