baidu_pcs 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 50eb3bbcf619e3a9b14a76c99b720cdd8bee0cb6
4
+ data.tar.gz: 5a52cda6a81d58fd8c6b795c72a7fb1b9e59def0
5
+ SHA512:
6
+ metadata.gz: 54150bb6958a6448b4feaed7cb6d1dd792e25e22ae126cdd252fa584250e8a77b122e32ee45ac537d9d596c6430f80e44f97601a1855765f847e2404b6a965d0
7
+ data.tar.gz: 7a718b040b87a07ef333b4f40729a0713b54ee1a9e079534e0fa5915f0999c700a0df8660295484cde4ebcc843724b92ffb2a8ced5ec92143f749e7ac8f46dd2
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ .ruby-version*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ #source 'https://rubygems.org'
2
+ source "http://ruby.taobao.org"
3
+
4
+ # Specify your gem's dependencies in baidu_pcs.gemspec
5
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 cao7113
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Note.md ADDED
@@ -0,0 +1,3 @@
1
+ important for develop a gem:
2
+ * bundle exec bin/bpcs -h
3
+ * bundle console
@@ -0,0 +1,66 @@
1
+ # BaiduPcs
2
+
3
+ Try baidu pcs using ruby!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'baidu_pcs'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install baidu_pcs
18
+
19
+ ## Usage
20
+
21
+ WARNING: in development, just try this, 另一个C语言实现的[命令行工具](https://github.com/emptyhua/baidu_pcs_cli)
22
+
23
+ Steps:
24
+
25
+ * create a [baidu PCS app](http://developer.baidu.com/ms/pcs) and enable pcs api permissions!
26
+ * `baidu_pcs setup _app_name _api_key _secret_key`
27
+ * `baidu_pcs config` to get an access token
28
+ * get help by `baidupcs --help` at any time
29
+ * another bins: `baidupcs_db, baidupcs_local`
30
+
31
+ List remote files:
32
+
33
+ ```
34
+ cao@tj-desktop:~/dev/baidu_pcs$ baidupcs ls
35
+ % Total % Received % Xferd Average Speed Time Time Time Current
36
+ Dload Upload Total Spent Left Speed
37
+ 100 1291 100 1291 0 0 3721 0 --:--:-- --:--:-- --:--:-- 4332
38
+ 627065058 books/ 0 2013-09-10 15:29:08 +0800
39
+ 3837173268 img/ 0 2013-09-10 15:45:35 +0800
40
+ 2118772017 personal/ 0 2013-09-10 15:31:12 +0800
41
+ 1827382386 pkg/ 0 2013-09-10 15:32:36 +0800
42
+ 3885218819 dns.jpg 28253 2013-09-13 08:41:59 +0800
43
+ 2730525873 Gemfile 127 2013-09-11 16:29:39 +0800
44
+ 3974115712 head.jpg 17888 2013-09-11 08:31:41 +0800
45
+ 2311496999 MongoDB-manual.pdf 8735748 2013-09-11 18:55:55 +0800
46
+ 1319250694 Note.md 71 2013-09-11 18:11:46 +0800
47
+ ```
48
+
49
+ ## TODO
50
+
51
+ * 文件夹批量索引和同步问题
52
+ * 分享文件列表
53
+ * 应用拓广
54
+
55
+ ## Test and Running
56
+
57
+ * platform: ubuntu
58
+ * ruby: 1.9.3, 2.0.0
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'baidu_pcs/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "baidu_pcs"
8
+ spec.version = BaiduPcs::VERSION
9
+ spec.authors = ["cao7113"]
10
+ spec.email = ["cao7113@hotmail.com"]
11
+ spec.description = %q{Baidu Pcs: personal cloud service}
12
+ spec.summary = spec.description
13
+ spec.homepage = "http://shareup.me"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "pry-nav"
26
+ spec.add_runtime_dependency "typhoeus", "~> 0.6.5"
27
+ spec.add_runtime_dependency "multi_json"
28
+ spec.add_runtime_dependency "thor" #slop
29
+ end
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'baidu_pcs/cli/fs_cli'
3
+ BaiduPcs::Cli::FsCli.start
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'baidu_pcs/cli/db_cli'
3
+ BaiduPcs::Cli::DbCli.start
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'baidu_pcs/cli/local_cli'
3
+ BaiduPcs::Cli::LocalCli.start
@@ -0,0 +1,110 @@
1
+ require 'typhoeus'
2
+ require 'multi_json'
3
+ require 'fileutils'
4
+ require 'yaml'
5
+ require "baidu_pcs/version"
6
+ require "baidu_pcs/util"
7
+
8
+ module BaiduPcs
9
+
10
+ PCS_BASE_URL = "https://pcs.baidu.com/rest/2.0/pcs"
11
+ CONFIG_FILE = File.expand_path(ENV['_BAIDU_PCS_CONFIG_FILE']||"~/.baidu_pcs_config.yml")
12
+
13
+ class Config
14
+ attr_reader :config
15
+ class << self
16
+ def instance
17
+ @_instance ||= new
18
+ end
19
+ def method_missing(method, *args)
20
+ instance.config[method.to_sym] #use like: Config.app_name --> :app_name in config file
21
+ end
22
+ end
23
+ private
24
+ def initialize
25
+ raise "Not found file: #{CONFIG_FILE}, Please run config and setup firstly!" unless File.exists?(CONFIG_FILE)
26
+ @config = YAML.load_file(CONFIG_FILE)
27
+ end
28
+ end
29
+
30
+ class Base
31
+ attr_accessor :request, :options, :response, :body
32
+
33
+ def initialize(url, method=:get, params={}, body={}, opts={})
34
+ [:noprogress, :verbose].each do |k|
35
+ opts[k] = params.delete(k) if params.key?(k) and !opts.key?(k)
36
+ end
37
+ @options = {
38
+ method: method||:get,
39
+ headers: {"User-Agent"=>"Mozilla/5.0 (X11; Linux x86_64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"},
40
+ params: params
41
+ }
42
+ if headers = opts.delete(:headers)
43
+ @options[:headers].merge!(headers)
44
+ end
45
+ @options.merge!(body: body) unless body.blank?
46
+ @options.merge!(opts)
47
+ @request = Typhoeus::Request.new(url, @options)
48
+ if @options[:verbose]
49
+ puts "#### request options: "
50
+ puts @options
51
+ end
52
+ @request.on_complete do |response|
53
+ @response = response
54
+ if response.success? #(mock || return_code == :ok) && response_code && response_code >= 200 && response_code < 300
55
+ if response.headers["Content-Disposition"] =~ /attachment;file/ or response.headers["Content-Type"] =~ /image\//
56
+ @body = response.body
57
+ else #default as json
58
+ @body = MultiJson.load(response.body, symbolize_keys: true)
59
+ end
60
+ elsif response.timed_out?
61
+ raise "Timeout with options: #{options}"
62
+ elsif response.code == 0
63
+ raise "Could not get an http response, something's wrong: #{response.return_message} with options: #{options}"
64
+ else
65
+ raise "Http request failed with code: #{response.code}, msg: #{response.body}"
66
+ end
67
+ end
68
+ self
69
+ end
70
+
71
+ def run!
72
+ @request.run
73
+ self
74
+ end
75
+ def ok?
76
+ response.success?
77
+ end
78
+ def http_code
79
+ response.code
80
+ end
81
+
82
+ class << self
83
+ def get(url, params={}, opts={})
84
+ new(url, :get, params, nil, opts).run!
85
+ end
86
+ def post(url, params={}, body={}, opts={})
87
+ new(url, :post, params, body, opts).run!
88
+ end
89
+ def put(url, params={}, body={}, opts={})
90
+ new(url, :put, params, body, opts).run!
91
+ end
92
+ def delete(url, params={}, opts={})
93
+ new(url, :delete, params, opts).run!
94
+ end
95
+
96
+ #common
97
+ def atoken_params
98
+ {access_token: Config.access_token}
99
+ end
100
+ def method_params(buz_method=nil, params={})
101
+ opts = atoken_params.merge(params)
102
+ opts.merge!(method: buz_method) if buz_method
103
+ opts
104
+ end
105
+ def quota
106
+ get("#{PCS_BASE_URL}/quota", method_params(:info))
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,55 @@
1
+ require 'baidu_pcs'
2
+ require 'thor'
3
+ require 'baidu_pcs/cli/thor_ext'
4
+ module BaiduPcs::Cli
5
+ class BaseCli < Thor
6
+ include Thor::Actions
7
+
8
+ class_option :noprogress, type: :boolean, default: false, desc: "display CURL progress-bar when interacting with remote resources"
9
+ class_option :verbose, type: :boolean, default: false, desc: "display verbose info for tracking"
10
+
11
+ desc 'setup APP_NAME, API_KEY, SECRET_KEY [, LOCAL_APP_ROOT]', 'setup app settings'
12
+ def setup(app_name, api_key, secret_key, local_app_root=nil)
13
+ local_app_root ||= File.expand_path("~/baidu/#{app_name}")
14
+ require 'erb'
15
+ content = (ERB.new <<-EOF).result(binding)
16
+ :app_name: <%=app_name||'<_app_name>'%>
17
+ :app_root: /apps/<%=app_name||'<_app_name_or_you_set_in_baidu>'%>
18
+ :api_key: <%=api_key||'<_api_key>'%>
19
+ :secret_key: <%=secret_key||'<_secret_key>'%>
20
+ :local_app_root: <%=local_app_root||'<_loal_app_root>'%>
21
+ EOF
22
+ config_path = BaiduPcs::CONFIG_FILE
23
+ File.write(config_path, content)
24
+ say "Has wrote #{config_path} for app settings."
25
+ end
26
+
27
+ desc 'config', 'config your access token'
28
+ def config
29
+ url = "https://openapi.baidu.com/oauth/2.0/authorize?response_type=token&client_id=#{BaiduPcs::Config.api_key}&redirect_uri=oob&scope=netdisk"
30
+ #res = BaiduPcs::Base.get(url, nil, followlocation: true) #
31
+ #页面可能有要执行的js代码和要用户授权操作, 不能完全程序自动执行
32
+ say "请在浏览器中完成授权操作并获取最终成功url!"
33
+ if system("which xdg-open")
34
+ cmd = "xdg-open '#{url}'"
35
+ say "running command: #{cmd}"
36
+ `#{cmd}`
37
+ else
38
+ say "将下面的链接粘入浏览器获取access_token"
39
+ say url
40
+ end
41
+ say "将浏览器的url输入到这里:"
42
+ atoken = STDIN.gets.chomp
43
+ atoken =~ /access_token=([^&]*)/
44
+ atoken = $1 if $1
45
+ raise "Invalid token: #{atoken}!" if atoken !~ /^[\da-f\.\-]*$/
46
+ File.open(BaiduPcs::CONFIG_FILE, "a"){|f| f.puts ":access_token: #{atoken}" }
47
+ say "Have append access token into file: #{BaiduPcs::CONFIG_FILE}"
48
+ end
49
+
50
+ desc 'quota', 'quota space for storage'
51
+ def quota
52
+ say BaiduPcs::Base.quota.body
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,17 @@
1
+ require 'baidu_pcs/cli/base_cli'
2
+ require 'baidu_pcs/db'
3
+
4
+ module BaiduPcs::Cli
5
+ class DbCli < BaseCli
6
+
7
+ desc 'create_table TBL_NAME', ' create a table'
8
+ def create_table(tbl_name)
9
+ say BaiduPcs::Db.create_tbl(tbl_name)
10
+ end
11
+
12
+ desc 'desc_table TBL_NAME', ' create a table'
13
+ def desc_table(tbl_name)
14
+ say BaiduPcs::Db.describe_table(tbl_name)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,100 @@
1
+ require 'baidu_pcs/cli/base_cli'
2
+ require 'baidu_pcs/fs'
3
+
4
+ module BaiduPcs::Cli
5
+ class FsCli < BaseCli
6
+ default_task :list
7
+
8
+ no_tasks do
9
+ def print_item(item)
10
+ print_in_columns [item[:fs_id], "#{item[:path].sub(BaiduPcs::Config.app_root+'/', '')}#{'/' if item[:isdir]==1}", item[:size], "#{Time.at(item[:mtime])}"]
11
+ end
12
+ end
13
+
14
+ desc 'upload LOCAL_PATH [, RPATH]', 'upload a local file /path/to/file --> /apps/appname/[rpath|file]'
15
+ option :ondup, type: :string, desc: <<-Desc, default: :newcopy
16
+ overwrite:表示覆盖同名文件;newcopy:表示生成文件副本并进行重命名,命名规则为“文件名_日期.后缀”。
17
+ Desc
18
+ def upload(local_path, rpath=nil)
19
+ res = BaiduPcs::Fs.upload(File.expand_path(local_path), rpath, options.dup)
20
+ print_item res.body
21
+ end
22
+
23
+ desc 'download RPATH', 'download a remote file, DONOT!!! support download a dir'
24
+ option :tosync, desc: "是否下载到本地同步目录!", type: :boolean, default: false
25
+ def download(rpath)
26
+ opts = options.dup
27
+ local_app_root = opts.delete(:tosync) ? BaiduPcs::Config.local_app_root : Dir.pwd
28
+ local_path = "#{local_app_root}/#{rpath}"
29
+ body = BaiduPcs::Fs.download(rpath, opts).body
30
+ FileUtils.mkdir_p(File.dirname(local_path))
31
+ if File.exists?(local_path)
32
+ extname = File.extname(local_path)
33
+ timestamp_ext = ".#{Time.now.strftime('%Y%m%d%H%M%S')}-#{rand(100)}#{extname}"
34
+ if extname.blank?
35
+ local_path += timestamp_ext
36
+ else
37
+ local_path.gsub!(extname, timestamp_ext)
38
+ end
39
+ end
40
+ File.binwrite(local_path, body)
41
+ say local_path
42
+ end
43
+
44
+ desc 'streamurl RPATH', 'get a stream-file url for using online, e.g. <img src="_streamurl" />'
45
+ def streamurl(rpath)
46
+ say BaiduPcs::Fs.streamurl(rpath)
47
+ end
48
+ map url: :streamurl
49
+
50
+ desc 'mkdir RPATH', 'mkdir remote path, e.g. mkdir path/to/newdir, support b/c1/d2'
51
+ def mkdir(rpath)
52
+ say BaiduPcs::Fs.mkdir(rpath).body
53
+ end
54
+
55
+ desc 'meta RPATH', 'get meta info about a remote path, file or directory'
56
+ def meta(rpath)
57
+ say BaiduPcs::Fs.meta(rpath).body
58
+ end
59
+
60
+ desc 'list [RPATH]', 'list a remote path for all file or directory'
61
+ option :by, desc: "sort field, possible values: [time | name | size]", type: :string
62
+ option :order, desc: "possible value: [asc | desc]", type: :string#, default: :desc
63
+ option :limit, desc: "n1-n2, default n1=0", type: :string
64
+ option :progress, desc: "progress bar control", type: :boolean, default: true
65
+ def list(rpath=nil)
66
+ res = BaiduPcs::Fs.list(rpath, options.dup)
67
+ res.body[:list].each{|item| print_item(item) }
68
+ end
69
+ map ls: :list
70
+
71
+ desc 'move FROM_RPATH, TO_RPATH', 'move a remote path/to/from --> path/to/to'
72
+ def move(from_rpath, to_rpath)
73
+ say BaiduPcs::Fs.move(from_rpath, to_rpath).body
74
+ end
75
+ map mv: :move
76
+
77
+ desc 'copy FROM_RPATH, TO_RPATH', 'copy a remote path/to/from --> path/to/to'
78
+ def copy(from_rpath, to_rpath)
79
+ say BaiduPcs::Fs.copy(from_rpath, to_rpath).body
80
+ end
81
+ map cp: :copy
82
+
83
+ desc 'delete RPATH', 'delete a remote path'
84
+ option :force, type: :boolean, aliases: :f, default: false
85
+ def delete(rpath)
86
+ if options[:force] || yes?("Are you sure to delte #{rpath}?")
87
+ say BaiduPcs::Fs.delete(rpath).body
88
+ else
89
+ say "Cancel to delete #{rpath}"
90
+ end
91
+ end
92
+ map del: :delete
93
+
94
+ desc 'search KEYWORD [, RPATH]', 'search a keyword in remote path'
95
+ option :recursive, type: :boolean, aliases: :r, default: false
96
+ def search(keyword, rpath=nil)
97
+ say BaiduPcs::Fs.search(keyword, rpath, options).body
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,14 @@
1
+ require 'baidu_pcs/cli/base_cli'
2
+ module BaiduPcs::Cli
3
+ class LocalCli < BaseCli
4
+ desc 'home', 'display local app root'
5
+ def home
6
+ say BaiduPcs::Config.local_app_root
7
+ end
8
+
9
+ desc 'list [PATH]', 'list local app path'
10
+ def list(path=nil)
11
+ say `ls -l #{BaiduPcs::Config.local_app_root}/#{path}`
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ class Thor::CoreExt::HashWithIndifferentAccess
2
+ protected
3
+ #prefered symbol key vs string key
4
+ def convert_key(key)
5
+ key.is_a?(Symbol) ? key : key.to_sym
6
+ end
7
+ end
@@ -0,0 +1,57 @@
1
+ require 'baidu_pcs'
2
+ module BaiduPcs
3
+ ######################################################
4
+ # REST structured-data Apis
5
+ #http://developer.baidu.com/wiki/index.php?title=docs/pcs/rest/structured_data_apis_list
6
+
7
+ class Db < Base
8
+ DB_BASE_URL = "#{PCS_BASE_URL}/structure"
9
+ TABLE_BASE_URL = "#{DB_BASE_URL}/table"
10
+ DATA_BASE_URL = "#{DB_BASE_URL}/data"
11
+
12
+ def self.create_table(tbl_name, opts={})
13
+ params = method_params(:create, v: "1.0", sk: Config.secret_key, table: tbl_name)
14
+ post(TABLE_BASE_URL, params)
15
+ end
16
+
17
+ def self.describe_table(tbl_name)
18
+ params = method_params(:describe, v: "1.0", table: tbl_name}
19
+ post(TABLE_BASE_URL, params)
20
+ end
21
+
22
+ def self.alter_table(tbl_name, opts={})
23
+ params = method_params(:alter, v: "1.0", table: tbl_name)
24
+ post(TABLE_BASE_URL, params)
25
+ end
26
+
27
+ def self.drop_table(tbl_name, opts={})
28
+ params = method_params(:drop, v: "1.0", table: tbl_name)
29
+ post(TABLE_BASE_URL, params)
30
+ end
31
+
32
+ def self.restore_table(tbl_name)
33
+ params = method_params(:restore, v: "1.0", table: tbl_name)
34
+ post(TABLE_BASE_URL, params)
35
+ end
36
+
37
+ def self.insert_record(tbl_name, records)
38
+ params = method_params(:insert, v: "1.0", table: tbl_name, records: records)
39
+ post(DATA_BASE_URL, params)
40
+ end
41
+
42
+ def self.update_record(tbl_name, records, opts={})
43
+ params = method_params(:update, v: "1.0", table: tbl_name, records: records)
44
+ post(DATA_BASE_URL, params)
45
+ end
46
+
47
+ def self.delete_record(tbl_name, records, opts={})
48
+ params = method_params(:delete, v: "1.0", table: tbl_name, records: records)
49
+ post(DATA_BASE_URL, params)
50
+ end
51
+
52
+ def self.select_record(tbl_name, condition, opts={})
53
+ params = method_params(:select, v: "1.0", table: tbl_name, condition: condition)
54
+ post(DATA_BASE_URL, params)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,72 @@
1
+ ####################################################################################
2
+ # File Apis
3
+ #http://developer.baidu.com/wiki/index.php?title=docs/pcs/rest/file_data_apis_list
4
+
5
+ require "baidu_pcs"
6
+ module BaiduPcs
7
+ class Fs < Base
8
+
9
+ FILE_BASE_URL = "#{PCS_BASE_URL}/file"
10
+
11
+ #path: local file path
12
+ #rpath: 上传文件路径(含上传的文件名称)
13
+ def self.upload(path, rpath=nil, opts={})
14
+ params = method_params(:upload, path: "#{Config.app_root}/#{rpath||File.basename(path)}")
15
+ params[:ondup] = opts.delete(:ondup) if opts[:ondup]
16
+ post(FILE_BASE_URL, params, {file: File.open(path)}, opts)
17
+ end
18
+
19
+ def self.download(rpath, opts={})
20
+ params = method_params(:download, path: "#{Config.app_root}/#{rpath}")
21
+ get(FILE_BASE_URL, params, opts)
22
+ end
23
+
24
+ #流式资源地址,可直接下载
25
+ def self.streamurl(rpath)
26
+ params = method_params(:download, path: "#{Config.app_root}/#{rpath}")
27
+ query_str = params.map{|k, v| "#{k}=#{v}"}.join("&") #可能有些转义问题
28
+ "#{FILE_BASE_URL}?#{query_str}"
29
+ end
30
+
31
+ def self.mkdir(rpath)
32
+ post(FILE_BASE_URL, method_params(:mkdir, path: "#{Config.app_root}/#{rpath}"))
33
+ end
34
+
35
+ def self.meta(rpath)
36
+ get(FILE_BASE_URL, method_params(:meta, path: "#{Config.app_root}/#{rpath}"))
37
+ end
38
+
39
+ def self.list(rpath=nil, opts={})
40
+ params = method_params(:list, path: "#{Config.app_root}/#{rpath}").merge(opts)
41
+ get(FILE_BASE_URL, params)
42
+ end
43
+
44
+ def self.move(from_rpath, to_rpath)
45
+ params = method_params(:move,
46
+ from: "#{Config.app_root}/#{from_rpath}",
47
+ to: "#{Config.app_root}/#{to_rpath}")
48
+ post(FILE_BASE_URL, params)
49
+ end
50
+
51
+ def self.copy(from_rpath, to_rpath)
52
+ params = method_params(:copy,
53
+ from: "#{Config.app_root}/#{from_rpath}",
54
+ to: "#{Config.app_root}/#{to_rpath}")
55
+ post(FILE_BASE_URL, params)
56
+ end
57
+
58
+ #文件/目录删除后默认临时存放在回收站内;10天后永久删除
59
+ def self.delete(rpath)
60
+ params = method_params(:delete, path: "#{Config.app_root}/#{rpath}")
61
+ post(FILE_BASE_URL, params)
62
+ end
63
+
64
+ def self.search(keyword, rpath, opts = {})
65
+ params = method_params(:search,
66
+ wd: keyword,
67
+ path: "#{Config.app_root}/#{rpath}")
68
+ params[:re] = opts[:recursive] ? '1' : '0'
69
+ get(FILE_BASE_URL, params)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,10 @@
1
+ class Object
2
+ def blank?
3
+ nil? || (respond_to?(:empty?) && empty?)
4
+ end
5
+ end
6
+
7
+ module BaiduPcs
8
+ class Util
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module BaiduPcs
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe BaiduPcs do
4
+ it 'should download a file' do
5
+ puts "====download a file"
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'baidu_pcs'
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: baidu_pcs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - cao7113
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-nav
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: typhoeus
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 0.6.5
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 0.6.5
97
+ - !ruby/object:Gem::Dependency
98
+ name: multi_json
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: thor
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: 'Baidu Pcs: personal cloud service'
126
+ email:
127
+ - cao7113@hotmail.com
128
+ executables:
129
+ - baidupcs
130
+ - baidupcs_db
131
+ - baidupcs_local
132
+ extensions: []
133
+ extra_rdoc_files: []
134
+ files:
135
+ - .gitignore
136
+ - .rspec
137
+ - .travis.yml
138
+ - Gemfile
139
+ - LICENSE.txt
140
+ - Note.md
141
+ - README.md
142
+ - Rakefile
143
+ - baidu_pcs.gemspec
144
+ - bin/baidupcs
145
+ - bin/baidupcs_db
146
+ - bin/baidupcs_local
147
+ - lib/baidu_pcs.rb
148
+ - lib/baidu_pcs/cli/base_cli.rb
149
+ - lib/baidu_pcs/cli/db_cli.rb
150
+ - lib/baidu_pcs/cli/fs_cli.rb
151
+ - lib/baidu_pcs/cli/local_cli.rb
152
+ - lib/baidu_pcs/cli/thor_ext.rb
153
+ - lib/baidu_pcs/db.rb
154
+ - lib/baidu_pcs/fs.rb
155
+ - lib/baidu_pcs/util.rb
156
+ - lib/baidu_pcs/version.rb
157
+ - spec/baidu_pcs_spec.rb
158
+ - spec/spec_helper.rb
159
+ homepage: http://shareup.me
160
+ licenses:
161
+ - MIT
162
+ metadata: {}
163
+ post_install_message:
164
+ rdoc_options: []
165
+ require_paths:
166
+ - lib
167
+ required_ruby_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - '>='
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ required_rubygems_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - '>='
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ requirements: []
178
+ rubyforge_project:
179
+ rubygems_version: 2.0.2
180
+ signing_key:
181
+ specification_version: 4
182
+ summary: 'Baidu Pcs: personal cloud service'
183
+ test_files:
184
+ - spec/baidu_pcs_spec.rb
185
+ - spec/spec_helper.rb