api4baidu 0.1.1 → 1.0.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e94a9e1c4b286e14c7fbc508616cb3fb96a32e14
4
- data.tar.gz: 9594091884bdae4160429ab84f15920f8a38196b
3
+ metadata.gz: 4a4388823d9e24396b98e6ab7cc390ea7594a519
4
+ data.tar.gz: 86c4e553fe26308c79be269bda3e3d87d2ce3eb5
5
5
  SHA512:
6
- metadata.gz: 99493a6e3d97ad17d2c527121310574a9d1678907d7a1996517e85cf31c8b7f2b8a3f8cd1a14a731b0a2e00aeca804c4943ac064e1f64176cf8767c64b89ed7f
7
- data.tar.gz: c5aba91a6be557155574cbd5991f32d2df24cb84fc560b9429c088a1694e46b1977a7db977b9cb96949eb448978fa3bfce7e8c22dc0f5b9636714ff47d19b4ea
6
+ metadata.gz: 0f8d6419f90e284965beee02ab0401d4450eaf5b747fb4f67b4c06a929172104f42b7b29f034c645e9771ac9a6a857d82c6abe48c0839d8f0c16bbf1683d30dc
7
+ data.tar.gz: 233e09592d2b66d9178156c78ca81a9cb1469c029e64884b8297803674dbd83c6d1bb712e38432b56a09cd24539afb6882e4517a24f9f1b97067002a9713cd49
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  Ruby gem for Baidu <http://developer.baidu.com> apis.
4
4
  **Developing**
5
5
 
6
- Rubygems: https://rubygems.org/gems/api4baidu
6
+ Rubygems: <https://rubygems.org/gems/api4baidu>
7
7
 
8
8
  ## Installation
9
9
 
@@ -26,7 +26,7 @@ Or install it yourself as:
26
26
  ```ruby
27
27
 
28
28
  $client = Baidu.configure do |config|
29
- config.api_key = "you client id"
29
+ config.api_key = "you client id"
30
30
  config.api_secret = "you client secert"
31
31
  end
32
32
 
@@ -45,12 +45,62 @@ $client.token!("515a268fd483ff4df85d2d458d34b43a")
45
45
  $client.get_loggedin_user
46
46
 
47
47
  => Result:
48
- {"uid"=>"1863251187",
49
- "uname"=>"0807515210",
50
- "portrait"=>"ba6f303830373531353231302c0a"}
48
+ {"uid" => "1863251187",
49
+ "uname" => "0807515210",
50
+ "portrait"=> "ba6f303830373531353231302c0a"}
51
51
 
52
52
  ```
53
53
 
54
+ 更多使用例子请查看 `spec`目录
55
+
56
+ ## 已完成API列表
57
+
58
+ ### 用户基础信息
59
+
60
+ 1. 获取当前登录用户的信息: get_loggedin_user
61
+
62
+ 2. 返回指定用户的用户资料: get_user_info
63
+
64
+ 3. 平台授权相关的权限: get_app_permission
65
+
66
+ 4. 判断用户是否为应用用户: is_app_user
67
+
68
+ 5. 返回用户好友资料: get_friends
69
+
70
+ 6. 获得指定用户之间好友关系: areFriends
71
+
72
+ ### PCS
73
+
74
+ 1. 获取当前用户空间配额信息: pcs_quota
75
+
76
+ 2. 上传单个文件: upload_single_file
77
+
78
+ 3. 下载单个文件: download_single_file
79
+
80
+ 4. 创建目录: create_directory
81
+
82
+ 5. 获取单个文件/目录的元信息: get_single_meta
83
+
84
+ 6. 删除单个文件/目录: delete_single_file
85
+
86
+ 7. 获取指定图片文件的缩略图: get_image_thumbnail
87
+
88
+ ### 翻译
89
+
90
+ 1. 翻译: translate
91
+
92
+ ### 工具
93
+
94
+ 1. 查询IP地址所在地区: query_with_ip
95
+
96
+ ## TODO
97
+
98
+ 1. 逐步完善所有API对接
99
+
100
+ 2. 重构oauth2,分离出独立的gem实现
101
+
102
+ 3. 添加Terminal命令控制
103
+
54
104
  ## Contributing
55
105
 
56
106
  1. Fork it
data/lib/baidu.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require "active_support/core_ext/object/to_query"
2
2
  require 'active_support/concern'
3
3
 
4
- %w(api/pcs api/user version client).each do |fname|
4
+ %w(api/pcs api/user api/tool api/translation version client).each do |fname|
5
5
  require File.expand_path("../baidu/#{fname}", __FILE__)
6
6
  end
7
7
 
