bastos (owner)

Fork Of

gist: 23788 by segphault A tool for uploading files ...

Revisions

gist: 26427 Download_button fork
public
Public Clone URL: git://gist.github.com/26427.git
Embed All Files: show embed
dropio_uploader.py #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
 
# Drop.io Upload Tool for GNOME
# Copyright (C) 2008 Ryan Paul (SegPhault)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
 
import gtk, pango, pycurl
import threading, simplejson, webbrowser
 
# Insert your API key here
API_KEY = ""
 
class UploadThread(threading.Thread):
  def __init__(self, drop_name, filename, token, callback=None, progress=None):
    threading.Thread.__init__(self)
 
    self.curl = pycurl.Curl()
    self.curl.setopt(pycurl.POST, 1)
    self.curl.setopt(pycurl.URL, "http://assets.drop.io/upload")
    self.curl.setopt(pycurl.NOPROGRESS, 0)
    if callback: self.curl.setopt(self.curl.WRITEFUNCTION, callback)
    if progress: self.curl.setopt(self.curl.PROGRESSFUNCTION, progress)
    self.curl.setopt(self.curl.HTTPPOST, [
      ("file", (self.curl.FORM_FILE, filename)),
      ("version", "1.0"),
      ("format", "json"),
      ("api_key", API_KEY),
      ("drop_name", drop_name),
      ("token", token)])
 
  def run(self):
    self.curl.perform()
    self.curl.close()
 
class DropIoUploader(gtk.Window):
  def __init__(self, drop_name=None, filename=None, token=None):
    gtk.Window.__init__(self)
    self.set_title("Upload a file to Drop.io")
 
    self.set_border_width(10)
    layout = gtk.Table(3, 2, False)
    layout.set_col_spacing(0, 10)
    layout.set_row_spacings(10)
    
    self.drop = gtk.Entry()
    self.token = gtk.Entry()
    self.token.set_visibility(False)
    self.chooser = gtk.FileChooserButton("Select a file")
 
    if drop_name: self.drop.set_text(drop_name)
    if filename: self.chooser.set_uri(filename)
    if token: self.token.set_text(token)
 
    self.btnupload = gtk.Button(stock=gtk.STOCK_SAVE)
    self.btncancel = gtk.Button(stock=gtk.STOCK_CANCEL)
    self.btnupload.connect("clicked", self.on_press_upload)
    self.btncancel.connect("clicked", lambda w: self.destroy())
 
    layout.attach(gtk.Label("Drop name:"), 0, 1, 0, 1, gtk.FILL)
    layout.attach(self.drop, 1, 2, 0, 1, gtk.EXPAND|gtk.FILL)
 
    layout.attach(gtk.Label("Authentication:"), 0, 1, 1, 2, gtk.FILL)
    layout.attach(self.token, 1, 2, 1, 2, gtk.EXPAND|gtk.FILL)
 
    layout.attach(gtk.Label("Select file:"), 0, 1, 2, 3, gtk.FILL)
    layout.attach(self.chooser, 1, 2, 2, 3, gtk.EXPAND|gtk.FILL)
 
    self.status_layout = gtk.VBox()
    self.progressbar = gtk.ProgressBar()
 
    buttons = gtk.HButtonBox()
    buttons.set_spacing(10)
    buttons.set_layout(gtk.BUTTONBOX_END)
    buttons.pack_start(self.btncancel)
    buttons.pack_start(self.btnupload)
 
    vb = gtk.VBox(spacing=10)
    vb.pack_start(layout, True, True)
    vb.pack_start(self.status_layout)
    vb.pack_start(gtk.HSeparator())
    vb.pack_start(buttons)
 
    self.add(vb)
 
  def on_link_clicked(self, link_button):
    webbrowser.open(link_button.get_uri())
 
  def on_upload_progress(self, dt, dd, utotal, udone):
    if utotal:
      self.progressbar.set_fraction(float(udone) / float(utotal))
 
  def on_upload_finish(self, response):
    self.btnupload.set_sensitive(True)
    self.status_layout.remove(self.progressbar)
    
    try:
      data = simplejson.loads(response)
      url = "http://drop.io/%s/asset/%s" % (self.drop.get_text(), data["name"])
      self.response = gtk.LinkButton(url)
      self.response.connect("clicked", self.on_link_clicked)
    except:
      self.response = gtk.Label()
      self.response.set_markup("<b>Upload failed!</b>")
    
    self.status_layout.pack_start(self.response)
    self.response.show()
 
  def on_press_upload(self, btn):
    btn.set_sensitive(False)
    
    if hasattr(self, "response"):
      self.status_layout.remove(self.response)
    
    self.progressbar.set_fraction(0)
    self.status_layout.pack_start(self.progressbar)
    self.progressbar.show()
    
    UploadThread(
      self.drop.get_text(),
      self.chooser.get_filename(),
      self.token.get_text(),
      self.on_upload_finish,
      self.on_upload_progress).start()
 
try:
  import nautilus
 
  class DropIoExtension(nautilus.MenuProvider):
    def __init__(self):
      pass
 
    def on_file_upload(self, menu, file):
      DropIoUploader(filename = file).show_all()
      gtk.main()
 
    def get_file_items(self, window, files):
      if len(files) == 1:
        if not files[0].is_directory():
          item = nautilus.MenuItem(
            "NautilusPython::dropio_upload_file",
            "Upload to Drop.io",
            "Upload %s to Drop.io" % files[0].get_name(),
            "gnome-fs-share")
 
          item.connect("activate", self.on_file_upload, files[0].get_uri())
 
          return item,
except: pass
 
 
if __name__ == "__main__":
  window = DropIoUploader()
  window.connect("destroy", gtk.main_quit)
  window.show_all()
 
  gtk.gdk.threads_init()
  gtk.main()