baidupan 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 924ff0c1bb3c5b9d29c19dcba20f352c5f32f664
4
+ data.tar.gz: 1f3bd351261e17c4693d10e423c2196c873a6d20
5
+ SHA512:
6
+ metadata.gz: f89b3b7a82cfaf69d9d7e914f8b3015b5f4edeaef3be69f6d8c3365b592947899426f816fcaf30e7ae824dff5d642fa23d6218b54795e8ac035f9d517bfc5b9d
7
+ data.tar.gz: 9682e1e49d46f2b92b8a2332119e4697920cdaf286dfd2f9a1342fdeacba0f64a3246954ebc1c6ea19519980d536de5a6298d780fc2ba4f716d1526baadab180
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-2.0.0-p247@common
data/README.md CHANGED
@@ -16,10 +16,31 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install baidupan
18
18
 
19
+
20
+
19
21
  ## Usage
22
+ 如何使用该gem:
23
+ * 创建百度应用 http://developer.baidu.com/wiki/index.php?title=%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3%E9%A6%96%E9%A1%B5
24
+ * baidupan setup 应用名字 api_key, secret_key
25
+ * baidupan config 按照提示 进行授权
26
+
27
+ 基本命令:
28
+ * baidupan setup 添加文件信息
29
+ * baidupan config 进行授权
30
+ * baidupan list or ls 显示app应用的文件列表
31
+ * baidupan upload 文件
32
+ * 更多命令 请运行baidupan 查看即可
33
+
34
+ Todo:
35
+ * 移动文件的位置
36
+ * 删除文件
37
+ * 对文件重命名
38
+
39
+ 已经实现:
40
+ * 通过配置连接百度盘
41
+ * 能够上传、下载以及显示文件
42
+
20
43
 
21
- TODO: Write usage instructions here
22
- 1: 能够连接百度盘
23
44
 
24
45
  ## Contributing
25
46
 
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["meeasyhappy@gmail.com"]
11
11
  spec.description = %q{利用百度云盘接口, 实现文件备份功能}
12
12
  spec.summary = %q{文件上传和查看等功能}
13
- spec.homepage = ""
13
+ spec.homepage = "http://ml-china.org"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -20,5 +20,6 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "typhoeus", "~> 0.6.5"
23
24
  spec.add_runtime_dependency "thor"
24
25
  end
@@ -1,3 +1,4 @@
1
- require 'baidupan/test'
1
+ #!/usr/bin/env ruby
2
2
 
3
- Baidupan::Test.start
3
+ require 'baidupan/cmd/fs_cmd'
4
+ Baidupan::Cmd::FsCmd.start
@@ -1,9 +1,55 @@
1
+ require "typhoeus"
2
+ require 'multi_json'
3
+
1
4
  require "baidupan/version"
5
+ require "baidupan/config"
6
+
7
+ require 'pry'
2
8
 
3
9
  module Baidupan
10
+ PAN_BASE_URL = "https://pcs.baidu.com/rest/2.0/pcs"
11
+
12
+ class Base
13
+ attr_reader :body
14
+
15
+ def initialize(url, method=:get, params={}, body={}, opts={})
16
+ @options = {
17
+ method: method,
18
+ headers: {"User-Agent"=>"Mozilla/5.0 (X11; Linux x86_64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"},
19
+ params: params
20
+ }
21
+ @options.merge!(body: body) if body
22
+ @options.merge!(opts)
23
+ @request = Typhoeus::Request.new(url, @options)
24
+ @request.on_complete do |response|
25
+ if response.success?
26
+ if response.headers["Content-Disposition"] =~ /attachment;file/ or response.headers["Content-Type"] =~ /image\//
27
+ @body = response.body
28
+ else
29
+ @body = MultiJson.load(response.body, symbolize_keys: true)
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ def run!
36
+ @request.run
37
+ self
38
+ end
39
+
40
+ class << self
41
+ def get(url, params={}, opts={})
42
+ new(url, :get, params, nil, opts).run!
43
+ end
4
44
 
5
- def hello_world
6
- puts 'hello world'
7
- end
45
+ def post(url, params={}, body={}, opts={})
46
+ new(url, :post, params, body, opts).run!
47
+ end
8
48
 