data/lib/baidu/api/pcs.rb CHANGED
@@ -17,12 +17,62 @@ module Baidu
17
17
  # 百度PCS服务目前支持最大2G的单个文件上传。
18
18
  # 如需支持超大文件(>2G)的断点续传,请参考下面的“分片文件上传”方法。
19
19
  def upload_single_file(yun_path, source_file_path="", opts={})
20
- require 'rest-client'
21
20
  source_file = File.open(source_file_path)
22
21
  response = RestClient.post(upload_file_url(path: yun_path), file: source_file)
23
22
  JSON.parse(response)
24
23
  end
25
24
 
25
+ # 下载单个文件。
26
+ # Download接口支持HTTP协议标准range定义,通过指定range的取值可以实现断点下载功能。 例如:
27
+ # 如果在request消息中指定“Range:bytes=099”,那么响应消息中会返回该文件的前100个字节的内容;继续指定“Range: bytes=100-199”,那么响应消息中会返回该文件的第二个100字节内容。
28
+
29
+ # 注意: 此API不直接下载文件,而是直接返回下载链接
30
+ def download_single_file(file_path)
31
+ default = {method: "download", path: file_path}
32
+ "https://d.pcs.baidu.com/rest/2.0/pcs/file?#{query_params(default)}"
33
+ end
34
+
35
+ # 创建目录
36
+ # 为当前用户创建一个目录。
37
+ # 注意:这个是您使用的路径不正确,这个路径需要是您开启PCS权限的时候,填写的文件夹名称,而不能随便填写
38
+ def create_directory(dir_path)
39
+ default = {method: "mkdir"}
40
+ create_dir_url = pcs_base_url("file", default)
41
+ response = RestClient.post(create_dir_url, path: dir_path)
42
+ JSON.parse(response)
43
+ end
44
+
45
+ # 获取单个文件/目录的元信息
46
+ def get_single_meta(path)
47
+ default = {method: "meta", path: path}
48
+ meta_url = pcs_base_url("file", default)
49
+ get_response_json(meta_url)
50
+ end
51
+
52
+ # 批量获取文件/目录的元信息
53
+ # list value should be a json
54
+ # {"list":[{"path":"\/apps\/album\/a\/b\/c"},{"path":"\/apps\/album\/a\/b\/d"}]}
55
+ # def get_batch_metas(*paths)
56
+ # path_list ={"list": paths}
57
+ # default = {method: "meta", param: paths}
58
+ # batch_metas_url = pcs_base_url("file", default)
59
+ # end
60
+
61
+ # 删除单个文件/目录。
62
+ def delete_single_file(path)
63
+ default = {method: "delete"}
64
+ create_dir_url = pcs_base_url("file", default)
65
+ response = RestClient.post(create_dir_url, path: path)
66
+ JSON.parse(response)
67
+ end
68
+
69
+ # 获取指定图片文件的缩略图。
70
+ def get_image_thumbnail(image_path, height, width ,quality="100")
71
+ default = {method: "generate", path: image_path,
72
+ width: width, height: height, quality: quality}
73
+ pcs_base_url("thumbnail", default)
74
+ end
75
+
26
76
  private
27
77
  def pcs_base_url(path, params={})
28
78
  "https://pcs.baidu.com/rest/2.0/pcs/#{path}?#{query_params(params)}"
@@ -0,0 +1,19 @@
1
+ module Baidu
2
+ module Tool
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+
7
+ # 查询IP地址所在地区
8
+ # 根据ip计算出用户对应的省份与城市。如果应用还没有开通平台授权权限,可以暂时使用用户token来调用此API。
9
+ # https://openapi.baidu.com/rest/2.0/iplib/query
10
+ # ip: 系统输入参数为编码为UTF-8格式编码。需要查询的ip地址,多个ip地址请用英文逗号隔开。
11
+ def query_with_ip(ip)
12
+ default = {ip: ip}
13
+ query_url = "https://openapi.baidu.com/rest/2.0/iplib/query?#{query_params(default)}"
14
+ get_response_json(query_url)
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ module Baidu
2
+ module Translation
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+
7
+ # http://openapi.baidu.com/public/2.0/bmt/translate
8
+ # auto auto 自动识别
9
+ # zh en 中 -> 英
10
+ # zh jp 中 -> 日
11
+ # en zh 英 -> 中
12
+ # jp zh 日 -> 中
13
+ # client_id=YourApiKey&q=today&from=auto&to=auto
14
+ def translate(from="auto", to="auto", q)
15
+ params = {from: from, to: to, q: q, client_id: api_key}.to_query
16
+ translate_url = "http://openapi.baidu.com/public/2.0/bmt/translate?#{params}"
17
+ get_response_json(translate_url)
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -9,7 +9,7 @@ module Baidu
9
9
  # 获取当前登录用户的信息
