shiny-sdk 0.4.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE +1 -1
  3. data/README.md +3 -2
  4. data/lib/shiny-sdk.rb +115 -13
  5. data/shiny-sdk.gemspec +5 -2
  6. metadata +6 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: a6d1b97900a1b2245973fe3c5f00255dadea263f
4
- data.tar.gz: 9837bf65b3d32e43af4abcc2816d69ea8146505c
2
+ SHA256:
3
+ metadata.gz: a6e8b4f3ec25fb2c30b0dcc9b8255d1c5d3d372b02cfe882deac9b376d4d4281
4
+ data.tar.gz: 28aa69a85ef6a4dd6cd3f98a8ab8f7fdac212ba67c5f265488927d430b31675e
5
5
  SHA512:
6
- metadata.gz: 1c5e9f0857640458551443e82de8e757e605a7728d42026da565471ce936f04c2aa85c4e9713eb65dbf23775a5d2886b520291d8ac7471222637e4eaec12b1ce
7
- data.tar.gz: f21a4449183bcf2315a005f910f54c5722187b9aedf6f4b43309c2a8dc9f2ce58be7804bf265bb3a66506f44e0cdd2907b5d6cd9cb069a8a0e3836169e0fa5f8
6
+ metadata.gz: 3b4d3eb499dea9119c4a5792063247bd2e2e70629bbc8c526d1ae9bfcfc9d00d2c3f14b8c653cd3bdbd6c31a6b9bc685bb2dcc425adaa0ce081290b95fa44a37
7
+ data.tar.gz: 27773500cbe7fb9aeceaf6c8c3650e385c1317366e2c7da111a73af4a28854b78a828865d3f77f29596c0efb8e5fdb071a246572b3e1f8f29d7e13b7b1df814d
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2016 Shiny
3
+ Copyright (c) 2016-2018 Shiny
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Shiny Ruby SDK
2
+ Shiny 的 Ruby SDK,基于 Python 版构建。
2
3
 
3
4
  ## 安装
4
5
  ```bash
@@ -11,11 +12,11 @@ require 'shiny-sdk'
11
12
 
12
13
  shiny = Shiny.new('apikey', 'api_secret_key', 'api_host') # apikey 和 api_secret_key 必须填写,api_host 不填写会使用默认值。
13
14
 
14
- # 获取最新项目
15
+ # 获取最新的消息
15
16
  shiny.recent(page) # page 选填,不填写默认为 1。
16
17
 
17
18
  # 添加数据
18
- shiny.add(spider_name, level, data, hash)
19
+ shiny.add(spider_name:String, level:Integer, data:Hash, hash:String)
19
20
  # 其中前三项为必填,hash为选填。
20
21
  # 不填写hash 默认把data MD5 一次做hash。
21
22
  ```
data/lib/shiny-sdk.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # Shiny Ruby SDK
1
2
  require 'json'
2
3
  require 'digest/md5'
3
4
  require 'digest/sha1'
@@ -12,13 +13,33 @@ end
12
13
 
13
14
  class Shiny
14
15
  def initialize(api_key, api_secret_key, api_host='https://shiny.kotori.moe')
15
- @API_KEY = api_key
16
- @API_SECRET_KEY = api_secret_key
17
- @API_HOST = api_host
16
+ @API_KEY = api_key.freeze
17
+ @API_SECRET_KEY = api_secret_key.freeze
18
+ @API_HOST = api_host.freeze
19
+ end
20
+
21
+ # API 请求签名
22
+ def sign(payload={})
23
+ data = @API_KEY + @API_SECRET_KEY
24
+ Hash[payload.sort].keys.each do |key|
25
+ data += payload[key].to_s
26
+ end
27
+ data
28
+ end
29
+
30
+
31
+ # 计算md5
32
+ def md5(text)
33
+ Digest::MD5.hexdigest(text)
34
+ end
35
+
36
+ # 计算sha1
37
+ def sha1(text)
38
+ Digest::SHA1.hexdigest(text)
18
39
  end
19
40
 
20
41
  # 添加数据项
21
- def add(spider_name, level, data=nil, hash=false)
42
+ def add(spider_name, level, data=nil, hash=false, channel: nil)
22
43
  if data.nil?
23
44
  data = {}
24
45
  end
@@ -27,30 +48,78 @@ class Shiny
27
48
 
28
49
  payload = {'api_key' => @API_KEY}
29
50
 
30
- event = {"level": level, "spiderName": spider_name}
51
+ event = {"level": level.to_i, "spiderName": spider_name}
31
52
 
32
53
  # 如果没有手动指定Hash,将会把data做一次md5生成hash
33
54
  begin
34
55
  if hash
35
56
  event['hash'] = hash
36
57
  else
37
- event['hash'] = Digest::MD5.hexdigest(data.to_json)
58
+ event['hash'] = md5(data.to_json)
38
59
  end
39
60
  rescue
40
61
  raise ShinyError.new('Fail to generate hash.')
41
62
  end
42
63
 
43
- event['data'] = data
64
+ event['data'] = data
44
65
 
45
- payload['sign'] = Digest::SHA1.hexdigest(@API_KEY + @API_SECRET_KEY + event.to_json)
66
+ event['channel'] = channel unless channel.nil?
67
+
68
+ payload['sign'] = sha1(@API_KEY + @API_SECRET_KEY + event.to_json)
46
69
 
47
70
  payload["event"] = event.to_json
48
71
 
49
72
  response = Net::HTTP.post_form(URI.parse(url), payload)
