mediafire 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/MIT_LICENSE +7 -0
- data/README.md +8 -0
- data/Rakefile +1 -0
- data/examples/common.rb +27 -0
- data/examples/create_folder.rb +7 -0
- data/examples/delete_file.rb +15 -0
- data/examples/download_image.rb +12 -0
- data/examples/dropbox_setup.rb +8 -0
- data/examples/dropbox_setup_option.rb +13 -0
- data/examples/dropbox_upload.rb +6 -0
- data/examples/edit_fileoption.rb +20 -0
- data/examples/edit_note.rb +16 -0
- data/examples/get_myfile_list.rb +4 -0
- data/examples/image_rotation.rb +13 -0
- data/examples/move_file.rb +10 -0
- data/examples/save_file.rb +6 -0
- data/examples/toggle_acl.rb +7 -0
- data/examples/update.rb +22 -0
- data/examples/upload_file.rb +12 -0
- data/lib/mediafire.rb +15 -0
- data/lib/mediafire/api.rb +416 -0
- data/lib/mediafire/client.rb +17 -0
- data/lib/mediafire/connection.rb +92 -0
- data/lib/mediafire/error.rb +24 -0
- data/lib/mediafire/store_object.rb +144 -0
- data/lib/mediafire/version.rb +3 -0
- data/mediafire.gemspec +24 -0
- metadata +96 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/MIT_LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2011 Hiroki Sato <m@skahack.com>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/examples/common.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require '../lib/mediafire'
|
2
|
+
require 'uuidtools'
|
3
|
+
|
4
|
+
def login
|
5
|
+
account = ''
|
6
|
+
password = ''
|
7
|
+
|
8
|
+
m = Mediafire.new
|
9
|
+
m.login(account, password)
|
10
|
+
return m
|
11
|
+
end
|
12
|
+
|
13
|
+
def pritty_format_datafile(f)
|
14
|
+
out = "filename:#{f.name} "
|
15
|
+
out << "size:#{f.size} "
|
16
|
+
out << "link:http://www.mediafire.com/download.php?#{f.quickkey}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def root_folder
|
20
|
+
key = 'b37807725fe86791'
|
21
|
+
data = ['1', '2', '0', key, key, 'myfiles', '0'].fill('', 7..20)
|
22
|
+
Mediafire::StoreObject.new(data)
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_uuid
|
26
|
+
UUIDTools::UUID.random_create.to_s
|
27
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require './common.rb'
|
2
|
+
|
3
|
+
m = login
|
4
|
+
|
5
|
+
uuid = create_uuid
|
6
|
+
folder = m.create_folder(uuid, root_folder)
|
7
|
+
|
8
|
+
uuid1 = create_uuid
|
9
|
+
File.open(uuid1, "w") do |f|
|
10
|
+
f.write(uuid1)
|
11
|
+
end
|
12
|
+
file = m.upload(uuid1)
|
13
|
+
File.delete(uuid1)
|
14
|
+
|
15
|
+
re = m.delete([file, folder])
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require './common'
|
2
|
+
|
3
|
+
m = login
|
4
|
+
|
5
|
+
uuid = create_uuid
|
6
|
+
File.open(uuid, "w") do |f|
|
7
|
+
f.write(uuid)
|
8
|
+
end
|
9
|
+
|
10
|
+
m = login
|
11
|
+
file = m.upload(uuid)
|
12
|
+
options = {
|
13
|
+
:filename => 'edited',
|
14
|
+
:description => 'edited',
|
15
|
+
:tags => 'edited',
|
16
|
+
:password => 'edited',
|
17
|
+
}
|
18
|
+
m.edit_fileoption(file, options)
|
19
|
+
|
20
|
+
File.delete(uuid)
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require './common'
|
2
|
+
|
3
|
+
uuid = create_uuid
|
4
|
+
File.open(uuid, "w") do |f|
|
5
|
+
f.write(uuid)
|
6
|
+
end
|
7
|
+
|
8
|
+
m = login
|
9
|
+
file = m.upload(uuid)
|
10
|
+
note = {
|
11
|
+
:subject => 'edited',
|
12
|
+
:description=> 'edited',
|
13
|
+
}
|
14
|
+
m.edit_note(file, note)
|
15
|
+
|
16
|
+
File.delete(uuid)
|
data/examples/update.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require './common'
|
2
|
+
|
3
|
+
m = login
|
4
|
+
|
5
|
+
uuid = create_uuid
|
6
|
+
File.open(uuid, "w") do |f|
|
7
|
+
f.write(uuid)
|
8
|
+
end
|
9
|
+
file_a = m.upload(uuid)
|
10
|
+
File.delete(uuid)
|
11
|
+
|
12
|
+
uuid = create_uuid
|
13
|
+
File.open(uuid, "w") do |f|
|
14
|
+
f.write(uuid)
|
15
|
+
end
|
16
|
+
file_b = m.upload(uuid)
|
17
|
+
File.delete(uuid)
|
18
|
+
|
19
|
+
puts pritty_format_datafile(file_a)
|
20
|
+
puts pritty_format_datafile(file_b)
|
21
|
+
m.update(file_a, file_b)
|
22
|
+
|
data/lib/mediafire.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'nokogiri'
|
4
|
+
require 'mediafire/connection'
|
5
|
+
require 'mediafire/store_object'
|
6
|
+
require 'mediafire/error'
|
7
|
+
require 'mediafire/client'
|
8
|
+
|
9
|
+
module Mediafire
|
10
|
+
class << self
|
11
|
+
def new()
|
12
|
+
Mediafire::Client.new()
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,416 @@
|
|
1
|
+
module Mediafire
|
2
|
+
module API
|
3
|
+
include Mediafire::Connection
|
4
|
+
|
5
|
+
def toppage
|
6
|
+
get('')
|
7
|
+
end
|
8
|
+
|
9
|
+
def login(account, password)
|
10
|
+
@cookie = {}
|
11
|
+
toppage
|
12
|
+
unless is_loggedin?
|
13
|
+
options = {
|
14
|
+
:login_email => account,
|
15
|
+
:login_pass => password,
|
16
|
+
:login_remember => 'on',
|
17
|
+
"submit_login.x" => 0,
|
18
|
+
"submit_login.y" => 0,
|
19
|
+
}
|
20
|
+
post("dynamic/login.php", options)
|
21
|
+
end
|
22
|
+
if @cookie.key?('user')
|
23
|
+
@loggedin = true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def list
|
28
|
+
raise NeedLogin unless is_loggedin?
|
29
|
+
|
30
|
+
datas = []
|
31
|
+
response = get(myfiles_path)
|
32
|
+
response.body.scan(/es\[\d+\]=Array\((.*?)\);/).each do |n|
|
33
|
+
data = eval("[#{n[0]}]")
|
34
|
+
datas.push StoreObject.new(data)
|
35
|
+
end
|
36
|
+
key = response.body.sub(/var eU = '(.*)';/, "\1")
|
37
|
+
root_folder = ['1', '2', '0', key, key, 'myfiles', '0'].fill('', 7..20)
|
38
|
+
@root_folder = StoreObject.new(root_folder)
|
39
|
+
return datas
|
40
|
+
end
|
41
|
+
|
42
|
+
def delete(files)
|
43
|
+
raise NeedLogin unless is_loggedin?
|
44
|
+
|
45
|
+
form_option = {
|
46
|
+
:form_todolist1 => create_list(files),
|
47
|
+
:form_todolist2 => '',
|
48
|
+
:form_todotype => 1,
|
49
|
+
:form_todoparent => '',
|
50
|
+
:form_todoconfirm => '',
|
51
|
+
}
|
52
|
+
options = api_options[:doselected].merge(form_option)
|
53
|
+
response = post('dynamic/doselected.php', options)
|
54
|
+
|
55
|
+
if response.body.match(/et ?= ?15;/)
|
56
|
+
true
|
57
|
+
else
|
58
|
+
false
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def move(files, folder)
|
63
|
+
raise NeedLogin unless is_loggedin?
|
64
|
+
|
65
|
+
form_option = {
|
66
|
+
:move_dest_navbar_filelist => folder.quickkey,
|
67
|
+
:move_list => create_list(files),
|
68
|
+
:Move => 'Move To Folder',
|
69
|
+
}
|
70
|
+
options = api_options[:move2folder].merge(form_option)
|
71
|
+
response = post('dynamic/move2folder.php', options)
|
72
|
+
if response.body.match(/et ?= ?15;/)
|
73
|
+
true
|
74
|
+
else
|
75
|
+
false
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# @param options {:filename, :description, :tags, :password}
|
80
|
+
def edit_fileoption(file, options)
|
81
|
+
raise NeedLogin unless is_loggedin?
|
82
|
+
|
83
|
+
new_options = {
|
84
|
+
:todotype => file.objecttype,
|
85
|
+
:todoquickkey => file.quickkey,
|
86
|
+
:todofilename => file.name,
|
87
|
+
:filename => options[:filename] || file.name,
|
88
|
+
:desc => options[:description] || file.description,
|
89
|
+
:tags => options[:tags] || file.tags,
|
90
|
+
:password => options[:password] || file.password,
|
91
|
+
}
|
92
|
+
|
93
|
+
if new_options[:desc].size > 200
|
94
|
+
puts 'Error: description too long'
|
95
|
+
return
|
96
|
+
end
|
97
|
+
|
98
|
+
if new_options[:tags].size > 100
|
99
|
+
puts 'Error: tags too long'
|
100
|
+
return
|
101
|
+
end
|
102
|
+
|
103
|
+
response = post('dynamic/dofileoptions.php', new_options)
|
104
|
+
if response.body.match(/et ?= ?15;/)
|
105
|
+
true
|
106
|
+
else
|
107
|
+
false
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# @param note {:subject, :description}
|
112
|
+
def edit_note(file, note)
|
113
|
+
raise NeedLogin unless is_loggedin?
|
114
|
+
|
115
|
+
options = {
|
116
|
+
:subject => note[:subject] || file.note_subject,
|
117
|
+
:description => note[:description] || file.note_description,
|
118
|
+
}
|
119
|
+
if options[:description].length > 10000
|
120
|
+
puts "Error: description too long"
|
121
|
+
return
|
122
|
+
end
|
123
|
+
type = file.is_folder? ? 'folder' : 'file'
|
124
|
+
post("dynamic/editfilenotes.php?quickkey=#{file.quickkey}&t=#{type}", options)
|
125
|
+
return true
|
126
|
+
end
|
127
|
+
|
128
|
+
def create_folder(name, folder)
|
129
|
+
raise NeedLogin unless is_loggedin?
|
130
|
+
|
131
|
+
options = {
|
132
|
+
:new_folder_parent => folder.folderkey,
|
133
|
+
:new_folder_name => name,
|
134
|
+
}
|
135
|
+
response = post("dynamic/createfolder.php", options)
|
136
|
+
unless response.body.match(/et ?= ?15;/)
|
137
|
+
return nil
|
138
|
+
end
|
139
|
+
|
140
|
+
quickkey = response.body.match(/var ia='(.*?)';/)[1]
|
141
|
+
date = Time.now.gmtime.strftime("%m/%d/%Y")
|
142
|
+
fquickkey = response.body.match(/var mO='(.*?)';/)[1]
|
143
|
+
|
144
|
+
data = ['1', '2', 0, quickkey, folder.folderkey, name, '0', '', '',
|
145
|
+
'', date, '0', '', '', '', fquickkey, '0', '0', '', '', '']
|
146
|
+
StoreObject.new(data)
|
147
|
+
end
|
148
|
+
|
149
|
+
def toggle_acl(file, recursive=false)
|
150
|
+
raise NeedLogin unless is_loggedin?
|
151
|
+
|
152
|
+
if file.access_type == '0'
|
153
|
+
access_type = '1'
|
154
|
+
else
|
155
|
+
access_type = '0'
|
156
|
+
end
|
157
|
+
query = "quickkey=#{file.quickkey}&acl=#{access_type}&type=#{file.objecttype}"
|
158
|
+
if recursive && file.is_folder?
|
159
|
+
query << "&recursive=1"
|
160
|
+
end
|
161
|
+
response = get("dynamic/doaclchange.php?#{query}")
|
162
|
+
if response.body.match(/et ?= ?15;/)
|
163
|
+
true
|
164
|
+
else
|
165
|
+
false
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def upload(filepath, folder=nil)
|
170
|
+
filename = File.basename(filepath)
|
171
|
+
keys = {}
|
172
|
+
response = get('basicapi/uploaderconfiguration.php')
|
173
|
+
doc = Nokogiri::XML(response.body)
|
174
|
+
doc.xpath('//mediafire/config').each do |c|
|
175
|
+
keys[:user] = c.xpath('./user').text
|
176
|
+
keys[:ukey] = c.xpath('./ukey').text
|
177
|
+
keys[:upload_session] = c.xpath('./upload_session').text
|
178
|
+
keys[:trackkey] = c.xpath('./trackkey').text
|
179
|
+
if folder.nil?
|
180
|
+
keys[:folderkey] = c.xpath('./folderkey').text
|
181
|
+
else
|
182
|
+
keys[:folderkey] = folder.status_id
|
183
|
+
end
|
184
|
+
end
|
185
|
+
keys[:mful_config] = doc.xpath('//mediafire/MFULConfig').text
|
186
|
+
|
187
|
+
query = "type=basic"
|
188
|
+
query << "&ukey=#{keys[:ukey]}&user=#{keys[:user]}"
|
189
|
+
query << "&uploadkey=#{keys[:folderkey]}"
|
190
|
+
query << "&filenum=0&uploader=0"
|
191
|
+
query << "&MFULConfig=#{keys[:mful_config]}"
|
192
|
+
response = File.open(filepath) do |f|
|
193
|
+
options = {:Filedata => UploadIO.new(f, 'application/octet-stream', filename)}
|
194
|
+
post("douploadtoapi?#{query}", options)
|
195
|
+
end
|
196
|
+
doc = Nokogiri::XML(response.body)
|
197
|
+
keys[:filekey] = doc.xpath('//response/doupload/key').text
|
198
|
+
|
199
|
+
if keys[:filekey] == ''
|
200
|
+
puts "Error: upload failed."
|
201
|
+
return nil
|
202
|
+
end
|
203
|
+
|
204
|
+
keys[:quickkey] = nil
|
205
|
+
query = "key=#{keys[:filekey]}&MFULConfig=#{keys[:mful_config]}"
|
206
|
+
while keys[:quickkey] == nil || keys[:quickkey] == ''
|
207
|
+
response = get("basicapi/pollupload.php?#{query}")
|
208
|
+
doc = Nokogiri::XML(response.body)
|
209
|
+
keys[:quickkey] = doc.xpath('//response/doupload/quickkey').text
|
210
|
+
keys[:size] = doc.xpath('//response/doupload/size').text
|
211
|
+
sleep 0.5
|
212
|
+
end
|
213
|
+
|
214
|
+
options = {
|
215
|
+
:filename => filename,
|
216
|
+
:quickkey => keys[:quickkey],
|
217
|
+
}
|
218
|
+
response = post("basicapi/getfiletype.php?MFULConfig=#{keys[:mful_config]}", options)
|
219
|
+
doc = Nokogiri::XML(response.body)
|
220
|
+
keys[:filetype] = doc.xpath('//response/file/filetype').text
|
221
|
+
keys[:sharekey] = doc.xpath('//response/file/sharekey').text
|
222
|
+
keys[:r_size], keys[:r_sizeunit] = filesize_to_readable_filesize(keys[:size].to_i)
|
223
|
+
|
224
|
+
data = ['1', '1', keys[:filetype], keys[:quickkey], '', filename, keys[:size],
|
225
|
+
keys[:r_size], keys[:r_sizeunit], '0', Time.now.gmtime.strftime("%m/%d/%Y"),
|
226
|
+
'0', '', '', '', keys[:sharekey], '0', '0', '', '', '']
|
227
|
+
StoreObject.new(data)
|
228
|
+
end
|
229
|
+
|
230
|
+
#def webupload(url, folder)
|
231
|
+
#end
|
232
|
+
|
233
|
+
#def download(file)
|
234
|
+
#end
|
235
|
+
|
236
|
+
#def download_selected_files_in_folder(folder, files)
|
237
|
+
#end
|
238
|
+
|
239
|
+
def download_image(file, size=6)
|
240
|
+
unless file.is_picture?
|
241
|
+
puts 'Error: file is not a picture.'
|
242
|
+
return
|
243
|
+
end
|
244
|
+
if size < 0 && size > 6
|
245
|
+
puts 'Error: size must be between 0 to 6.'
|
246
|
+
return
|
247
|
+
end
|
248
|
+
|
249
|
+
response = get("imgbnc.php/#{file.imagekey}#{size.to_s}g.jpg")
|
250
|
+
response['Location']
|
251
|
+
end
|
252
|
+
|
253
|
+
def update(src, dest)
|
254
|
+
raise NeedLogin unless is_loggedin?
|
255
|
+
|
256
|
+
option = {
|
257
|
+
"file_a" => src.quickkey,
|
258
|
+
"file_b" => dest.quickkey,
|
259
|
+
}
|
260
|
+
response = post('dynamic/updatefile.php', option)
|
261
|
+
if response.body.match(/et ?= ?15;/)
|
262
|
+
true
|
263
|
+
else
|
264
|
+
false
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
def image_rotation(file, rotation)
|
269
|
+
raise NeedLogin unless is_loggedin?
|
270
|
+
|
271
|
+
unless file.is_picture?
|
272
|
+
puts "Error: file is not a picture."
|
273
|
+
return
|
274
|
+
end
|
275
|
+
|
276
|
+
unless ratation == 0 || ratation == 90 || ratation == 180 || ratation == 270
|
277
|
+
puts "Error: rotation need 0 or 90 or 180 or 270"
|
278
|
+
return
|
279
|
+
end
|
280
|
+
|
281
|
+
get("dynamic/imagerotation.php?imgkey=#{file.imagekey}&newrotation=#{ratation.to_s}&nocache=#{rand.to_s}")
|
282
|
+
return true
|
283
|
+
end
|
284
|
+
|
285
|
+
def save_file_to_my_account(quickkey)
|
286
|
+
raise NeedLogin unless is_loggedin?
|
287
|
+
|
288
|
+
response = get("dynamic/savefile.php?qk=#{quickkey}&status=statusmessage")
|
289
|
+
if response.body.match(/et ?= ?15;/)
|
290
|
+
true
|
291
|
+
else
|
292
|
+
false
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
296
|
+
def dropbox_setup(folder, status)
|
297
|
+
raise NeedLogin unless is_loggedin?
|
298
|
+
|
299
|
+
options = {
|
300
|
+
:dbx_status_enable => status ? 1 : 0,
|
301
|
+
:dbx_silent_return => 0,
|
302
|
+
:dbx_status_id => folder.folder_quickkey,
|
303
|
+
}
|
304
|
+
response = post('dropbox/dosetupdropbox.php', options)
|
305
|
+
if response.body.match(/et ?= ?15[012];/)
|
306
|
+
true
|
307
|
+
else
|
308
|
+
false
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
# return Dropbox ID
|
313
|
+
def dropbox_setup_option(folder, options)
|
314
|
+
raise NeedLogin unless is_loggedin?
|
315
|
+
|
316
|
+
response = post('dropbox/dosetupdropbox.php', {:dbx_folderid => folder.folder_quickkey})
|
317
|
+
folder_options = eval("[#{response.body.match(/parent.Bl\((.*?)\);break;/)[1]}]")
|
318
|
+
|
319
|
+
allow_add_description = options[:allow_add_description] ? 1 : 0
|
320
|
+
multiple_upload = options[:multiple_upload] ? 1 : 0
|
321
|
+
email_notification = options[:email_notification] ? 1 : 0
|
322
|
+
form_options = {
|
323
|
+
:dbx_folder => folder.folder_quickkey,
|
324
|
+
:dbx_text1 => options[:header] || folder_options[2],
|
325
|
+
:dbx_text2 => options[:message] || folder_options[3],
|
326
|
+
:dbx_text3 => options[:completion_message] || folder_options[4],
|
327
|
+
:dbx_color1 => options[:background_color] || folder_options[5],
|
328
|
+
:dbx_color2 => options[:header_background_color] || folder_options[6],
|
329
|
+
:dbx_color3 => options[:header_text_color] || folder_options[7],
|
330
|
+
:dbx_color4 => options[:text_color] || folder_options[8],
|
331
|
+
:dbx_color5 => options[:link_color] || folder_options[9],
|
332
|
+
:dbx_sharing => nil,
|
333
|
+
:dbx_descrip => allow_add_description || folder_options[10],
|
334
|
+
:dbx_multi => multiple_upload || folder_options[11],
|
335
|
+
:dbx_width => options[:width] || folder_options[12],
|
336
|
+
:dbx_email => email_notification || folder_options[13],
|
337
|
+
}
|
338
|
+
response = post('dropbox/dosetupdropbox.php', form_options)
|
339
|
+
if response.body.match(/et ?= ?200;/)
|
340
|
+
folder_options[0]
|
341
|
+
else
|
342
|
+
nil
|
343
|
+
end
|
344
|
+
end
|
345
|
+
|
346
|
+
def dropbox_upload(filepath, dropbox_id)
|
347
|
+
keys = {}
|
348
|
+
filename = File.basename(filepath)
|
349
|
+
response = get("dropbox/dropboxcfg.php?key=#{dropbox_id}")
|
350
|
+
doc = Nokogiri::XML(response.body)
|
351
|
+
doc.xpath('//mediafire/config').each do |c|
|
352
|
+
keys[:user] = c.xpath('./user').text
|
353
|
+
keys[:ukey] = c.xpath('./ukey').text
|
354
|
+
keys[:upload_session] = c.xpath('./upload_session').text
|
355
|
+
keys[:trackkey] = c.xpath('./trackkey').text
|
356
|
+
end
|
357
|
+
|
358
|
+
query = "type=basic"
|
359
|
+
query << "&track=#{keys[:trackkey]}&dropbox=1"
|
360
|
+
query << "&ukey=#{keys[:ukey]}&user=#{keys[:user]}"
|
361
|
+
query << "&uploadkey=#{dropbox_id}&upload=0"
|
362
|
+
response = File.open(filepath) do |f|
|
363
|
+
options = {:Filedata => UploadIO.new(f, 'application/octet-stream', filename)}
|
364
|
+
post("douploadtoapi?#{query}", options)
|
365
|
+
end
|
366
|
+
doc = Nokogiri::XML(response.body)
|
367
|
+
keys[:filekey] = doc.xpath('//response/doupload/key').text
|
368
|
+
|
369
|
+
if keys[:filekey] == ''
|
370
|
+
puts "Error: upload failed."
|
371
|
+
return nil
|
372
|
+
end
|
373
|
+
|
374
|
+
keys[:quickkey] = nil
|
375
|
+
query = "key=#{keys[:filekey]}&dropbox=#{dropbox_id}"
|
376
|
+
while keys[:quickkey] == nil || keys[:quickkey] == ''
|
377
|
+
response = get("dropbox/pollupload.php?#{query}")
|
378
|
+
doc = Nokogiri::XML(response.body)
|
379
|
+
keys[:quickkey] = doc.xpath('//response/doupload/quickkey').text
|
380
|
+
keys[:size] = doc.xpath('//response/doupload/size').text
|
381
|
+
sleep 0.5
|
382
|
+
end
|
383
|
+
|
384
|
+
puts "#{ENDPOINT}?#{keys[:quickkey]}"
|
385
|
+
end
|
386
|
+
|
387
|
+
def is_loggedin?
|
388
|
+
@loggedin
|
389
|
+
end
|
390
|
+
|
391
|
+
private
|
392
|
+
|
393
|
+
def create_list(files)
|
394
|
+
f = files
|
395
|
+
f = [f] unless f.is_a?(Array)
|
396
|
+
f.map {|n| "#{n.objecttype}:#{n.quickkey}"}.join(';')
|
397
|
+
end
|
398
|
+
|
399
|
+
def myfiles_path
|
400
|
+
response = get('myfiles.php')
|
401
|
+
'' + /js\/myfiles.php.[^"]*/.match(response.body)[0]
|
402
|
+
end
|
403
|
+
|
404
|
+
def filesize_to_readable_filesize(size)
|
405
|
+
b = 10 ** 2
|
406
|
+
s = size
|
407
|
+
return [s.to_s, "B"] if s / 1024 == 0
|
408
|
+
s = ((s / 1024.0) * b).ceil / b
|
409
|
+
return [s.to_s, "KB"] if s / 1024 == 0
|
410
|
+
s = ((s / 1024.0) * b).ceil / b
|
411
|
+
return [s.to_s, "MB"] if s / 1024 == 0
|
412
|
+
s = ((s / 1024.0) * b).ceil / b
|
413
|
+
return [s.to_s, "GB"]
|
414
|
+
end
|
415
|
+
end
|
416
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/http/post/multipart'
|
4
|
+
|
5
|
+
Net::HTTP.version_1_1
|
6
|
+
|
7
|
+
module Mediafire
|
8
|
+
module Connection
|
9
|
+
ENDPOINT = 'http://www.mediafire.com/'
|
10
|
+
|
11
|
+
def get(path, options={})
|
12
|
+
request(:get, path, options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def post(path, options={})
|
16
|
+
request(:post, path, options)
|
17
|
+
end
|
18
|
+
|
19
|
+
def request(method, path, options)
|
20
|
+
uri = URI.parse("#{ENDPOINT}#{path}")
|
21
|
+
|
22
|
+
request = nil
|
23
|
+
if method == :get
|
24
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
25
|
+
elsif method == :post
|
26
|
+
if has_multipart? options
|
27
|
+
request = Net::HTTP::Post::Multipart.new(uri.request_uri, options)
|
28
|
+
else
|
29
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
30
|
+
request.set_form_data(options)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
request['Cookie'] = cookie
|
34
|
+
|
35
|
+
response = Net::HTTP.start(uri.host, uri.port) do |http|
|
36
|
+
http.request(request)
|
37
|
+
end
|
38
|
+
build_cookie(response.get_fields('Set-Cookie'))
|
39
|
+
|
40
|
+
return response
|
41
|
+
end
|
42
|
+
|
43
|
+
def has_multipart?(options)
|
44
|
+
options.values.each do |v|
|
45
|
+
if v.is_a? UploadIO
|
46
|
+
return true
|
47
|
+
end
|
48
|
+
end
|
49
|
+
false
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def check_statuscode(response)
|
55
|
+
case response.code
|
56
|
+
when 400
|
57
|
+
raise BadRequest
|
58
|
+
when 401
|
59
|
+
raise Unauthorized
|
60
|
+
when 403
|
61
|
+
raise Forbidden
|
62
|
+
when 404
|
63
|
+
raise NotFound
|
64
|
+
when 406
|
65
|
+
raise NotAcceptable
|
66
|
+
when 408
|
67
|
+
raise RequestTimeout
|
68
|
+
when 500
|
69
|
+
raise InternalServerError
|
70
|
+
when 502
|
71
|
+
raise BadGateway
|
72
|
+
when 503
|
73
|
+
raise ServiceUnavailable
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def cookie
|
78
|
+
s = []
|
79
|
+
@cookie.each do |k,v|
|
80
|
+
s.push "#{k}=#{v}"
|
81
|
+
end
|
82
|
+
s.join(';')
|
83
|
+
end
|
84
|
+
|
85
|
+
def build_cookie(cookies)
|
86
|
+
cookies.each do |n|
|
87
|
+
c = n[0...n.index(';')].match(/(.*)=(.*)/)
|
88
|
+
@cookie[c[1]] = c[2] if c
|
89
|
+
end if cookies
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Mediafire
|
2
|
+
class Error < StandardError; end
|
3
|
+
|
4
|
+
# HTTP status code 400
|
5
|
+
class BadRequest < StandardError; end
|
6
|
+
# HTTP status code 401
|
7
|
+
class Unauthorized < StandardError; end
|
8
|
+
# HTTP status code 403
|
9
|
+
class Forbidden < StandardError; end
|
10
|
+
# HTTP status code 404
|
11
|
+
class NotFound < StandardError; end
|
12
|
+
# HTTP status code 406
|
13
|
+
class NotAcceptable < StandardError; end
|
14
|
+
# HTTP status code 408
|
15
|
+
class RequestTimeout < StandardError; end
|
16
|
+
# HTTP status code 500
|
17
|
+
class InternalServerError < StandardError; end
|
18
|
+
# HTTP status code 502
|
19
|
+
class BadGateway < StandardError; end
|
20
|
+
# HTTP status code 503
|
21
|
+
class ServiceUnavailable < StandardError; end
|
22
|
+
|
23
|
+
class NeedLogin < Error; end
|
24
|
+
end
|
@@ -0,0 +1,144 @@
|
|
1
|
+
module Mediafire
|
2
|
+
class StoreObject
|
3
|
+
L = {
|
4
|
+
# ??? => 0,
|
5
|
+
:objecttype => 1,
|
6
|
+
:filetype => 2,
|
7
|
+
:quickkey => 3,
|
8
|
+
:folderkey => 4,
|
9
|
+
:filename => 5,
|
10
|
+
:filesize => 6,
|
11
|
+
:readable_filesize => 7,
|
12
|
+
:readable_filesize_unit => 8,
|
13
|
+
:downloads => 9,
|
14
|
+
:upload_date => 10,
|
15
|
+
:access_type => 11,
|
16
|
+
:tags => 12,
|
17
|
+
:description => 13,
|
18
|
+
:password => 14,
|
19
|
+
:imagekey => 15, :folder_quickkey => 15,
|
20
|
+
:dropbox_enabled => 16,
|
21
|
+
:image_rotation => 17, :custom_url => 17,
|
22
|
+
# ??? => 0,
|
23
|
+
:note_subject => 19,
|
24
|
+
:note_description => 20,
|
25
|
+
}
|
26
|
+
|
27
|
+
def initialize(d)
|
28
|
+
data(d)
|
29
|
+
end
|
30
|
+
|
31
|
+
def data(d)
|
32
|
+
if d.is_a?(Array) && d.size == 21
|
33
|
+
@data = d
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def objecttype
|
38
|
+
@data[L[:objecttype]]
|
39
|
+
end
|
40
|
+
|
41
|
+
def is_folder?
|
42
|
+
if objecttype == '2'
|
43
|
+
return true
|
44
|
+
end
|
45
|
+
return false
|
46
|
+
end
|
47
|
+
|
48
|
+
def filetype
|
49
|
+
# 1 : picture
|
50
|
+
# 9 : compress file
|
51
|
+
# 0 : other
|
52
|
+
@data[L[:filetype]]
|
53
|
+
end
|
54
|
+
|
55
|
+
def is_picture?
|
56
|
+
if filetype == '1'
|
57
|
+
return true
|
58
|
+
end
|
59
|
+
return false
|
60
|
+
end
|
61
|
+
|
62
|
+
def quickkey
|
63
|
+
@data[L[:quickkey]]
|
64
|
+
end
|
65
|
+
|
66
|
+
def folderkey
|
67
|
+
@data[L[:folderkey]]
|
68
|
+
end
|
69
|
+
|
70
|
+
def name
|
71
|
+
@data[L[:filename]]
|
72
|
+
end
|
73
|
+
|
74
|
+
def size
|
75
|
+
"#{@data[L[:readable_filesize]]}#{@data[L[:readable_filesize_unit]]}"
|
76
|
+
end
|
77
|
+
|
78
|
+
def downloads
|
79
|
+
@data[L[:downloads]].to_i
|
80
|
+
end
|
81
|
+
|
82
|
+
def date
|
83
|
+
@data[L[:upload_date]]
|
84
|
+
end
|
85
|
+
|
86
|
+
def tags
|
87
|
+
@data[L[:tags]]
|
88
|
+
end
|
89
|
+
|
90
|
+
def description
|
91
|
+
@data[L[:description]]
|
92
|
+
end
|
93
|
+
|
94
|
+
def password
|
95
|
+
@data[L[:password]]
|
96
|
+
end
|
97
|
+
|
98
|
+
def imagekey
|
99
|
+
@data[L[:imagekey]]
|
100
|
+
end
|
101
|
+
|
102
|
+
def folder_quickkey
|
103
|
+
@data[L[:folder_quickkey]]
|
104
|
+
end
|
105
|
+
|
106
|
+
def dropbox_enabled
|
107
|
+
if @data[L[:dropbox_enabled]] == '1'
|
108
|
+
return true
|
109
|
+
end
|
110
|
+
return false
|
111
|
+
end
|
112
|
+
|
113
|
+
def access_type
|
114
|
+
@data[L[:access_type]]
|
115
|
+
end
|
116
|
+
|
117
|
+
def is_public?
|
118
|
+
if access_type == '0'
|
119
|
+
return true
|
120
|
+
end
|
121
|
+
return false
|
122
|
+
end
|
123
|
+
|
124
|
+
def image_rotation
|
125
|
+
# 0
|
126
|
+
# 90
|
127
|
+
# 180
|
128
|
+
# 270
|
129
|
+
@data[L[:image_rotation]]
|
130
|
+
end
|
131
|
+
|
132
|
+
def folder_quickkey
|
133
|
+
@data[L[:folder_quickkey]]
|
134
|
+
end
|
135
|
+
|
136
|
+
def note_subject
|
137
|
+
@data[L[:note_subject]]
|
138
|
+
end
|
139
|
+
|
140
|
+
def note_description
|
141
|
+
@data[L[:note_description]]
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
data/mediafire.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "mediafire/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "mediafire"
|
7
|
+
s.version = Mediafire::VERSION
|
8
|
+
s.authors = ["SKAhack"]
|
9
|
+
s.email = ["m@skahack.com"]
|
10
|
+
s.homepage = "https://github.com/SKAhack/mediafire"
|
11
|
+
s.summary = %q{Ruby wrapper for the unofficial Mediafire API}
|
12
|
+
s.description = %q{A Ruby wrapper for the unofficial Mediafire API}
|
13
|
+
|
14
|
+
s.rubyforge_project = "mediafire"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# s.add_development_dependency "rspec"
|
22
|
+
s.add_runtime_dependency "nokogiri"
|
23
|
+
s.add_runtime_dependency "multipart-post"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mediafire
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- SKAhack
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-01 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: nokogiri
|
16
|
+
requirement: &70296753195000 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70296753195000
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: multipart-post
|
27
|
+
requirement: &70296753194580 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70296753194580
|
36
|
+
description: A Ruby wrapper for the unofficial Mediafire API
|
37
|
+
email:
|
38
|
+
- m@skahack.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- Gemfile
|
45
|
+
- MIT_LICENSE
|
46
|
+
- README.md
|
47
|
+
- Rakefile
|
48
|
+
- examples/common.rb
|
49
|
+
- examples/create_folder.rb
|
50
|
+
- examples/delete_file.rb
|
51
|
+
- examples/download_image.rb
|
52
|
+
- examples/dropbox_setup.rb
|
53
|
+
- examples/dropbox_setup_option.rb
|
54
|
+
- examples/dropbox_upload.rb
|
55
|
+
- examples/edit_fileoption.rb
|
56
|
+
- examples/edit_note.rb
|
57
|
+
- examples/get_myfile_list.rb
|
58
|
+
- examples/image_rotation.rb
|
59
|
+
- examples/move_file.rb
|
60
|
+
- examples/save_file.rb
|
61
|
+
- examples/toggle_acl.rb
|
62
|
+
- examples/update.rb
|
63
|
+
- examples/upload_file.rb
|
64
|
+
- lib/mediafire.rb
|
65
|
+
- lib/mediafire/api.rb
|
66
|
+
- lib/mediafire/client.rb
|
67
|
+
- lib/mediafire/connection.rb
|
68
|
+
- lib/mediafire/error.rb
|
69
|
+
- lib/mediafire/store_object.rb
|
70
|
+
- lib/mediafire/version.rb
|
71
|
+
- mediafire.gemspec
|
72
|
+
homepage: https://github.com/SKAhack/mediafire
|
73
|
+
licenses: []
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project: mediafire
|
92
|
+
rubygems_version: 1.8.6
|
93
|
+
signing_key:
|
94
|
+
specification_version: 3
|
95
|
+
summary: Ruby wrapper for the unofficial Mediafire API
|
96
|
+
test_files: []
|