midjourney-ruby 0.1.21 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/midjourney/api/gateway.rb +107 -0
- data/lib/midjourney/api/imagine.rb +14 -0
- data/lib/midjourney/api/messages.rb +18 -0
- data/lib/midjourney/configuration.rb +6 -2
- data/lib/midjourney.rb +30 -3
- data/lib/tasks/install.rb +15 -9
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2e648fd2e9826785943bab6a582fca4da21ae4358bf76cd80554b84f25e6df23
|
4
|
+
data.tar.gz: 2e3c6ad1067d655703e40e0b30b2a73c30e0812d9408f4303275253b8a62c68d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 487bb6c38f6ed7dc77ad490d02720b2f24d60129bc3c644a4cad178e285c12269e7fc212dac12ad6e5f9b553e0ba8fd6e99bd8b823e94df00d483c0ceb056fb7
|
7
|
+
data.tar.gz: 2819dadc88926ab1aad5d30bfbd331bd65fb0258103768d1dbf8f436865df0d993b94d005b43bf71c6ea7b6746a4e0ecf37ca301902ccc4168aab5f9d157a438
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Midjourney
|
4
|
+
module API
|
5
|
+
#
|
6
|
+
# Discord API Gateway using RestClient
|
7
|
+
#
|
8
|
+
class Gateway
|
9
|
+
BASE_URL = 'https://discord.com/api/v9'
|
10
|
+
|
11
|
+
attr_reader :token, :channel_id
|
12
|
+
|
13
|
+
def initialize(token:, channel_id:)
|
14
|
+
@token = token
|
15
|
+
@channel_id = channel_id
|
16
|
+
end
|
17
|
+
|
18
|
+
def read_messages(limit = 50)
|
19
|
+
get_request("#{BASE_URL}/channels/#{channel_id}/messages?limit=#{limit}")
|
20
|
+
end
|
21
|
+
|
22
|
+
def send_message(message)
|
23
|
+
post_request("#{BASE_URL}/channels/#{channel_id}/messages", message_payload(message))
|
24
|
+
end
|
25
|
+
|
26
|
+
def send_imagine(prompt)
|
27
|
+
post_request("#{BASE_URL}/interactions", imagine_payload(prompt))
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def get_request(url)
|
33
|
+
JSON.parse RestClient.get(url, headers).body
|
34
|
+
rescue RestClient::ExceptionWithResponse => e
|
35
|
+
puts "Error sending message: #{e.response.body}"
|
36
|
+
end
|
37
|
+
|
38
|
+
def post_request(url, payload)
|
39
|
+
RestClient.post(url, payload, headers)
|
40
|
+
rescue RestClient::ExceptionWithResponse => e
|
41
|
+
puts "Error sending message: #{e.response.body}"
|
42
|
+
end
|
43
|
+
|
44
|
+
def imagine_payload(prompt) # rubocop:disable Metrics/MethodLength
|
45
|
+
{
|
46
|
+
type: 2,
|
47
|
+
application_id: '936929561302675456',
|
48
|
+
guild_id: '1158081328068165704',
|
49
|
+
channel_id: channel_id,
|
50
|
+
session_id: '939307d50a643c3d8cfcf4e756063e3b',
|
51
|
+
data: {
|
52
|
+
version: '1118961510123847772',
|
53
|
+
id: '938956540159881230',
|
54
|
+
name: 'imagine',
|
55
|
+
type: 1,
|
56
|
+
options: [
|
57
|
+
{
|
58
|
+
type: 3,
|
59
|
+
name: 'prompt',
|
60
|
+
value: prompt
|
61
|
+
}
|
62
|
+
],
|
63
|
+
application_command: {
|
64
|
+
id: '938956540159881230',
|
65
|
+
application_id: '936929561302675456',
|
66
|
+
version: '1118961510123847772',
|
67
|
+
default_member_permissions: nil,
|
68
|
+
type: 1,
|
69
|
+
nsfw: false,
|
70
|
+
name: 'imagine',
|
71
|
+
description: 'Create images with Midjourney',
|
72
|
+
dm_permission: true,
|
73
|
+
contexts: [0, 1, 2],
|
74
|
+
integration_types: [0],
|
75
|
+
options: [
|
76
|
+
{
|
77
|
+
type: 3,
|
78
|
+
name: 'prompt',
|
79
|
+
description: 'The prompt to imagine',
|
80
|
+
required: true
|
81
|
+
}
|
82
|
+
]
|
83
|
+
},
|
84
|
+
attachments: []
|
85
|
+
}
|
86
|
+
}.to_json
|
87
|
+
end
|
88
|
+
|
89
|
+
def message_payload(message)
|
90
|
+
{
|
91
|
+
content: message,
|
92
|
+
flags: 0,
|
93
|
+
mobile_network_type: 'unknown',
|
94
|
+
tts: false
|
95
|
+
}.to_json
|
96
|
+
end
|
97
|
+
|
98
|
+
def headers
|
99
|
+
@headers ||= {
|
100
|
+
'Authorization' => token,
|
101
|
+
'Accept' => 'application/json',
|
102
|
+
'Content-Type' => 'application/json'
|
103
|
+
}
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Midjourney
|
4
|
+
module API
|
5
|
+
#
|
6
|
+
# Midjourney Read Messages Module
|
7
|
+
#
|
8
|
+
module Messages
|
9
|
+
def read_messages(limit = 50)
|
10
|
+
api.read_messages(limit)
|
11
|
+
end
|
12
|
+
|
13
|
+
def send_message(message)
|
14
|
+
api.send_message(message)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -1,9 +1,13 @@
|
|
1
1
|
module Midjourney
|
2
|
+
#
|
3
|
+
# This class is used to store the configuration of the gem.
|
4
|
+
#
|
2
5
|
class Configuration
|
3
|
-
attr_accessor :
|
6
|
+
attr_accessor :discord_user_token, :discord_channel_id
|
4
7
|
|
5
8
|
def initialize
|
6
|
-
@
|
9
|
+
@discord_user_token = nil
|
10
|
+
@discord_channel_id = nil
|
7
11
|
end
|
8
12
|
end
|
9
13
|
end
|
data/lib/midjourney.rb
CHANGED
@@ -1,17 +1,44 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require 'json'
|
4
|
+
require 'pry-byebug'
|
5
|
+
require 'rainbow'
|
6
|
+
require 'rake'
|
7
|
+
require 'rest-client'
|
4
8
|
|
9
|
+
require_relative 'midjourney/configuration'
|
10
|
+
require_relative 'midjourney/api/gateway'
|
11
|
+
require_relative 'midjourney/api/imagine'
|
12
|
+
require_relative 'midjourney/api/messages'
|
13
|
+
|
14
|
+
#
|
15
|
+
# Midjourney Ruby Client
|
16
|
+
#
|
5
17
|
module Midjourney
|
6
|
-
VERSION =
|
18
|
+
VERSION = '0.2.0'
|
7
19
|
|
8
20
|
class Error < StandardError; end
|
9
21
|
|
22
|
+
extend API::Imagine
|
23
|
+
extend API::Messages
|
24
|
+
|
10
25
|
def self.configuration
|
11
|
-
|
26
|
+
@configuration ||= Configuration.new
|
12
27
|
end
|
13
28
|
|
14
29
|
def self.config
|
15
30
|
yield(configuration)
|
16
31
|
end
|
32
|
+
|
33
|
+
def self.api
|
34
|
+
@api ||= API::Gateway.new(token: discord_user_token, channel_id: discord_channel_id)
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.discord_user_token
|
38
|
+
configuration.discord_user_token
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.discord_channel_id
|
42
|
+
configuration.discord_channel_id
|
43
|
+
end
|
17
44
|
end
|
data/lib/tasks/install.rb
CHANGED
@@ -1,26 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
namespace :midjourney do
|
2
|
-
desc
|
4
|
+
desc 'Install Midjourney Ruby Client'
|
3
5
|
task install: :environment do
|
4
6
|
return :ok unless defined?(Rails)
|
5
7
|
|
6
8
|
initializer_content = <<~RUBY
|
7
9
|
# frozen_string_literal: true
|
8
10
|
|
11
|
+
#
|
12
|
+
# Take a look at https://github.com/leom806/midjourney-ruby for more information.
|
13
|
+
#
|
9
14
|
Midjourney.config do |config|
|
10
|
-
# Fetch from an environment variable
|
11
|
-
|
12
|
-
|
13
|
-
config.
|
15
|
+
# Fetch from an environment variable or fetch from Rails credentials if
|
16
|
+
# you're using Rails 5.2+ and have credentials set up.
|
17
|
+
|
18
|
+
config.discord_user_token = ENV['DISCORD_USER_TOKEN']
|
19
|
+
config.discord_channel_id = ENV['DISCORD_CHANNEL_ID']
|
14
20
|
end
|
15
21
|
RUBY
|
16
22
|
|
17
|
-
initializer_path = Rails.root.join(
|
23
|
+
initializer_path = Rails.root.join('config', 'initializers', 'midjourney.rb')
|
18
24
|
|
19
25
|
if File.exist?(initializer_path)
|
20
|
-
puts "\n
|
26
|
+
puts "\n #{Rainbow('Midjourney Ruby').purple} is already installed."
|
21
27
|
else
|
22
|
-
File.open(initializer_path,
|
23
|
-
puts "\n
|
28
|
+
File.open(initializer_path, 'w') { |f| f.write(initializer_content) }
|
29
|
+
puts "\n #{Rainbow('Midjourney Ruby').purple} has been installed! 🎉"
|
24
30
|
end
|
25
31
|
end
|
26
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: midjourney-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Leonardo Momente
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-10-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: This is a Ruby client for Midjourney that allows you to use its main
|
14
14
|
products via their Discord API.
|
@@ -19,6 +19,9 @@ extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- lib/midjourney.rb
|
22
|
+
- lib/midjourney/api/gateway.rb
|
23
|
+
- lib/midjourney/api/imagine.rb
|
24
|
+
- lib/midjourney/api/messages.rb
|
22
25
|
- lib/midjourney/configuration.rb
|
23
26
|
- lib/tasks/install.rb
|
24
27
|
homepage: https://github.com/leom806/midjourney-ruby
|