Class: PutioFs::PutioApi

Inherits:
Object
  • Object
show all
Includes:
FromHash
Defined in:
lib/putio_fs/putio_api.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



4
5
6
# File 'lib/putio_fs/putio_api.rb', line 4

def access_token
  @access_token
end

Class Method Details

.testObject



102
103
104
105
106
107
# File 'lib/putio_fs/putio_api.rb', line 102

def test
  user = User.first
  api = new(:access_token => user.access_token)
  require 'pp'
  pp api.files
end

Instance Method Details

#file_info(id) ⇒ Object



66
67
68
69
# File 'lib/putio_fs/putio_api.rb', line 66

def file_info(id)
  res = get "files/#{id}"
  res['file']
end

#files(root = 0) ⇒ Object



39
40
41
42
# File 'lib/putio_fs/putio_api.rb', line 39

def files(root=0)
  res = get "files/list", :parent_id => root
  res['files']
end

#get(sub, data = {}) ⇒ Object



16
17
18
19
# File 'lib/putio_fs/putio_api.rb', line 16

def get(sub,data={})
  json = open(url(sub,data)).read
  JSON.parse(json)
end

#movie_file_id(root) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/putio_fs/putio_api.rb', line 48

def movie_file_id(root)
  info = file_info(root)
  return root if info['content_type'] =~ /video/
  
  res = files(root)
  raise "no files found for #{root}" unless res
  #pp res
  
  cd1 = res.find { |x| x['content_type'] =~ /directory/ && x['name'].to_s.strip.downcase == 'cd1' }
  if cd1
    movie_file_id(cd1['id'])
  else
    res = res.reject { |x| x['content_type'] =~ /directory/ }.sort_by { |x| x['size'].to_i }[-1]
    raise "no movie file for #{root}" unless res
    res["id"]
  end
end

#mp4_url_for_folder(root) ⇒ Object



87
88
89
90
# File 'lib/putio_fs/putio_api.rb', line 87

def mp4_url_for_folder(root)
  movie = movie_file_id(root)
  mp4_url_raw(movie)
end

#mp4_url_raw(id) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/putio_fs/putio_api.rb', line 78

def mp4_url_raw(id)
  info = file_info(id)
  if info['content_type'] =~ /mp4/
    url("files/#{id}/download")
  else
    url("files/#{id}/mp4/download")
  end
end

#post(sub, data = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/putio_fs/putio_api.rb', line 21

def post(sub,data={})
  require 'net/http'
  
  uri = URI(url(sub))
  req = Net::HTTP::Post.new(uri.path)
  data['oauth_token'] = access_token
  req.set_form_data(data)
  
  body = nil
  res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http|
    body = http.request(req).body
  end
  puts "BODY #{body.inspect}"
  body = JSON.parse(body)
  puts "BODY2 #{body.class} #{body.inspect}"
  body
end

#search(keyword, ops = {}) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/putio_fs/putio_api.rb', line 92

def search(keyword,ops={})
  query = "\"#{keyword}\""
  query = CGI.escape(query)
  ops.each { |k,v| query += "%20#{k}:#{v}" }
  sub = "files/search/#{query}"
  res = get sub
  res['files']
end

#transfer_file_id(id) ⇒ Object



71
72
73
74
75
76
# File 'lib/putio_fs/putio_api.rb', line 71

def transfer_file_id(id)
  res = get "transfers/#{id}"
  puts res.inspect
  return nil unless res && res['transfer']
  res['transfer']['file_id']
end

#upload(file) ⇒ Object



44
45
46
# File 'lib/putio_fs/putio_api.rb', line 44

def upload(file)
  post "transfers/add", :url => file, :callback_url => "http://putbay.io/media_jobs/putio_callback"
end

#url(sub, data = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/putio_fs/putio_api.rb', line 6

def url(sub,data={})
  require 'open-uri'
  url = "https://api.put.io/v2/#{sub}?oauth_token=#{access_token}"
  data.each do |k,v|
    url += "&#{k}=#{v}"
  end
  puts "getting #{url}"
  url
end