#!/usr/bin/env python
import postimg
import argparse
import pyperclip
import json
def main(args):
    if not args.quiet:
        print "Uploading....."
    resp = postimg.Imgur(args.img_path).upload()
    if not resp['success']:
        if not args.quiet:
            print json.dumps(resp, sort_keys=True, indent=4, separators=(',', ': '))
            print "Unable to upload !!!"
        return None
    link = resp['data']['link']
    if args.github:
        link = '![GithubSnap](%s)'%link
    elif args.reddit:
        link = '[Reddit](%s)'%link
    elif args.html:
        link = '<img src="%s" alt="snap">'%link
    pyperclip.copy(link)
    print link

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Post/upload image on imgur.com', epilog='link will automatically copied to clipboard')
    parser.add_argument('img_path', type=str, help='image path of file')
    parser.add_argument('--github', action='store_true', help='Github markdown code of imgur url')
    parser.add_argument('--html', action='store_true', help='html <img> code of imgur url')
    parser.add_argument('--reddit', action='store_true', help='reddit markdown  code of imgur url')
    parser.add_argument('-q','--quiet', action='store_true', help='print only img url without verbose output')
    args = parser.parse_args()
    try:
        main(args)
    except KeyboardInterrupt:
        print "Error: Interrupted by user!!"
