weeb 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/weeb.rb +1 -0
- data/lib/weeb/api.rb +54 -0
- data/lib/weeb/api/korra.rb +13 -0
- data/lib/weeb/api/shimakaze.rb +13 -0
- data/lib/weeb/api/tama.rb +13 -0
- data/lib/weeb/api/toph.rb +147 -0
- data/lib/weeb/data.rb +177 -0
- data/lib/weeb/err.rb +60 -0
- data/lib/weeb/interfaces/client.rb +82 -0
- data/lib/weeb/interfaces/korra.rb +9 -0
- data/lib/weeb/interfaces/shimakaze.rb +9 -0
- data/lib/weeb/interfaces/tama.rb +9 -0
- data/lib/weeb/interfaces/toph.rb +144 -0
- data/lib/weeb/ver.rb +3 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9d900a7f70e3ae873dbb55e26a6f24feeef6981f
|
4
|
+
data.tar.gz: 5af3916cb1ded4612981e94edb9e99a46763e029
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5a596fd7ec93ff32b3182a89439a5576b7f82192c70c87d0515ecd6719a87a7f000ff7f9f47524c2afc883c27c8a6236c89d8aba4ae5efa49e58bcc323bb0042
|
7
|
+
data.tar.gz: f6e6029ed3a7a0cf1d7e63fb556356494ccd81196692cc5208e78bf90583e2256da9c18b1aa419d1d7a2ba29b0047c507816e5f7a622c467c1a8ee0ffa993892
|
data/lib/weeb.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'weeb/interfaces/client'
|
data/lib/weeb/api.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'weeb/err'
|
2
|
+
require 'weeb/ver'
|
3
|
+
require 'uri'
|
4
|
+
require 'json'
|
5
|
+
require 'rest-client'
|
6
|
+
|
7
|
+
module WeebSh
|
8
|
+
module API
|
9
|
+
|
10
|
+
module_function
|
11
|
+
|
12
|
+
def request(type, *attributes)
|
13
|
+
parse_json(RestClient.send(type, *attributes))
|
14
|
+
rescue RestClient::RequestEntityTooLarge
|
15
|
+
raise WeebSh::Err::TooLarge, 'Requested files are too large!'
|
16
|
+
rescue RestClient::Unauthorized => e
|
17
|
+
json = parse_json(e.response)
|
18
|
+
if json.is_a?(Hash)
|
19
|
+
raise WeebSh::Err::BadAuth, 'Token is invalid!' if json['message'] == 'Unauthorized'
|
20
|
+
end
|
21
|
+
rescue RestClient::BadRequest => e
|
22
|
+
json = parse_json(e.response)
|
23
|
+
if json.is_a?(Hash)
|
24
|
+
raise WeebSh::Err::InvalidMIME, json['message'] if json['message'].start_with? 'The mimetype' and json['message'].end_with? 'is not supported'
|
25
|
+
raise WeebSh::Err::TagExists, json['message'] if json['message'] == 'Tags existed already or had no content'
|
26
|
+
raise WeebSh::Err::ImagePrivate, json['message'] if json['message'] == 'This image is private'
|
27
|
+
raise WeebSh::Err::InvalidImage, json['message'] if json['message'] == 'No image found for your query'
|
28
|
+
end
|
29
|
+
raise e
|
30
|
+
rescue RestClient::Forbidden => e
|
31
|
+
json = parse_json(e.response)
|
32
|
+
if json.is_a?(Hash)
|
33
|
+
raise WeebSh::Err::MissingScope, json['message'] if json['message'].start_with? 'missing scope'
|
34
|
+
end
|
35
|
+
raise e
|
36
|
+
rescue RestClient::InternalServerError
|
37
|
+
raise WeebSh::Err::ServerFail, 'Server Error!'
|
38
|
+
rescue RuntimeError => e
|
39
|
+
raise e
|
40
|
+
end
|
41
|
+
|
42
|
+
def format_user_agent(user_agent)
|
43
|
+
user_agent = "#{user_agent['botname']}/#{user_agent['version']}#{"/#{user_agent['env']}" if user_agent['env']}" if user_agent.is_a?(Hash)
|
44
|
+
return nil unless user_agent.is_a?(String) && !user_agent.empty?
|
45
|
+
user_agent
|
46
|
+
end
|
47
|
+
|
48
|
+
def parse_json(raw)
|
49
|
+
JSON.parse(raw)
|
50
|
+
rescue JSON::ParserError
|
51
|
+
raw
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,147 @@
|
|
1
|
+
require 'weeb/api'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module WeebSh
|
5
|
+
module API
|
6
|
+
module Toph
|
7
|
+
BASE = '/images'.freeze
|
8
|
+
|
9
|
+
module_function
|
10
|
+
|
11
|
+
def upload(i, resource, type, hidden, tags, nsfw, source)
|
12
|
+
resource.is_a?(File) ? WeebSh::API.request(
|
13
|
+
:post,
|
14
|
+
"#{i.api_url}#{BASE}/upload",
|
15
|
+
{
|
16
|
+
'file'.to_sym => resource,
|
17
|
+
'baseType'.to_sym => type,
|
18
|
+
'hidden'.to_sym => hidden,
|
19
|
+
'tags'.to_sym => tags,
|
20
|
+
'nsfw'.to_sym => nsfw,
|
21
|
+
'source'.to_sym => source
|
22
|
+
},
|
23
|
+
{
|
24
|
+
Authorization: i.auth,
|
25
|
+
'User-Agent': i.user_agent
|
26
|
+
}
|
27
|
+
) : WeebSh::API.request(
|
28
|
+
:post,
|
29
|
+
"#{i.api_url}#{BASE}/upload",
|
30
|
+
{
|
31
|
+
url: resource,
|
32
|
+
baseType: type,
|
33
|
+
hidden: hidden,
|
34
|
+
tags: tags,
|
35
|
+
nsfw: nsfw,
|
36
|
+
source: source
|
37
|
+
}.to_json,
|
38
|
+
{
|
39
|
+
Authorization: i.auth,
|
40
|
+
'User-Agent': i.user_agent
|
41
|
+
}
|
42
|
+
)
|
43
|
+
end
|
44
|
+
|
45
|
+
def types(i, query)
|
46
|
+
WeebSh::API.request(
|
47
|
+
:get,
|
48
|
+
"#{i.api_url}#{BASE}/types?#{URI.encode_www_form(query)}",
|
49
|
+
{
|
50
|
+
Authorization: i.auth,
|
51
|
+
'User-Agent': i.user_agent
|
52
|
+
}
|
53
|
+
)
|
54
|
+
end
|
55
|
+
|
56
|
+
def tags(i, query)
|
57
|
+
WeebSh::API.request(
|
58
|
+
:get,
|
59
|
+
"#{i.api_url}#{BASE}/tags?#{URI.encode_www_form(query)}",
|
60
|
+
{
|
61
|
+
Authorization: i.auth,
|
62
|
+
'User-Agent': i.user_agent
|
63
|
+
}
|
64
|
+
)
|
65
|
+
end
|
66
|
+
|
67
|
+
def random(i, query)
|
68
|
+
WeebSh::API.request(
|
69
|
+
:get,
|
70
|
+
"#{i.api_url}#{BASE}/random?#{URI.encode_www_form(query)}",
|
71
|
+
{
|
72
|
+
Authorization: i.auth,
|
73
|
+
'User-Agent': i.user_agent
|
74
|
+
}
|
75
|
+
)
|
76
|
+
end
|
77
|
+
|
78
|
+
def image(i, id)
|
79
|
+
WeebSh::API.request(
|
80
|
+
:get,
|
81
|
+
"#{i.api_url}#{BASE}/info/#{id}",
|
82
|
+
{
|
83
|
+
Authorization: i.auth,
|
84
|
+
'User-Agent': i.user_agent
|
85
|
+
}
|
86
|
+
)
|
87
|
+
end
|
88
|
+
|
89
|
+
def add_tags_to_image(i, id, tags)
|
90
|
+
WeebSh::API.request(
|
91
|
+
:post,
|
92
|
+
"#{i.api_url}#{BASE}/info/#{id}",
|
93
|
+
{ tags: tags }.to_json,
|
94
|
+
{
|
95
|
+
Authorization: i.auth,
|
96
|
+
'User-Agent': i.user_agent
|
97
|
+
}
|
98
|
+
)
|
99
|
+
end
|
100
|
+
|
101
|
+
def remove_tags_to_image(i, id, tags)
|
102
|
+
WeebSh::API.request(
|
103
|
+
:delete,
|
104
|
+
"#{i.api_url}#{BASE}/info/#{id}",
|
105
|
+
{ tags: tags }.to_json,
|
106
|
+
{
|
107
|
+
Authorization: i.auth,
|
108
|
+
'User-Agent': i.user_agent
|
109
|
+
}
|
110
|
+
)
|
111
|
+
end
|
112
|
+
|
113
|
+
def remove_image(i, id)
|
114
|
+
WeebSh::API.request(
|
115
|
+
:delete,
|
116
|
+
"#{i.api_url}#{BASE}/info/#{id}",
|
117
|
+
{
|
118
|
+
Authorization: i.auth,
|
119
|
+
'User-Agent': i.user_agent
|
120
|
+
}
|
121
|
+
)
|
122
|
+
end
|
123
|
+
|
124
|
+
def list(i, query)
|
125
|
+
WeebSh::API.request(
|
126
|
+
:get,
|
127
|
+
"#{i.api_url}#{BASE}/list?#{URI.encode_www_form(query)}",
|
128
|
+
{
|
129
|
+
Authorization: i.auth,
|
130
|
+
'User-Agent': i.user_agent
|
131
|
+
}
|
132
|
+
)
|
133
|
+
end
|
134
|
+
|
135
|
+
def list_user(i, id, query)
|
136
|
+
WeebSh::API.request(
|
137
|
+
:get,
|
138
|
+
"#{i.api_url}#{BASE}/list/#{id}?#{URI.encode_www_form(query)}",
|
139
|
+
{
|
140
|
+
Authorization: i.auth,
|
141
|
+
'User-Agent': i.user_agent
|
142
|
+
}
|
143
|
+
)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
data/lib/weeb/data.rb
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
require 'weeb/api'
|
2
|
+
|
3
|
+
module WeebSh
|
4
|
+
# Mixin for objects with IDs
|
5
|
+
module IDObject
|
6
|
+
# @return [String] the ID which uniquely identifies this object in toph.
|
7
|
+
attr_reader :id
|
8
|
+
alias_method :resolve_id, :id
|
9
|
+
alias_method :hash, :id
|
10
|
+
|
11
|
+
# ID based comparison
|
12
|
+
def ==(other)
|
13
|
+
other.respond_to?(:resolve_id) ? (@id.resolve_id == other.resolve_id) : (@id == other)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Represents a generic interface
|
18
|
+
class Interface
|
19
|
+
# @return [String] the authorization used in weeb.sh.
|
20
|
+
attr_accessor :auth
|
21
|
+
|
22
|
+
# @return [String] the user agent used in weeb.sh.
|
23
|
+
attr_accessor :user_agent
|
24
|
+
|
25
|
+
# @return [String] the URL being used for weeb.sh.
|
26
|
+
attr_reader :api_url
|
27
|
+
|
28
|
+
# @param token [String] the token required to use weeb.sh.
|
29
|
+
# @param user_agent [String, Hash] the user agent to use on endpoints.
|
30
|
+
# @param api_url [String] the URL to use when using the weeb.sh API.
|
31
|
+
def initialize(auth, user_agent = nil, api_url: 'https://api.weeb.sh', client: nil)
|
32
|
+
@user_agent = WeebSh::API.format_user_agent(user_agent)
|
33
|
+
@auth = auth
|
34
|
+
@api_url = api_url
|
35
|
+
@client = client
|
36
|
+
|
37
|
+
return if client
|
38
|
+
raise WeebSh::Err::BadAuth, 'Authorization is empty!' if auth.empty?
|
39
|
+
puts "[#{self.class.name}] Your User Agent is empty. Please consider adding a user agent to help identify issues easier." if user_agent.nil?
|
40
|
+
puts "[#{self.class.name}] Your User Agent is not ideal. Please consider adding a user agent to help identify issues easier." if !user_agent.nil? && user_agent.split('/').count < 2
|
41
|
+
end
|
42
|
+
|
43
|
+
def inspect
|
44
|
+
"#<#{self.class.name} @api_url=#{@api_url.inspect} @user_agent=#{@user_agent.inspect}>"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Represents a preview image for toph
|
49
|
+
class PreviewImage
|
50
|
+
include IDObject
|
51
|
+
|
52
|
+
# @return [Symbol] the type of image this is
|
53
|
+
attr_reader :type
|
54
|
+
|
55
|
+
# @return [String] the file extension of the image
|
56
|
+
attr_reader :file_type
|
57
|
+
|
58
|
+
# @return [String] the url of the image
|
59
|
+
attr_reader :url
|
60
|
+
|
61
|
+
# @!visibility private
|
62
|
+
def initialize(data, interface)
|
63
|
+
@interface = interface
|
64
|
+
@id = data['id']
|
65
|
+
@type = data['type']
|
66
|
+
@file_type = data['fileType']
|
67
|
+
@url = data['url']
|
68
|
+
end
|
69
|
+
|
70
|
+
def inspect
|
71
|
+
"#<WeebSh::PreviewImage @url=#{@url.inspect} @type=#{@type.inspect}>"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# Represents an image for toph
|
76
|
+
class WeebImage < PreviewImage
|
77
|
+
|
78
|
+
# @return [true, false] whether or not this image is nsfw
|
79
|
+
attr_reader :nsfw
|
80
|
+
alias_method :nsfw?, :nsfw
|
81
|
+
|
82
|
+
# @return [String] the mime type of the image
|
83
|
+
attr_reader :mime_type
|
84
|
+
|
85
|
+
# @return [Array<String>] the tags on image
|
86
|
+
attr_reader :tags
|
87
|
+
|
88
|
+
# @return [true, false] whether or not this image can only be seen by the uploader
|
89
|
+
attr_reader :hidden
|
90
|
+
alias_method :hidden?, :hidden
|
91
|
+
|
92
|
+
# @return [String, nil] the source of the image
|
93
|
+
attr_reader :source
|
94
|
+
|
95
|
+
# @return [String] the ID of the uploader
|
96
|
+
attr_reader :account
|
97
|
+
alias_method :uploader, :account
|
98
|
+
alias_method :author, :account
|
99
|
+
|
100
|
+
# @!visibility private
|
101
|
+
def initialize(data, interface)
|
102
|
+
@interface = interface
|
103
|
+
@id = data['id']
|
104
|
+
@type = data['type']
|
105
|
+
@nsfw = data['nsfw']
|
106
|
+
@file_type = data['fileType']
|
107
|
+
@mime_type = data['mimeType']
|
108
|
+
@url = data['url']
|
109
|
+
@hidden = data['hidden']
|
110
|
+
@file_type = data['fileType']
|
111
|
+
@source = data['source'] || nil
|
112
|
+
@account = data['account']
|
113
|
+
@tags = data['tags'].map { |r| Tag.new(r, interface) }
|
114
|
+
end
|
115
|
+
|
116
|
+
# Add tags to the image
|
117
|
+
# @param tags [Array<String, Tag>] the affected tags
|
118
|
+
def add_tags(tags)
|
119
|
+
@interface.add_tags_to_image(self, tags)
|
120
|
+
end
|
121
|
+
|
122
|
+
# Remove tags from the image
|
123
|
+
# @param tags [Array<String, Tag>] the affected tags
|
124
|
+
def remove_tags(tags)
|
125
|
+
@interface.remove_tags_to_image(self, tags)
|
126
|
+
end
|
127
|
+
|
128
|
+
# Add a tag to the image
|
129
|
+
# @param tag [String, Tag] the affected tag
|
130
|
+
def add_tag(tag)
|
131
|
+
@interface.add_tags_to_image(self, [tag])
|
132
|
+
end
|
133
|
+
|
134
|
+
# Remove a tag to the image
|
135
|
+
# @param tags [String, Tag] the affected tag
|
136
|
+
def remove_tag(tag)
|
137
|
+
@interface.remove_tags_to_image(self, [tag])
|
138
|
+
end
|
139
|
+
|
140
|
+
# Delete this image
|
141
|
+
def delete()
|
142
|
+
@interface.delete_image(self)
|
143
|
+
end
|
144
|
+
alias_method :remove, :delete
|
145
|
+
|
146
|
+
def inspect
|
147
|
+
"#<WeebSh::WeebImage @url=#{@url.inspect} @type=#{@type.inspect} @nsfw=#{@nsfw.inspect}>"
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
# Represents an Image tag for toph
|
152
|
+
class Tag
|
153
|
+
# @return [String] the name of the tag
|
154
|
+
attr_reader :name
|
155
|
+
|
156
|
+
# @return [true, false] whether or not this tag can only be seen by the uploader
|
157
|
+
attr_reader :hidden
|
158
|
+
alias_method :hidden?, :hidden
|
159
|
+
|
160
|
+
# @return [String] the ID of the creator
|
161
|
+
attr_reader :account
|
162
|
+
alias_method :creator, :account
|
163
|
+
alias_method :author, :account
|
164
|
+
|
165
|
+
# @!visibility private
|
166
|
+
def initialize(data, interface)
|
167
|
+
@interface = interface
|
168
|
+
@name = data['name']
|
169
|
+
@hidden = data['hidden']
|
170
|
+
@account = data['account']
|
171
|
+
end
|
172
|
+
|
173
|
+
def inspect
|
174
|
+
"#<WeebSh::Tag @name=#{@name.inspect} @hidden=#{@hidden.inspect} @account=#{@account.inspect}>"
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
data/lib/weeb/err.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
module WeebSh
|
2
|
+
# Custom errors raised in various places
|
3
|
+
module Err
|
4
|
+
# Raised when a token is invalid or incorrect.
|
5
|
+
class BadAuth < RuntimeError
|
6
|
+
def message
|
7
|
+
'Token invalid'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
# Raised when too many files were uploaded.
|
12
|
+
class MissingScope < RuntimeError
|
13
|
+
def message
|
14
|
+
'Too many files requested!'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Raised when a server error occurs
|
19
|
+
class ServerFail < RuntimeError
|
20
|
+
def message
|
21
|
+
'Server tried to do a thing and borked!'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Raised when requested file(s) have too much bytes.
|
26
|
+
class TooLarge < RuntimeError
|
27
|
+
def message
|
28
|
+
'File(s) are too large!'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Raised when an invalid mime type was given.
|
33
|
+
class InvalidMIME < RuntimeError
|
34
|
+
def message
|
35
|
+
'You gave an unsupported MIME type!'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Raised when trying to add an existing tag to an image
|
40
|
+
class TagExists < RuntimeError
|
41
|
+
def message
|
42
|
+
'Tags existed already or had no content!'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Raised when trying to interact with a non-existant image
|
47
|
+
class InvalidImage < RuntimeError
|
48
|
+
def message
|
49
|
+
'Non-existant image!'
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# Raised when loading a file recieves an error
|
54
|
+
class FileError < RuntimeError
|
55
|
+
def message
|
56
|
+
'Unknown'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'weeb/interfaces/toph'
|
2
|
+
require 'weeb/interfaces/korra'
|
3
|
+
require 'weeb/interfaces/shimakaze'
|
4
|
+
require 'weeb/interfaces/tama'
|
5
|
+
|
6
|
+
module WeebSh
|
7
|
+
class Client
|
8
|
+
# @return [String] the authorization used in weeb.sh.
|
9
|
+
attr_reader :auth
|
10
|
+
|
11
|
+
# @return [String] the user agent used in weeb.sh.
|
12
|
+
attr_reader :user_agent
|
13
|
+
|
14
|
+
# @return [String] the URL being used for weeb.sh.
|
15
|
+
attr_reader :api_url
|
16
|
+
|
17
|
+
# @return [Topc] the toph class tied with the client.
|
18
|
+
attr_reader :toph
|
19
|
+
alias_method :image, :toph
|
20
|
+
alias_method :images, :toph
|
21
|
+
|
22
|
+
# @return [String] the korra class tied with the client.
|
23
|
+
attr_reader :korra
|
24
|
+
alias_method :image_gen, :korra
|
25
|
+
alias_method :auto_image, :korra
|
26
|
+
|
27
|
+
# @return [String] the shimakaze class tied with the client.
|
28
|
+
attr_reader :shimakaze
|
29
|
+
alias_method :reputation, :shimakaze
|
30
|
+
alias_method :rep, :shimakaze
|
31
|
+
|
32
|
+
# @return [String] the tama class tied with the client.
|
33
|
+
attr_reader :tama
|
34
|
+
alias_method :settings, :tama
|
35
|
+
|
36
|
+
# Groups all interfaces into one.
|
37
|
+
# @param token [String] the token required to use weeb.sh.
|
38
|
+
# @param user_agent [String, Hash] the user agent to use with requests.
|
39
|
+
def initialize(auth, user_agent = nil, api_url: 'https://api.weeb.sh')
|
40
|
+
@user_agent = WeebSh::API.format_user_agent(user_agent)
|
41
|
+
@auth = auth
|
42
|
+
@api_url = api_url
|
43
|
+
|
44
|
+
raise WeebSh::Err::BadAuth, 'Authorization is empty!' if auth.empty?
|
45
|
+
puts '[WeebSh::Client] Your User Agent is empty. Please consider adding a user agent to help identify issues easier.' if user_agent.nil?
|
46
|
+
puts '[WeebSh::Client] Your User Agent is not ideal. Please consider adding a user agent to help identify issues easier.' if !user_agent.nil? && user_agent.split('/').count < 2
|
47
|
+
|
48
|
+
@toph = WeebSh::Toph.new(auth, user_agent, api_url: api_url, client: self)
|
49
|
+
@korra = WeebSh::Korra.new(auth, user_agent, api_url: api_url, client: self)
|
50
|
+
@shimakaze = WeebSh::Shimakaze.new(auth, user_agent, api_url: api_url, client: self)
|
51
|
+
@tama = WeebSh::Tama.new(auth, user_agent, api_url: api_url, client: self)
|
52
|
+
end
|
53
|
+
|
54
|
+
def user_agent=(user_agent)
|
55
|
+
@user_agent = user_agent
|
56
|
+
@toph.user_agent = user_agent
|
57
|
+
@korra.user_agent = user_agent
|
58
|
+
@shimakaze.user_agent = user_agent
|
59
|
+
@tama.user_agent = user_agent
|
60
|
+
end
|
61
|
+
|
62
|
+
def auth=(auth)
|
63
|
+
@auth = auth
|
64
|
+
@toph.auth = auth
|
65
|
+
@korra.auth = auth
|
66
|
+
@shimakaze.auth = auth
|
67
|
+
@tama.auth = auth
|
68
|
+
end
|
69
|
+
|
70
|
+
def api_url=(api_url)
|
71
|
+
@api_url = api_url
|
72
|
+
@toph.api_url = api_url
|
73
|
+
@korra.api_url = api_url
|
74
|
+
@shimakaze.api_url = api_url
|
75
|
+
@tama.api_url = api_url
|
76
|
+
end
|
77
|
+
|
78
|
+
def inspect
|
79
|
+
"#<WeebSh::Client @api_url=#{@api_url.inspect} @user_agent=#{@user_agent.inspect}>"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,144 @@
|
|
1
|
+
require 'weeb/api/toph'
|
2
|
+
require 'weeb/data'
|
3
|
+
|
4
|
+
module WeebSh
|
5
|
+
# Image API
|
6
|
+
class Toph < Interface
|
7
|
+
|
8
|
+
# Uploads an image.
|
9
|
+
# @param resource [File, String] the file to upload, this can either be a File, a URL or a path.
|
10
|
+
# @param type [String] the type of image this is in.
|
11
|
+
# @param hidden [true, false] whether or not this image will be only seen to the uploader.
|
12
|
+
# @param tags [Array<String, Tag>] the tags that this image applies to.
|
13
|
+
# @param nsfw [true, false] whether or not this image is NSFW.
|
14
|
+
# @param source [String] the source URL of the image
|
15
|
+
# @return [Image] Returns image that was uploaded.
|
16
|
+
def upload(resource, type, hidden: false, tags: [], nsfw: false, source: nil)
|
17
|
+
tags = tags.map { |t| t.name if t.is_a(Tag) }
|
18
|
+
if resource.is_a?(String) and URI(resource).host.nil?
|
19
|
+
begin
|
20
|
+
resource = File.new(File.absolute_path(resource), 'rb')
|
21
|
+
rescue Errno::ENOENT, Errno::EACCES, Errno::ENAMETOOLONG => e
|
22
|
+
errstring = 'Unknown'
|
23
|
+
case e.class.name
|
24
|
+
when 'Errno::ENOENT'
|
25
|
+
errstring = 'File Not Found'
|
26
|
+
when 'Errno::EACCES'
|
27
|
+
errstring = 'Permission Denied'
|
28
|
+
when 'Errno::ENAMETOOLONG'
|
29
|
+
errstring = 'Name Too Long'
|
30
|
+
end
|
31
|
+
raise WeebSh::Err::FileError, "Error initializing file '${resource}' | #{errstring} | #{e.class.name}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Image.new(API::Toph.upload(self, resource, type, hidden, tags, nsfw, source)['file'], self)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Gets a list of available types of images.
|
39
|
+
# @param preview [true, false] whether or not to return an array ofpreview images instead of and array of strings
|
40
|
+
# @param hidden [true, false] if true, you only get back hidden preview images you uploaded
|
41
|
+
# @param nsfw [true, false, String] whether or not preview images could be NSFW. Setting this to "only" restricts it to NSFW images.
|
42
|
+
# @return [Array<String>, Array<PreviewImage>] Returns all available types.
|
43
|
+
def types(hidden: false, nsfw: false, preview: false)
|
44
|
+
response = API::Toph.types(self, {
|
45
|
+
hidden: hidden,
|
46
|
+
nsfw: nsfw,
|
47
|
+
preview: preview
|
48
|
+
})
|
49
|
+
preview ? response['preview'].map { |p| PreviewImage.new(p, self) } : response['types']
|
50
|
+
end
|
51
|
+
|
52
|
+
# Gets a list of available types of images.
|
53
|
+
# @param hidden [true, false] if true, you only get back hidden tags you added
|
54
|
+
# @param nsfw [true, false, String] whether or not preview images could be NSFW. Setting this to "only" restricts it to NSFW images.
|
55
|
+
# @return [Array<String>, Array<PreviewImage>] Returns all available tags.
|
56
|
+
def tags(hidden: false, nsfw: false)
|
57
|
+
API::Toph.types(self, {
|
58
|
+
hidden: hidden,
|
59
|
+
nsfw: nsfw
|
60
|
+
})['tags']
|
61
|
+
end
|
62
|
+
|
63
|
+
# Get a random image based on type or a set of tags.
|
64
|
+
# @param type [String] the type of images to recieve
|
65
|
+
# @param tags [Array<String, Tag>] the tags a image should require to be for selection
|
66
|
+
# @param hidden [true, false] if true, you can only get back hidden tags you added
|
67
|
+
# @param nsfw [true, false, String] whether or not preview images could be NSFW. Setting this to "only" restricts it to NSFW images.
|
68
|
+
# @param file_type [String] the file type an image is required to be for selection
|
69
|
+
# @return [WeebImage] Returns image that was randomly selected.
|
70
|
+
def random(type: nil, tags: [], hidden: false, nsfw: false, file_type: nil)
|
71
|
+
tags = tags.map { |t| t.name if t.is_a(Tag) }
|
72
|
+
WeebImage.new(API::Toph.random(self, {
|
73
|
+
type: type,
|
74
|
+
tags: tags,
|
75
|
+
hidden: hidden,
|
76
|
+
nsfw: nsfw,
|
77
|
+
file_type: file_type
|
78
|
+
}), self)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Get an image.
|
82
|
+
# @param id [String, WeebImage, PreviewImage, #resolve_id] the image's ID to recieve
|
83
|
+
# @return [WeebImage] Returns the image with the given ID.
|
84
|
+
def image(id)
|
85
|
+
id = id.resolve_id if id.respond_to?(:resolve_id)
|
86
|
+
WeebImage.new(API::Toph.image(self, id), self)
|
87
|
+
end
|
88
|
+
|
89
|
+
# Delete an image.
|
90
|
+
# @param id [String, WeebImage, PreviewImage, #resolve_id] the image's ID to delete
|
91
|
+
# @return [WeebImage] Returns the image with the given ID.
|
92
|
+
def delete_image(id)
|
93
|
+
id = id.resolve_id if id.respond_to?(:resolve_id)
|
94
|
+
WeebImage.new(API::Toph.delete_image(self, id), self)
|
95
|
+
end
|
96
|
+
alias_method :remove_image, :delete_image
|
97
|
+
|
98
|
+
# Add tags to an image.
|
99
|
+
# @param image [String, WeebImage, PreviewImage, #resolve_id] the image being referred
|
100
|
+
# @param tags [Array<String, Tag>] the affected tags
|
101
|
+
# @return [WeebImage] Returns the image with the given ID.
|
102
|
+
def add_tags_to_image(image, tags)
|
103
|
+
id = id.resolve_id if id.respond_to?(:resolve_id)
|
104
|
+
tags = tags.map { |t| t.name if t.is_a(Tag) }
|
105
|
+
WeebImage.new(API::Toph.add_tags_to_image(self, image, tags), self)
|
106
|
+
end
|
107
|
+
|
108
|
+
# Remove tags off of an image.
|
109
|
+
# @param image [String, WeebImage, PreviewImage, #resolve_id] the image being referred
|
110
|
+
# @param tags [Array<String, Tag>] the affected tags
|
111
|
+
# @return [WeebImage] Returns the image with the given ID.
|
112
|
+
def remove_tags_to_image(image, tags)
|
113
|
+
id = id.resolve_id if id.respond_to?(:resolve_id)
|
114
|
+
tags = tags.map { |t| t.name if t.is_a(Tag) }
|
115
|
+
WeebImage.new(API::Toph.remove_tags_to_image(self, image, tags), self)
|
116
|
+
end
|
117
|
+
|
118
|
+
# Get a list of images based on arguments.
|
119
|
+
# @param type [String] the type of images to recieve
|
120
|
+
# @param tags [Array<String, Tag>] the tags a image should require to be for selection
|
121
|
+
# @param hidden [true, false] if true, you can only get back hidden tags you added
|
122
|
+
# @param nsfw [true, false, String] whether or not preview images could be NSFW. Setting this to "only" restricts it to NSFW images.
|
123
|
+
# @param file_type [String] the file type an image is required to be for selection
|
124
|
+
# @param page [Number] the page number of images to use
|
125
|
+
# @param account [String] the account ID to get images from
|
126
|
+
# @return [WeebImage] Returns image that was randomly selected.
|
127
|
+
def list(type: nil, tags: [], hidden: false, nsfw: false, file_type: nil, page: 0, account: nil)
|
128
|
+
tags = tags.map { |t| t.name if t.is_a(Tag) }
|
129
|
+
object = {
|
130
|
+
type: type,
|
131
|
+
tags: tags,
|
132
|
+
hidden: hidden,
|
133
|
+
nsfw: nsfw,
|
134
|
+
file_type: file_type,
|
135
|
+
page: page
|
136
|
+
}
|
137
|
+
repsonse = account ? API::Toph.list_user(self, account, object) : API::Toph.list(self, object)
|
138
|
+
repsonse.map { |i| WeebImage.new(i, self) }
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
# Alias for the class {Toph}
|
143
|
+
Image = Toph
|
144
|
+
end
|
data/lib/weeb/ver.rb
ADDED
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: weeb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Snazzah
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
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: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
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: rest-client
|
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
|
+
description: A gem that utilizes the weeb.sh API.
|
56
|
+
email: suggesttosnazzy@gmail.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- lib/weeb.rb
|
62
|
+
- lib/weeb/api.rb
|
63
|
+
- lib/weeb/api/korra.rb
|
64
|
+
- lib/weeb/api/shimakaze.rb
|
65
|
+
- lib/weeb/api/tama.rb
|
66
|
+
- lib/weeb/api/toph.rb
|
67
|
+
- lib/weeb/data.rb
|
68
|
+
- lib/weeb/err.rb
|
69
|
+
- lib/weeb/interfaces/client.rb
|
70
|
+
- lib/weeb/interfaces/korra.rb
|
71
|
+
- lib/weeb/interfaces/shimakaze.rb
|
72
|
+
- lib/weeb/interfaces/tama.rb
|
73
|
+
- lib/weeb/interfaces/toph.rb
|
74
|
+
- lib/weeb/ver.rb
|
75
|
+
homepage: https://github.com/Snazzah/weeb.rb
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.5.1
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: A gem that utilizes the weeb.sh API.
|
99
|
+
test_files: []
|