#!/usr/bin/env python
#coding:utf-8

# help you upload an image in clipboard to qiniu cloud storage, 
# so you don't need to download a picture and click the upload button before uploading it.
# must install qiniu(use pip) before using.

import gtk

# get gtk.SelectionData object or None according to the given target
# how to get the target value? you can use gtk.Clipboard.wait_for_targets to get the target of current clipboard
image = gtk.clipboard_get().wait_for_contents('image/png')

if image is None:
    print 'no image in clipboard!' 
else:
    from qiniu import Auth, put_data
    # 'key_qiniu' is another file storing my keys and bucket_name, touch one by yourself
    from key_qiniu import access_key,secret_key,bucket_name
    q = Auth(access_key,secret_key)

    import time
    key = 'from_clipboard/' + time.strftime('%Y/%m/%d/%H/%M/%S', time.localtime(time.time())) 
    print key

    data = image.data
    token = q.upload_token(bucket_name)
    ret, info = put_data(token,key,data)
    print ret
    assert ret['key'] == key
    

@@ -94,17 +94,70 @@
 torage, 
+and set the clipboard with url of the image on qiniu.
 %0A
-
 # so you
@@ -478,21 +478,25 @@
 ipboard%0A
-image
+clipboard
  = gtk.c
@@ -509,16 +509,35 @@
 rd_get()
+ %0Aimage = clipboard
 .wait_fo
@@ -707,16 +707,29 @@
  my keys
+,qiniu_domain
  and buc
@@ -819,16 +819,29 @@
 ket_name
+,qiniu_domain
 %0A    q =
@@ -981,16 +981,17 @@
 me())) %0A
+#
     prin
@@ -996,16 +996,16 @@
 int key%0A
-
 %0A    dat
@@ -1102,16 +1102,17 @@
 data)%0A  
+#
   print 
@@ -1144,13 +1144,183 @@
  == key%0A
+    image_url = qiniu_domain + '/' + key%0A    clipboard.set_text(image_url, len(image_url))%0A    clipboard.store()%0A    print 'success!'%0A    print clipboard.wait_for_text()%0A
     %0A

set clipboard with return-url