10
10
  # passport/users/getLoggedInUser
11
11
  def get_loggedin_user
12
- profile_url = "#{user_base_url('getLoggedInUser')}"
12
+ profile_url = "#{user_base_url('passport/users/getLoggedInUser')}"
13
13
  get_response_json(profile_url)
14
14
  end
15
15
 
@@ -18,7 +18,7 @@ module Baidu
18
18
  # uid default "", will return current user info
19
19
  # if you want to access other user info, you should have this permission
20
20
  def get_user_info(uid="")
21
- user_info_url = "#{user_base_url('getInfo', uid: uid)}"
21
+ user_info_url = "#{user_base_url('passport/users/getInfo', uid: uid)}"
22
22
  get_response_json(user_info_url)
23
23
  end
24
24
 
@@ -31,19 +31,39 @@ module Baidu
31
31
  # public 可以访问公共的开放API。
32
32
  # hao123 可以访问Hao123 提供的开放API接口该权限需要申请开通,请将具体的理由和用途发邮件给tuangou@baidu.com。
33
33
  def get_app_permission(uid="", ext_perms)
34
- app_permissions_url = "#{user_base_url('hasAppPermissions', {uid: uid, ext_perms: ext_perms})}"
34
+ app_permissions_url = "#{user_base_url('passport/users/hasAppPermissions', {uid: uid, ext_perms: ext_perms})}"
35
35
  get_response_json(app_permissions_url)
36
36
  end
37
37
 
38
38
  # refactor
39
+ # 判断用户是否为应用用户
39
40
  def is_app_user(uid="")
40
- app_user_url = "#{user_base_url('isAppUser', uid: uid)}"
41
+ app_user_url = "#{user_base_url('passport/users/isAppUser', uid: uid)}"
41
42
  get_response_json(app_user_url)
42
43
  end
43
44
 
45
+ # 返回用户好友资料
46
+ # 根据用户id以及在百度的相应的操作权限(可以是多个权限半角逗号隔开)来判断用户是否可以进行此操作。
47
+ # https://openapi.baidu.com/rest/2.0/friends/getFriends
48
+
49
+ def get_friends(params={})
50
+ get_friends_url = "#{user_base_url('friends/getFriends', params)}"
51
+ get_response_json(get_friends_url)
52
+ end
53
+
54
+ # 获得指定用户之间好友关系
55
+ # 获得指定用户之间是否是好友关系。第一个数组指定一半用户,第二个数组指定另外一半,两个数组必须同样的个数,一次最多可以查20个。
56
+ # https://openapi.baidu.com/rest/2.0/friends/areFriends
57
+ # uids1、uids2每个uid都是以半角逗号隔开。二者个数必须相等: 402818250,2718323483"
58
+ def areFriends(uids1, uids2)
59
+ default = {uids1: uids1, uids2: uids2}
60
+ are_friends_url = "#{user_base_url('friends/areFriends', default)}"
61
+ get_response_json(are_friends_url)
62
+ end
63
+
44
64
  private
45
65
  def user_base_url(path, params={})
46
- "https://openapi.baidu.com/rest/2.0/passport/users/#{path}?#{query_params(params)}"
66
+ "https://openapi.baidu.com/rest/2.0/#{path}?#{query_params(params)}"
47
67
  end
48
68
  end
49
69
  end
data/lib/baidu/client.rb CHANGED
@@ -1,11 +1,14 @@
1
1
  require 'oauth2'
2
2
  require "json"
3
+ require 'rest-client'
3
4
 
4
5
  module Baidu
5
6
  class Client
6
7
 
7
8
  include Pcs
8
9
  include User
10
+ include Tool
11
+ include Translation
9
12
 
10
13
  attr_accessor :api_key, :api_secret
11
14
  ##
data/lib/baidu/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Baidu
2
- VERSION = "0.1.1"
2
+ VERSION = "1.0.0"
3
3
  end
data/spec/feature_spec.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
+ # if you want to run test, please instead 'backup_baidu' with you application name
3
4
  describe "Bidu" do
4
5
  describe "Feature" do
5
6
  before :all do
@@ -10,6 +11,10 @@ describe "Bidu" do
10
11
  auth_code = $stdin.gets.chomp.split("\n").first
11
12
  $client.token!(auth_code)
12
13
  puts "access_token = %s" % $client.access_token.token
14
+
15
+ @application_name = "backup_baidu"
16
+ @app_dir = "/apps/#{@application_name}"
17
+ @new_dir_path = "#{@app_dir}/gem_test_#{Time.now.to_i}"
13
18
  end
14
19
 
15
20
  describe "#User personal info" do
