shiny-sdk 0.2.0 → 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 +6 -5
  4. data/lib/shiny-sdk.rb +125 -20
  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: 56f3aaede8a828302d70b50a9d08195e5cde3047
4
- data.tar.gz: e5512e3ec167d8c0a23ae907e3317d1813f7d3d9
2
+ SHA256:
3
+ metadata.gz: a6e8b4f3ec25fb2c30b0dcc9b8255d1c5d3d372b02cfe882deac9b376d4d4281
4
+ data.tar.gz: 28aa69a85ef6a4dd6cd3f98a8ab8f7fdac212ba67c5f265488927d430b31675e
5
5
  SHA512:
6
- metadata.gz: 54fe92825571f5e7c1a95dc0cce93879127f513a9e0d8fefa1e342e07d0dcd3e4711bb6755cafda9879a4b1b563d876e7e0d0840fd4085ab1da767e5326567de
7
- data.tar.gz: 83ef04ef5ae4f71663d77e18995339db3b946fd02e6b17084b2436546da32deb05cb0d0f1955a44087cc27462a9327418630041297d96bd8f5e5ebb6c8d45aeb
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
@@ -9,13 +10,13 @@ gem install shiny-sdk
9
10
  ```ruby
10
11
  require 'shiny-sdk'
11
12
 
12
- shiny = Shiny.new('apikey', 'api_secret_key', 'api_host')
13
+ shiny = Shiny.new('apikey', 'api_secret_key', 'api_host') # apikey 和 api_secret_key 必须填写,api_host 不填写会使用默认值。
13
14
 
14
- # 获取最新项目
15
- shiny.recent
15
+ # 获取最新的消息
16
+ shiny.recent(page) # page 选填,不填写默认为 1。
16
17
 
17
18
  # 添加数据
18
- shiny.add(spider_name, level, data, hash)
19
- # 其中三项为必填,hash为选填。
19
+ shiny.add(spider_name:String, level:Integer, data:Hash, hash:String)
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'
@@ -11,14 +12,34 @@ class ShinyError < StandardError
11
12
  end
12
13
 
13
14
  class Shiny
14
- def initialize(api_key, api_secret_key, api_host)
15
- @API_KEY = api_key
16
- @API_SECRET_KEY = api_secret_key
17
- @API_HOST = api_host
15
+ def initialize(api_key, api_secret_key, api_host='https://shiny.kotori.moe')
16
+ @API_KEY = api_key.freeze
17
+ @API_SECRET_KEY = api_secret_key.freeze
18
+ @API_HOST = api_host.freeze
18
19
  end
19
20
 
20
- def add(spider_name, level, data=nil, hash=false)
21
- # 添加数据项
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)
39
+ end
40
+
41
+ # 添加数据项
42
+ def add(spider_name, level, data=nil, hash=false, channel: nil)
22
43
  if data.nil?
23
44
  data = {}
24
45
  end
@@ -27,43 +48,127 @@ 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
65
+
66
+ event['channel'] = channel unless channel.nil?
44
67
 
45
- payload['sign'] = Digest::SHA1.hexdigest(@API_KEY + @API_SECRET_KEY + event.to_json)
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('Network error:' + response.code)
82
+ JSON.parse(response.body)
54
83
  end
55
84
  end
56
85
 
57
- def recent
58
- # 获取最新项目
59
- url = @API_HOST + '/Data/recent'
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)
123
+ end
124
+ end
125
+
126
+ # 获取最新项目
127
+ def recent(page=1)
128
+ url = @API_HOST + "/Data/recent?page=#{page}"
60
129
  uri = URI.parse(url)
61
- req = Net::HTTP::Get.new(uri)
62
- response = Net::HTTP.start(uri.host, uri.port) {|http| http.request req }
63
- if response.code == '200'
130
+ http = Net::HTTP.new(uri.host, uri.port)
131
+ http.use_ssl = true if uri.scheme == 'https'
132
+ response = http.start {|http|
133
+ http.request Net::HTTP::Get.new(uri.request_uri)
134
+ }
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
64
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']}")
65
170
  else
66
- raise ShinyError.new('Network error:' + response.code)
171
+ JSON.parse(response.body)
67
172
  end
68
173
  end
69
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.2.0'
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.2.0
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: 2016-10-23 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.5.1
43
+ rubygems_version: 3.1.6
44
44
  signing_key:
45
45
  specification_version: 4
46
46
  summary: Shiny SDK