sleeproom 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "async/queue"
4
+ module SleepRoom
5
+ module Record
6
+ class WriteStatus
7
+ attr_accessor :queue
8
+ def initialize
9
+ @queue = Async::Queue.new
10
+ end
11
+
12
+ def run
13
+ Async do
14
+ while status = @queue.dequeue
15
+ status[:update] = Time.now
16
+ old_status = SleepRoom.load_config(:status)
17
+ room = status[:room]
18
+ if old_status.find { |h| h[:room] == room }
19
+ new_status = old_status.delete_if { |h| h[:room] == room }
20
+ new_status.push(status)
21
+ else
22
+ new_status = old_status.push(status)
23
+ end
24
+ SleepRoom.write_config_file(:status, new_status)
25
+ end
26
+ end
27
+ end
28
+
29
+ def add(status)
30
+ Async do
31
+ @queue.enqueue(status)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,214 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "configatron"
4
+ require "colorize"
5
+ require "fileutils"
6
+ require "yaml"
7
+
8
+ module SleepRoom
9
+ class Error < StandardError; end
10
+ # @return [String]
11
+ def self.root_path
12
+ Dir.pwd
13
+ end
14
+
15
+ # @return [String]
16
+ def self.user_path
17
+ Dir.home
18
+ end
19
+
20
+ # @return [String]
21
+ def self.sleeproom_dir
22
+ File.join(user_path, "sleeproom")
23
+ end
24
+
25
+ def self.working_dir
26
+ Dir.pwd
27
+ end
28
+
29
+ # @return [String]
30
+ def self.config_dir
31
+ File.join(user_path, ".config", "sleeproom")
32
+ end
33
+
34
+ # @param filename [String]
35
+ # @return [String]
36
+ def self.config_path(config)
37
+ name = {
38
+ status: "status.yml",
39
+ base: "base.yml",
40
+ record: "record.yml",
41
+ pid: "tmp/pid.yml"
42
+ }
43
+ file = name[config].to_s
44
+ raise Error if file.empty?
45
+ return File.join(config_dir, file) if config == :base
46
+
47
+ if load_config(:base)[:config_path] == "USER_CONFIG"
48
+ File.join(config_dir, file)
49
+ else
50
+ File.join(load_config(:base)[:config_path], file)
51
+ end
52
+ end
53
+
54
+ # @param filename [Symbol]
55
+ # @param settings [Hash]
56
+ # @return [Boolean]
57
+ def self.write_config_file(config, settings)
58
+ file = File.new(config_path(config), "w")
59
+ file.puts(YAML.dump(settings))
60
+ file.close
61
+ end
62
+
63
+ def self.create_config_file(config, settings)
64
+ path = config_path(config)
65
+ return false if File.exist?(path)
66
+ mkdir(File.dirname(path)) unless Dir.exist?(File.dirname(path))
67
+ write_config_file(config, settings)
68
+ end
69
+
70
+ def self.load_config(config)
71
+ raise Error if config.empty? || !File.exist?(config_path(config))
72
+
73
+ YAML.load_file(config_path(config))
74
+ rescue Error => e
75
+ init_config
76
+ retry
77
+ end
78
+
79
+ def self.mkdir(path)
80
+ FileUtils.mkdir_p(path) unless Dir.exist?(path)
81
+ end
82
+
83
+ def self.init_base
84
+ base = {
85
+ web: {
86
+ use: true,
87
+ server: "localhost",
88
+ port: 3000
89
+ },
90
+ proxy: {
91
+ use: false,
92
+ server: "localhost",
93
+ port: 8080,
94
+ type: "socks5"
95
+ },
96
+ record: {
97
+ all: true,
98
+ wanted: [],
99
+ unwanted: []
100
+ },
101
+ config_path: "USER_CONFIG",
102
+ save_path: "#{sleeproom_dir}/archive",
103
+ working_path: "#{sleeproom_dir}/working",
104
+ default_save_name: "%ROOMNAME%-%TIME%.ts",
105
+ minyami: {
106
+ threads: 8,
107
+ retries: 999,
108
+ },
109
+ logger: {
110
+ console: true,
111
+ file: {
112
+ use: true,
113
+ path: "#{sleeproom_dir}/log"
114
+ },
115
+ websocket: {
116
+ log: true,
117
+ ping_log: false
118
+ }
119
+ }
120
+ }
121
+ create_config_file(:base, base)
122
+ end
123
+
124
+ def self.init_config
125
+ mkdir(config_dir) unless Dir.exist?(config_dir)
126
+
127
+ mkdir("#{config_dir}/tmp") unless Dir.exist?("#{config_dir}/tmp")
128
+
129
+ init_base
130
+
131
+ record = {
132
+ "default" => []
133
+ }
134
+
135
+ create_config_file(:record, record)
136
+ create_config_file(:status, [])
137
+ write_config_file(:pid, nil)
138
+ end
139
+
140
+ # @return [Boolean]
141
+ def self.reload_config
142
+ configs = %i[base]
143
+ configs.each do |config|
144
+ configatron.configure_from_hash(YAML.load_file(config_path(config)))
145
+ end
146
+ true
147
+ rescue Errno::ENOENT => e
148
+ error("Could not load file. \n#{e.message}")
149
+ false
150
+ end
151
+
152
+ def self.settings
153
+ configatron
154
+ end
155
+ def self.load_status
156
+ SleepRoom.load_config(:status)
157
+ rescue Error
158
+ create_status
159
+ retry
160
+ end
161
+
162
+ def self.load_pid
163
+ SleepRoom.load_config(:pid)
164
+ rescue Error
165
+ create_pid(nil)
166
+ retry
167
+ end
168
+
169
+ def self.running?(pid=nil)
170
+ pid = SleepRoom.load_config(:pid) if pid.nil?
171
+ Process.getpgid(pid)
172
+ true
173
+ rescue
174
+ false
175
+ end
176
+
177
+ def self.load_record
178
+ SleepRoom.load_config(:record)
179
+ rescue Error
180
+ create_record
181
+ retry
182
+ end
183
+
184
+ def self.create_status(status = [])
185
+ SleepRoom.create_config_file(:status, status)
186
+ end
187
+
188
+ def self.create_record(record = {default: []})
189
+ SleepRoom.create_config_file(:record, record)
190
+ end
191
+
192
+ def self.create_pid(pid)
193
+ SleepRoom.create_config_file(:pid, pid)
194
+ SleepRoom.write_config_file(:pid, pid)
195
+ end
196
+
197
+ # @param string [String]
198
+ # @return [nil]
199
+ def self.info(string)
200
+ STDOUT.puts("[INFO] #{string}".colorize(:white))
201
+ end
202
+
203
+ # @param string [String]
204
+ # @return [nil]
205
+ def self.warning(string)
206
+ warn("[WARN] #{string}".colorize(:yellow))
207
+ end
208
+
209
+ # @param string [String]
210
+ # @return [nil]
211
+ def self.error(string)
212
+ STDOUT.puts("[ERROR] #{string}".colorize(:red))
213
+ end
214
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SleepRoom
4
+ VERSION = "0.1.1"
5
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/sleeproom/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "sleeproom"
7
+ spec.version = SleepRoom::VERSION
8
+ spec.authors = ["Koell"]
9
+ spec.email = ["i@wug.moe"]
10
+
11
+ spec.summary = "sleeproom"
12
+ spec.homepage = "https://github.com/KoellM/sleeproom"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "bin"
25
+ spec.executables = ["sleeproom"]
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_runtime_dependency("colorize")
29
+ spec.add_runtime_dependency("async")
30
+ spec.add_runtime_dependency("async-websocket")
31
+ spec.add_runtime_dependency("configatron")
32
+ spec.add_runtime_dependency("async-http-faraday")
33
+ spec.add_runtime_dependency("terminal-table")
34
+
35
+ spec.post_install_message = <<~STR
36
+ SleepRoom 需要:
37
+ [Minyami] https://github.com/Last-Order/minyami
38
+ (npm install -g minyami)
39
+ 使用前请确保已安装上述依赖。
40
+ STR
41
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sleeproom
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Koell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-06-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: async
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: async-websocket
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: configatron
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
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: async-http-faraday
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
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: terminal-table
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - i@wug.moe
100
+ executables:
101
+ - sleeproom
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".rubocop.yml"
108
+ - ".rubocop_todo.yml"
109
+ - Gemfile
110
+ - Gemfile.lock
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - bin/sleeproom
115
+ - lib/sleeproom/async/websocket.rb
116
+ - lib/sleeproom/cli.rb
117
+ - lib/sleeproom/record.rb
118
+ - lib/sleeproom/record/api/api.rb
119
+ - lib/sleeproom/record/api/room.rb
120
+ - lib/sleeproom/record/api/room_api.rb
121
+ - lib/sleeproom/record/api/streaming_api.rb
122
+ - lib/sleeproom/record/record.rb
123
+ - lib/sleeproom/record/tasks.rb
124
+ - lib/sleeproom/record/websocket.rb
125
+ - lib/sleeproom/record/write_status.rb
126
+ - lib/sleeproom/utils.rb
127
+ - lib/sleeproom/version.rb
128
+ - sleeproom.gemspec
129
+ homepage: https://github.com/KoellM/sleeproom
130
+ licenses:
131
+ - MIT
132
+ metadata:
133
+ homepage_uri: https://github.com/KoellM/sleeproom
134
+ post_install_message: |
135
+ SleepRoom 需要:
136
+ [Minyami] https://github.com/Last-Order/minyami
137
+ (npm install -g minyami)
138
+ 使用前请确保已安装上述依赖。
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: 2.3.0
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubygems_version: 3.1.4
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: sleeproom
157
+ test_files: []