Created
August 8, 2021 09:20
-
-
Save harshitsinghai77/e9117300e2db6a63538ee51a9d9cad88 to your computer and use it in GitHub Desktop.
Imgbb upload image via image url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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