50
- if response.code == '200'
51
- return JSON.parse(response.body)
73
+ if response.code != '200'
74
+ begin
75
+ error = JSON.parse(response.body)
76
+ rescue
77
+ raise ShinyError.new("Network error: #{response.code}")
78
+ end
79
+
80
+ raise ShinyError.new("Shiny error: #{error['error']['info']} code=#{error['error']['code']}")
52
81
  else
53
- raise ShinyError.new("HTTP Code: #{response.code}, Body: #{JSON.parse(response.body)}.")
82
+ JSON.parse(response.body)
83
+ end
84
+ end
85
+
86
+ # 添加多个事件
87
+ def add_many(events)
88
+ payload = {}
89
+ url = @API_HOST + "Data/add"
90
+ events.each do |event|
91
+ if event[:hash].nil?
92
+ if event[:data].nil?
93
+ if event[:data][:hash]
94
+ event[:hash] = event[:data][:hash]
95
+ event[:data].delete(:hash)
96
+ else
97
+ event[:hash] = md5(Hash[event[:data]].sort.to_json)
98
+ end
99
+ else
100
+ raise ShinyError.new("Missing parameters.")
101
+ end
102
+ event[:level] = event[:level].to_i
103
+ end
104
+ end
105
+ payload[:event] = events.to_json
106
+
107
+ payload_sign = sign(payload)
108
+
109
+ payload[:sign] = payload_sign
110
+ payload[:api_key] = @API_KEY
111
+
112
+ response = Net::HTTP.post_form(URI.parse(url), payload)
113
+ if response.code != '200'
114
+ begin
115
+ error = JSON.parse(response.body)
116
+ rescue
117
+ raise ShinyError.new("Network error: #{response.code}")
118
+ end
119
+
120
+ raise ShinyError.new("Shiny error: #{error['error']['info']} code=#{error['error']['code']}")
121
+ else
122
+ JSON.parse(response.body)
54
123
  end
55
124
  end
56
125
 
@@ -63,10 +132,43 @@ class Shiny
63
132
  response = http.start {|http|
64
133
  http.request Net::HTTP::Get.new(uri.request_uri)
65
134
  }
66
- if response.code == '200'
135
+ if response.code != '200'
136
+ raise ShinyError.new("Network error: #{response.code}")
137
+ else
138
+ return JSON.parse(response.body)
139
+ end
140
+ end
141
+
142
+ def get_jobs
143
+ url = @API_HOST + "/Job/query?api_key=#{@API_KEY}&sign=#{sign}"
144
+ response = http.start {|http|
145
+ http.request Net::HTTP::Get.new(uri.request_uri)
146
+ }
147
+ if response.code != '200'
148
+ raise ShinyError.new("Network error: #{response.code}")
149
+ else
67
150
  return JSON.parse(response.body)
151
+ end
152
+ end
153
+
154
+ def report(job_id, status)
155
+ url = @API_HOST + "Job/report"
156
+ payload = {
157
+ "jobId": job_id,
158
+ "status": status
159
+ }
160
+ payload_sign = sign(payload)
161
+ response = Net::HTTP.post_form(URI.parse(url), payload)
162
+ if response.code != '200'
163
+ begin
164
+ error = JSON.parse(response.body)
165
+ rescue
166
+ raise ShinyError.new("Network error: #{response.code}")
167
+ end
168
+
169
+ raise ShinyError.new("Shiny error: #{error['error']['info']} code=#{error['error']['code']}")
68
170
  else
69
- raise ShinyError.new("HTTP Code: #{response.code}, Body: #{JSON.parse(response.body)}.")
171
+ JSON.parse(response.body)
70
172
  end
71
173
  end
72
174
  end
data/shiny-sdk.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'shiny-sdk'
3
- s.version = '0.4.1'
3
+ s.version = '1.0.0'
4
4
  s.date = `git log --pretty="%ai" -n 1`.split(" ").first
5
5
  s.summary = "Shiny SDK"
6
6
  s.description = "Shiny Ruby SDK"
@@ -8,6 +8,9 @@ Gem::Specification.new do |s|
8
8
  s.email = 'i@wug.moe'
9
9
  s.files = `git ls-files -z`.split("\0")
10
10
  s.homepage = 'https://shiny.kotori.moe'
11
+ s.metadata = {
12
+ "source_code_uri" => "https://github.com/Shiny-Project/Shiny-Ruby-SDK/"
13
+ }
11
14
  s.license = 'MIT'
12
- s.required_ruby_version = '>= 2.0.0'
15
+ s.required_ruby_version = '>= 2.6.0'
13
16
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shiny-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-28 00:00:00.000000000 Z
11
+ date: 2021-08-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Shiny Ruby SDK
14
14
  email: i@wug.moe
@@ -23,7 +23,8 @@ files:
23
23
  homepage: https://shiny.kotori.moe
24
24
  licenses:
25
25
  - MIT
26
- metadata: {}
26
+ metadata:
27
+ source_code_uri: https://github.com/Shiny-Project/Shiny-Ruby-SDK/
27
28
  post_install_message:
28
29
  rdoc_options: []
29
30
  require_paths:
@@ -32,15 +33,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
32
33
  requirements:
33
34
  - - ">="
34
35
  - !ruby/object:Gem::Version
35
- version: 2.0.0
36
+ version: 2.6.0
36
37
  required_rubygems_version: !ruby/object:Gem::Requirement
37
38
  requirements:
38
39
  - - ">="
39
40
  - !ruby/object:Gem::Version
40
41
  version: '0'
41
42
  requirements: []
42
- rubyforge_project:
43
- rubygems_version: 2.6.13
43
+ rubygems_version: 3.1.6
44
44
  signing_key:
45
45
  specification_version: 4
46
46
  summary: Shiny SDK