@@ -27,6 +32,19 @@ describe "Bidu" do
27
32
  result_json = $client.get_user_info
28
33
  result_json.keys.should =~ ["userid", "username", "birthday", "marriage", "sex", "blood", "constellation", "figure", "trade", "job", "portrait", "education"]
29
34
  end
35
+
36
+ it "should get friends" do
37
+ result_json = $client.get_friends
38
+ result_json.first.keys.should =~ ["uid", "uname", "portrait"]
39
+ end
40
+
41
+ it "should return some friends relations" do
42
+ uids1 = "2718323483,402818250,2819457800"
43
+ uids2 = "1527552252,3289658540,1694714450"
44
+ result_json = $client.areFriends(uids1, uids2)
45
+ result_json.size.should == 3
46
+ end
47
+
30
48
  end
31
49
 
32
50
  describe "#User netdisk" do
@@ -37,9 +55,73 @@ describe "Bidu" do
37
55
 
38
56
  it "should upload a image file to baidu yun" do
39
57
  @source_path = File.expand_path("../fixtures/avater.jpg", __FILE__)
40
- result_json = $client.upload_single_file("/apps/backup_baidu/avater.jpg", @source_path)
58
+ result_json = $client.upload_single_file("#{@app_dir}/avater.jpg", @source_path)
41
59
  result_json.keys.should =~ ["path", "size", "ctime", "mtime", "md5", "fs_id", "request_id"]
42
60
  end
61
+
62
+ it "should download a image named avater.jpg" do
63
+ download_url = $client.download_single_file("#{@app_dir}/avater.jpg")
64
+ response = $client.access_token.get(download_url)
65
+
66
+ response.status.should == 200
67
+ response.headers["content-type"].should == "image/jpeg"
68
+ end
69
+
70
+ it "should create a directory" do
71
+ response = $client.create_directory(@new_dir_path)
72
+ response.keys.should =~ ["request_id", "ctime", "fs_id", "mtime", "path"]
73
+ end
74
+
75
+ it "should delete a dir" do
76
+ response = $client.delete_single_file(@new_dir_path)
77
+ response.keys.should =~ ["request_id"]
78
+ end
79
+
80
+ it "should get a image named avater.jpg meta" do
81
+ response = $client.get_single_meta("#{@app_dir}/avater.jpg")
82
+ response.keys.should =~ ["list", "request_id"]
83
+ response["list"].first.keys.should =~ ["block_list", "ctime", "filenum", "fs_id",
84
+ "ifhassubdir", "isdir", "mtime", "path", "size"]
85
+ response["list"].first["path"].should == "#{@app_dir}/avater.jpg"
86
+ response["list"].first["isdir"].should == 0
87
+ end
88
+
89
+ it "should get a dir meta" do
90
+ response = $client.get_single_meta(@app_dir)
91
+ response.keys.should =~ ["list", "request_id"]
92
+ response["list"].first["path"].should == @app_dir
93
+ response["list"].first["isdir"].should == 1
94
+ end
95
+
96
+ it "should get a thumbnail image" do
97
+ thumbnail_url = $client.get_image_thumbnail("#{@app_dir}/avater.jpg", 50, 50)
98
+ response = $client.access_token.get(thumbnail_url)
99
+ response.status.should == 200
100
+ response.headers["content-type"].should == "image/jpeg"
101
+ end
102
+
103
+ it "should delete a file" do
104
+ response = $client.delete_single_file("#{@app_dir}/avater.jpg")
105
+ response.keys.should =~ ["request_id"]
106
+ end
107
+
108
+ end
109
+
110
+ describe "#Tool" do
111
+ it "query with your ip " do
112
+ ip = "163.179.238.216"
113
+ result_json = $client.query_with_ip(ip)
114
+ result_json[ip]["province"].should == "广东"
115
+ result_json[ip]["city"].should == "惠州"
116
+ end
117
+ end
118
+
119
+ describe "#Translate" do
120
+ it "should translate 'Today' to '今天'" do
121
+ result_json = $client.translate("en", "zh", "Today")
122
+ result_json["trans_result"][0]["src"].should == "Today"
123
+ result_json["trans_result"][0]["dst"].should == "今天"
124
+ end
43
125
  end
44
126
 
45
127
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api4baidu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dylan Deng
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-15 00:00:00.000000000 Z
11
+ date: 2013-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -83,6 +83,8 @@ files:
83
83
  - baidu.gemspec
84
84
  - lib/baidu.rb
85
85
  - lib/baidu/api/pcs.rb
86
+ - lib/baidu/api/tool.rb
87
+ - lib/baidu/api/translation.rb
86
88
  - lib/baidu/api/user.rb
87
89
  - lib/baidu/client.rb
88
90
  - lib/baidu/version.rb