Skip to content

Instantly share code, notes, and snippets.

@zhwei
Created May 7, 2013 12:31
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 zhwei/5532211 to your computer and use it in GitHub Desktop.
Save zhwei/5532211 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "zhwei"
# 使用imgur的api上传图片并返回url
import urllib2
import urllib
import json
def uploadgeturl(pic):
url = "https://api.imgur.com/3/image"
header = {"Authorization":"Client-ID 58c6c1449861345"}
datas={"image":pic}
datas = urllib.urlencode(datas)
req = urllib2.Request(url,data=datas,headers=header)
json_object = urllib2.urlopen(req).read()
result = json.loads(json_object)['data']['link']
return result
# 测试
#test = file("test.jpg").read()
#print uploadgeturl(test)
@ma6174
Copy link

ma6174 commented May 7, 2013

add this

if __name__=='__main__':
    pic = sys.argv[1]
    try:
        data = open(pic).read()
    except:
        print("Cannot open file")
    print uploadgeturl(data)

and move code to /usr/bin/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment