Skip to content

Instantly share code, notes, and snippets.

@cnbeining
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cnbeining/6c051a6a9037b7fc6474 to your computer and use it in GitHub Desktop.
Save cnbeining/6c051a6a9037b7fc6474 to your computer and use it in GitHub Desktop.
Bash upload image to imgur
#Bash upload image to imgur
#Beining http://www.cnbeining.com/
#NEED requests !!!!!
import sys
import os
import glob
import requests
from base64 import b64encode
url = 'https://api.imgur.com/3/image.json'
line_to_write_this = ''
headers = {"Authorization": "Client-ID **********"}#change me!
filelist = glob.glob('*.*')#change me!
print(filelist)
f = open('result.csv', "a")
for file_this in filelist:
print(file_this)
try:
j1 = requests.post(
url,
headers = headers,
data = {
'image': b64encode(open(file_this, 'rb').read()),
'type': 'base64',
}
)
info = json.loads(j1.text.decode('utf-8'))
status_this = str(info['status'])
link_this = str(info['data']['link'])
line_to_write_this = str(file_this) + ',' + link_this + ',' + status_this + '\n'
f.writelines(line_to_write_this)
print(status_this)
except:
print('ERROR')
line_to_write_this = ',,ERROR' + '\n'
f = open('result.csv', "a")
f.writelines(line_to_write_this)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment