Module: Mediafire::API

Includes:
Connection
Included in:
Client
Defined in:
lib/mediafire/api.rb

Constant Summary

Constants included from Connection

Connection::ENDPOINT

Instance Method Summary collapse

Methods included from Connection

#get, #has_multipart?, #post, #request, #upload_size

Instance Method Details

#create_folder(newname, folder = nil) ⇒ Object

Raises:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/mediafire/api.rb', line 115

def create_folder(newname, folder=nil)
  raise NeedLogin unless is_loggedin?

  options = base_query
  options << "foldername=#{newname}"
  options << "parent_key=#{folder.key}" unless folder.nil?

  response = get("api/folder/create.php?#{options.join('&')}")
  response = JSON.parse(response.body)['response']
  if response['result'] == 'Success'
    pick_object(response['folder_key'], 'folder', folder)
  else
    nil
  end
end

#delete(objs) ⇒ Object

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mediafire/api.rb', line 44

def delete(objs)
  raise NeedLogin unless is_loggedin?

  file_response = true
  folder_response = true

  obj = separate_store_object(objs)

  file_response = delete_files(obj[:files]) unless obj[:files].empty?
  folder_response = delete_folders(obj[:folders]) unless obj[:folders].empty?

  if file_response and folder_response
    true
  else
    false
  end
end

#description(file, desc) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mediafire/api.rb', line 92

def description(file, desc)
  options = {
    'filename' => file.name,
    'description' => desc,
  }
  if file_update(file, options)
    pick_object(file.key, 'file')
  else
    nil
  end
end

#download(file) ⇒ Object

def webupload(url, folder) end



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/mediafire/api.rb', line 205