9
- end
49
+ def common_params(method, params={})
50
+ params = {access_token: Config.access_token}.merge(params)
51
+ params.merge!(method: method)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'baidupan'
4
+ require 'thor'
5
+
6
+ module Baidupan::Cmd
7
+ class Base < Thor
8
+ include Thor::Actions
9
+
10
+ desc 'setup [app_name, api_key, secret_key]', "setup your setting when you first use this gem; if you don't have baidu app, please create one: at http://developer.baidu.com/wiki/index.php?title=%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3%E9%A6%96%E9%A1%B5"
11
+ def setup(app_name, api_key, secret_key)
12
+ require 'erb'
13
+ content = (ERB.new <<-EOF).result(binding)
14
+ :app_name: <%=app_name||'<_app_name>'%>
15
+ :app_root: /apps/<%=app_name||'<_app_name_or_you_set_in_baidu>'%>
16
+ :api_key: <%=api_key||'<_api_key>'%>
17
+ :secret_key: <%=secret_key||'<_secret_key>'%>
18
+ :base_url: https://pcs.baidu.com/rest/2.0/pcs
19
+ EOF
20
+
21
+ config_path = Baidupan::CONF_FILE
22
+ File.write(config_path, content)
23
+ say "Has wrote #{config_path} for app settings."
24
+ end
25
+
26
+ desc 'config', 'config your access token'
27
+ def config
28
+ url = "https://openapi.baidu.com/oauth/2.0/authorize?response_type=token&client_id=#{Baidupan::Config.api_key}&redirect_uri=oob&scope=netdisk"
29
+ say "请在浏览器中完成授权操作并获取最终成功url!\n将下面的链接粘入浏览器获取access_token"
30
+ say '*'*60
31
+ say url
32
+ say '*'*60
33
+
34
+ say "将浏览器的url输入到这里:"
35
+ atoken = STDIN.gets.chomp
36
+
37
+ atoken =~ /access_token=([^&]*)/
38
+ atoken = $1 if $1
39
+
40
+ raise "Invalid token: #{atoken}!" if atoken !~ /^[\da-f\.\-]*$/
41
+ File.open(Baidupan::CONF_FILE, "a"){|f| f.puts ":access_token: #{atoken}" }
42
+
43
+ say '-'*60
44
+ say "Have append access token into file: #{Baidupan::CONF_FILE}"
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,59 @@
1
+ require 'baidupan'
2
+ require 'baidupan/cmd/base'
3
+ require 'baidupan/fs_cmd'
4
+
5
+ module Baidupan::Cmd
6
+
7
+ class FsCmd < Base
8
+
9
+ no_tasks do
10
+ def print_item(item)
11
+ new_items = []
12
+ new_items << item[:fs_id]
13
+ new_items << "#{item[:path].sub(Baidupan::Config.app_root + '/', '')}"
14
+ new_items << "#{Time.at(item[:mtime])}"
15
+ print_in_columns new_items
16
+ end
17
+ end
18
+
19
+
20
+ desc 'list [Remote path]', 'list files under Remote path'
21
+ def list(rpath=nil)
22
+ res = Baidupan::FsCmd.list(rpath)
23
+ res.body[:list].each do |item|
24
+ print_item(item)
25
+ end
26
+ end
27
+ map ls: :list
28
+
29
+ desc 'upload [Local path, Remote path]', 'upload a local file /path/to/file --> /apps/appname/[rpath|file]'
30
+ option :ondup, type: :string, desc: <<-Desc, default: :newcopy
31
+ overwrite:表示覆盖同名文件;newcopy:表示生成文件副本并进行重命名,命名规则为“文件名_日期.后缀”。
32
+ Desc
33
+ def upload(lpath, rpath=nil)
34
+ res = Baidupan::FsCmd.upload(lpath, rpath, options.dup)
35
+ print_item res.body
36
+ end
37
+
38
+
39
+ desc 'download file [Remote path, Local path', 'download remote file to local, not support for download dir'
40
+ def download(rpath, lpath=nil)
41
+ lpath = (lpath||rpath).dup
42
+ res = Baidupan::FsCmd.download(rpath, lpath, options.dup)
43
+
44
+ if File.exists?(lpath)
45
+ extname = File.extname(lpath)
46
+ timestamp_name = "_#{Time.now.strftime(Baidupan::Config.time_format)}_#{rand(10)}#{extname}"
47
+
48
+ if extname.empty?
49
+ lpath += timestamp_name
50
+ else
51
+ lpath.gsub!(extname, timestamp_name)
52
+ end
53
+ end
54
+
55
+ File.binwrite(lpath, res.body)
56
+ say "download and save at'#{lpath}'..."
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml'
4
+ require 'pry'
5
+
6
+ module Baidupan
7
+ CONF_FILE = File.expand_path("~/.baidupan.yml")
8
+
9
+ class Config < Hash
10
+ attr_reader :config
11
+
12
+ class << self
13
+
14
+ def single_instance
15
+ @_instance ||= new
16
+ end
17
+
18
+ def method_missing(method, *args)
19
+ single_instance.config[method.to_sym]
20
+ end
21
+
22
+ def file_path
23
+ File.join(self.base_url, 'file')
24
+ end
25
+
26
+ def time_format
27
+ "%Y%m%d%H%M%S"
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def initialize
34
+ @config = YAML.load_file(CONF_FILE)
35
+ end
36
+ end
37
+ end
38
+ Baidupan::Config.app_root
@@ -0,0 +1,31 @@
1
+ require 'baidupan'
2
+ require 'baidupan/config'
3
+
4
+ module Baidupan
5
+
6
+ class FsCmd < Base
7
+ class << self
8
+
9
+ def list(rpath, **opts)
10
+ opts.merge!(common_params(:list, path: "#{Config.app_root}/#{rpath}"))
11
+ get(Config.file_path, opts)
12
+ end
13
+
14
+ def upload(lpath, rpath, opts={})
15
+ params = common_params(:upload, path: "#{Config.app_root}/#{rpath}/#{lpath}").merge(ondup: :newcopy)
16
+ params[:ondup] = opts.delete(:ondup) if opts[:ondup]
17
+
18
+ body = {:file => File.open(lpath)}
19
+ opts[:noprogress] ||= true
20
+
21
+ post(Config.file_path, params, body, opts)
22
+ end
23
+
24
+ def download(rpath, lpath, opts={})
25
+ params = common_params(:download, path: "#{Config.app_root}/#{rpath}")
26
+
27
+ get(Config.file_path, params, opts.merge(followlocation: true))
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module Baidupan
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baidupan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.0.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - AndyHu
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-09-27 00:00:00.000000000 Z
11
+ date: 2013-09-28 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
@@ -30,33 +27,43 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: typhoeus
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.6.5
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.6.5
46
55
  - !ruby/object:Gem::Dependency
47
56
  name: thor
48
57
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
58
  requirements:
51
- - - ! '>='
59
+ - - '>='
52
60
  - !ruby/object:Gem::Version
53
61
  version: '0'
54
62
  type: :runtime
55
63
  prerelease: false
56
64
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
65
  requirements:
59
- - - ! '>='
66
+ - - '>='
60
67
  - !ruby/object:Gem::Version
61
68
  version: '0'
62
69
  description: 利用百度云盘接口, 实现文件备份功能
@@ -68,6 +75,7 @@ extensions: []
68
75
  extra_rdoc_files: []
69
76
  files:
70
77
  - .gitignore
78
+ - .rvmrc
71
79
  - Gemfile
72
80
  - LICENSE.txt
73
81
  - README.md
@@ -75,31 +83,34 @@ files:
75
83
  - baidupan.gemspec
76
84
  - bin/baidupan
77
85
  - lib/baidupan.rb
86
+ - lib/baidupan/cmd/base.rb
87
+ - lib/baidupan/cmd/fs_cmd.rb
88
+ - lib/baidupan/config.rb
89
+ - lib/baidupan/fs_cmd.rb
78
90
  - lib/baidupan/test.rb
79
91
  - lib/baidupan/version.rb
80
- homepage: ''
92
+ homepage: http://ml-china.org
81
93
  licenses:
82
94
  - MIT
95
+ metadata: {}
83
96
  post_install_message:
84
97
  rdoc_options: []
85
98
  require_paths:
86
99
  - lib
87
100
  required_ruby_version: !ruby/object:Gem::Requirement
88
- none: false
89
101
  requirements:
90
- - - ! '>='
102
+ - - '>='
91
103
  - !ruby/object:Gem::Version
92
104
  version: '0'
93
105
  required_rubygems_version: !ruby/object:Gem::Requirement
94
- none: false
95
106
  requirements:
96
- - - ! '>='
107
+ - - '>='
97
108
  - !ruby/object:Gem::Version
98
109
  version: '0'
99
110
  requirements: []
100
111
  rubyforge_project:
101
- rubygems_version: 1.8.25
112
+ rubygems_version: 2.1.4
102
113
  signing_key:
103
- specification_version: 3
114
+ specification_version: 4
104
115
  summary: 文件上传和查看等功能
105
116
  test_files: []