def download(file)
  if file.is_a?(Array)
    return downloads(file)
  end

  if file.is_a?(StoreFile)
    response = get("?#{file.key}")
  else
    response = get("?#{file}")
  end
  if response.code.to_i == 302
    return response['Location']
  end

  body = response.body
  funcbody = body.scan(/function .*?for.*?eval/)
  functions = funcbody.map {|n| n.match(/function (.*?)\(/).to_a[1]}
  decrypt_str = funcbody.map {|n| find_encrypt_string_and_decrypt(n)}
  [body, funcbody, functions, decrypt_str]

  t = decrypt_str.last
  funcname = t.match(/(.*?)\(/)[1]
  params = t.scan(/'(.*?)'/)
  qk = params[0][0]
  pk1 = params[1][0]
  pkr = body.match(/pKr ?= ?'(.*?)';/)[1]

  t = decrypt_str[functions.index(funcname)]
  key = t.match(/getElementById\("(.*?)"\)/).to_a[1]

  response = get("dynamic/download.php?qk=#{qk}&pk1=#{pk1}&r=#{pkr}")

  body = response.body
  t = body.match(/var.*?function/).to_a[0]
  download_keys = find_encrypt_string_and_decrypt(t)
  t = body.match(/#{key}.*?("http.*?)>.*?if/)[1]
  i,j,k = t.scan(/"(.*?)".*?\+(.*?)\+.*?"(.*?)\\?"/)[0]
  d = download_keys.match(/#{j}='([\w\d]*)'/)[1]

  return "#{i}#{d}#{k}"
end

#download_image(file, size = 6) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/mediafire/api.rb', line 266

def download_image(file, size=6)
  unless file.is_picture?
    puts 'Error: file is not a picture.'
    return
  end
  if size < 0 && size > 6
    puts 'Error: size must be between 0 to 6.'
    return
  end

  "#{ENDPOINT}imgbnc.php/#{file.hash}#{size.to_s}g.jpg"
end

#downloads(files) ⇒ Object

Raises:



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/mediafire/api.rb', line 247

def downloads(files)
  raise NeedLogin unless is_loggedin?

  option = {
    :form_todolist1 => create_list(files),
    :form_todolist2 => '',
    :form_todotype => 3,
    :form_todoparent => '',
    :form_todoconfirm => 0,
  }
  response = post("dynamic/doselected.php", option)

  filename = response['Content-Disposition'].sub(/^.*?filename="(.*)"$/, '\1')
  puts "downloading: #{filename}"
  File.open("./#{filename}", 'w+b') do |f|
    f.write response.body
  end
end

#dropbox_setup(folder, status) ⇒ Object

Raises:



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/mediafire/api.rb', line 329

def dropbox_setup(folder, status)
  raise NeedLogin unless is_loggedin?

  options = {
    :dbx_status_enable => status ? 1 : 0,
    :dbx_silent_return => 0,
    :dbx_status_id => folder.key,
  }
  response = post('dropbox/dosetupdropbox.php', options)
  if response.body.match(/et ?= ?15[012];/)
    return true unless status
  else
    return false
  end

  if dropbox_setup_option(folder)
    return true
  else
    return false
  end
end

#dropbox_setup_option(folder, options = {}) ⇒ Object

return Dropbox ID

Raises:



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/mediafire/api.rb', line 352

def dropbox_setup_option(folder, options={})
  raise NeedLogin unless is_loggedin?

  response = post('dropbox/dosetupdropbox.php', {:dbx_folderid => folder.key})
  folder_options = eval("[#{response.body.match(/parent.Bl\((.*?)\);break;/)[1]}]")

  allow_add_description = options[:allow_add_description] ? 1 : 0
  multiple_upload = options[:multiple_upload] ? 1 : 0
  email_notification = options[:email_notification] ? 1 : 0
  form_options = {
    :dbx_folder => folder.key,
    :dbx_text1 => options[:header] || folder_options[2],
    :dbx_text2 => options[:message] || folder_options[3],
    :dbx_text3 => options[:completion_message] || folder_options[4],
    :dbx_color1 => options[:background_color] || folder_options[5],
    :dbx_color2 => options[:header_background_color] || folder_options[6],
    :dbx_color3 => options[:header_text_color] || folder_options[7],
    :dbx_color4 => options[:text_color] || folder_options[8],
    :dbx_color5 => options[:link_color] || folder_options[9],
    :dbx_sharing => nil,
    :dbx_descrip => allow_add_description || folder_options[10],
    :dbx_multi => multiple_upload || folder_options[11],
    :dbx_width => options[:width] || folder_options[12],
    :dbx_email => email_notification || folder_options[13],
  }
  response = post('dropbox/dosetupdropbox.php', form_options)
  if response.body.match(/et ?= ?200;/)
    folder_options[0]
  else
    nil
  end
end

#dropbox_upload(filepath, dropbox_id) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/mediafire/api.rb', line 385

def dropbox_upload(filepath, dropbox_id)
  keys = {}
  filename = File.basename(filepath)
  response = get("dropbox/dropboxcfg.php?key=#{dropbox_id}")
  doc = Nokogiri::XML(response.body)
  doc.xpath('//mediafire/config').each do |c|
    keys[:user] = c.xpath('./user').text
    keys[:ukey] = c.xpath('./ukey').text
    keys[:upload_session] = c.xpath('./upload_session').text
    keys[:trackkey] = c.xpath('./trackkey').text
  end

  query = []
  query << "type=basic"
  query << "track=#{keys[:trackkey]}"
  query << "dropbox=1"
  query << "ukey=#{keys[:ukey]}"
  query << "user=#{keys[:user]}"
  query << "uploadkey=#{dropbox_id}"
  query << "upload=0"
  response = File.open(filepath) do |f|
    options = {:Filedata => UploadIO.new(f, 'application/octet-stream', filename)}
    post("douploadtoapi?#{query.join('&')}", options)
  end
  doc = Nokogiri::XML(response.body)
  keys[:filekey] = doc.xpath('//response/doupload/key').text

  if keys[:filekey] == ''
    puts "Error: upload failed."
    return nil
  end

  keys[:quickkey] = nil
  query = "key=#{keys[:filekey]}&dropbox=#{dropbox_id}"
  while keys[:quickkey] == nil || keys[:quickkey] == ''
    response = get("dropbox/pollupload.php?#{query}")
    doc = Nokogiri::XML(response.body)
    keys[:quickkey] = doc.xpath('//response/doupload/quickkey').text
    keys[:size] = doc.xpath('//response/doupload/size').text
    sleep 0.5
  end

  puts "#{ENDPOINT}?#{keys[:quickkey]}"
end

#file_listObject



40
41
42
# File 'lib/mediafire/api.rb', line 40

def file_list
  list('recursive' => true, 'filter' => 'files').files
end

#folder_listObject



36
37
38
# File 'lib/mediafire/api.rb', line 36

def folder_list
  list('recursive' => true, 'filter' => 'folders').folders
end

#image_rotation(file, rotation) ⇒ Object

Raises:



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/mediafire/api.rb', line 279

def image_rotation(file, rotation)
  raise NeedLogin unless is_loggedin?

  unless file.is_picture?
    puts "Error: file is not a picture."
    return
  end

  unless rotation == 0 || rotation == 90 || rotation == 180 || rotation == 270
    puts "Error: rotation need 0 or 90 or 180 or 270"
    return
  end

  query = []
  query << "imgkey=#{file.hash}"
  query << "newrotation=#{rotation.to_s}"
  query << "nocache=#{rand.to_s}"

  get("dynamic/imagerotation.php?#{query.join('&')}")
  return true
end

#is_loggedin?Boolean

Returns:

  • (Boolean)


440
441
442
# File 'lib/mediafire/api.rb', line 440

def is_loggedin?
  @loggedin
end

#list(options = {}) ⇒ Object

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mediafire/api.rb', line 21

def list(options={})
  raise NeedLogin unless is_loggedin?

  folder    = options['folder']
  filter    = options['filter']
  recursive = options['recursive'] || true

  if folder.nil?
    response = myfiles(recursive, filter)
  else
    response = info(folder.key, recursive)
  end
  StoreFolder.new(response)
end

#login(account, password) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mediafire/api.rb', line 5

def (, password)
  @cookie = {}
  toppage
  unless is_loggedin?
    options = {
      :login_email => ,
      :login_pass => password,
      :login_remember => 'on',
    }
    post("dynamic/login.php", options)
  end
  if @cookie.key?('user')
    @loggedin = true
  end
end

#move(src, dest) ⇒ Object

Raises:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mediafire/api.rb', line 62

def move(src, dest)
  raise NeedLogin unless is_loggedin?

  file_response = true
  folder_response = true

  obj = separate_store_object(src)

  file_response = move_files(obj[:files], dest) unless obj[:files].empty?
  folder_response = move_folders(obj[:folders], dest) unless obj[:folders].empty?

  if file_response and folder_response
    true
  else
    false
  end
end

#password(file, pass) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/mediafire/api.rb', line 104

def password(file, pass)
  options = {
    'password' => pass,
  }
  if file_update(file, options)
    pick_object(file.key, 'file')
  else
    nil
  end
end

#rename(file, name) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mediafire/api.rb', line 80

def rename(file, name)
  options = {
    'filename' => name,
    'description' => file.description,
  }
  if file_update(file, options)
    pick_object(file.key, 'file')
  else
    nil
  end
end

#revision(folder_key = "myfiles", recursive = false) ⇒ Object



430
431
432
433
434
435
436
437
438
# File 'lib/mediafire/api.rb', line 430

def revision(folder_key="myfiles", recursive=false)
  query = base_query
  query << "r=#{r(4)}"
  query << "recursive=#{recursive ? 'yes' : 'no'}"
  query << "folder_key=#{folder_key}"

  response = get("api/folder/get_revision.php?#{query.join('&')}").body
  JSON.parse(response)['response']['revision']
end

#save_file_to_my_account(quickkey) ⇒ Object

Raises:



318
319
320
321
322
323
324
325
326
327
# File 'lib/mediafire/api.rb', line 318

def (quickkey)
  raise NeedLogin unless is_loggedin?

  response = get("dynamic/savefile.php?qk=#{quickkey}")
  if response.body.match(/et ?= ?15;/)
    true
  else
    false
  end
end

#toggle_acl(file, recursive = false) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/mediafire/api.rb', line 131

def toggle_acl(file, recursive=false)
  options = {}
  if file.is_public?
    options['privacy'] = 'private'
  else
    options['privacy'] = 'public'
  end
  options['privacy_recursive'] = 'yes' if recursive

  if file_update(file, options)
    pick_object(file.key, 'file')
  else
    nil
  end
end

#update(dest, src) ⇒ Object

Raises:



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/mediafire/api.rb', line 301

def update(dest, src)
  raise NeedLogin unless is_loggedin?

  query = base_query
  query << "r=#{r(4)}"
  query << "from_quickkey=#{src.key}"
  query << "to_quickkey=#{dest.key}"
  query << "quickkey=#{src.key}"

  response = get("api/file/update_file.php?#{query.join('&')}")
  if JSON.parse(response.body)['response']['result'] == 'Success'
    true
  else
    false
  end
end

#upload(filepath, folder = nil) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/mediafire/api.rb', line 147

def upload(filepath, folder=nil)
  filename = File.basename(filepath)
  keys = {}
  response = get('basicapi/uploaderconfiguration.php')
  doc = Nokogiri::XML(response.body)
  doc.xpath('//mediafire/config').each do |c|
    keys[:user] = c.xpath('./user').text
    keys[:ukey] = c.xpath('./ukey').text
    keys[:upload_session] = c.xpath('./upload_session').text
    keys[:trackkey] = c.xpath('./trackkey').text
    if folder.nil?
      keys[:folderkey] = c.xpath('./folderkey').text
    else
      keys[:folderkey] = folder.key
    end
  end
  keys[:mful_config] = doc.xpath('//mediafire/MFULConfig').text

  query = []
  query << "type=basic"
  query << "ukey=#{keys[:ukey]}"
  query << "user=#{keys[:user]}"
  query << "uploadkey=#{keys[:folderkey]}"
  query << "filenum=0"
  query << "uploader=0"
  query << "MFULConfig=#{keys[:mful_config]}"
  response = File.open(filepath) do |f|
    options = {:Filedata => UploadIO.new(f, 'application/octet-stream', filename)}
    post("douploadtoapi?#{query.join('&')}", options)
  end
  doc = Nokogiri::XML(response.body)
  keys[:filekey] = doc.xpath('//response/doupload/key').text

  if keys[:filekey] == ''
    puts "Error: upload failed."
    return nil
  end

  keys[:quickkey] = nil

  query = []
  query << "key=#{keys[:filekey]}"
  query << "MFULConfig=#{keys[:mful_config]}"
  while keys[:quickkey] == nil || keys[:quickkey] == ''
    response = get("basicapi/pollupload.php?#{query.join('&')}")
    doc = Nokogiri::XML(response.body)
    keys[:quickkey] = doc.xpath('//response/doupload/quickkey').text
    keys[:size] = doc.xpath('//response/doupload/size').text
    sleep 0.5
  end

  pick_object(keys[:quickkey], 'file', folder)
end