rubyhexagon 0.4.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,100 +0,0 @@
1
- =begin
2
- Copyright 2014, 2015, 2017 Maxine Michalski <maxine@furfind.net>
3
-
4
- This file is part of rubyhexagon.
5
-
6
- rubyhexagon is free software: you can redistribute it and/or modify
7
- it under the terms of the GNU General Public License as published by
8
- the Free Software Foundation, either version 3 of the License, or
9
- (at your option) any later version.
10
-
11
- rubyhexagon is distributed in the hope that it will be useful,
12
- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- GNU General Public License for more details.
15
-
16
- You should have received a copy of the GNU General Public License
17
- along with rubyhexagon. If not, see <http://www.gnu.org/licenses/>.
18
- =end
19
-
20
- require "post"
21
- require "thread"
22
-
23
- module E621
24
- class Search
25
- # commands update/download
26
- def initialize(query,page=1)
27
- @api, @queue = API.new, SizedQueue.new(4096)
28
- @request = {limit:100,page:page,tags:query.url_encode}
29
- end
30
-
31
- def next_page
32
- @request[:page] += 1
33
- end
34
-
35
- def previous_page
36
- @request[:page] -= 1
37
- end
38
- # Return each post via a block and process them individually.
39
- # This could cause a race condition in the unlikely manner when the queue is
40
- # empty and not being filled again, but the worker is still runnning.
41
- def each_post(before_id=false)
42
- if before_id then
43
- fetch_all_before_id(before_id)
44
- else
45
- fetch_all
46
- end
47
- until !@worker.status do
48
- until @queue.empty? do
49
- post = @queue.pop
50
- yield post
51
- end
52
- end
53
- end
54
-
55
- def posts
56
- fetch
57
- end
58
-
59
- def page
60
- return @request[:page]
61
- end
62
-
63
- def page=(page)
64
- @request[:page] = page
65
- end
66
-
67
- private
68
- def fetch
69
- @api.post_list(@request).map do |post|
70
- Post.new(post)
71
- end.compact
72
- end
73
- def fetch_all
74
- @worker = Thread.new do
75
- until (posts = @api.post_list(@request)) == Array.new do
76
- posts.each do |post|
77
- post = Post.new(post)
78
- @queue << post if post
79
- end
80
- next_page
81
- end
82
- end
83
- end
84
- def fetch_all_before_id(id)
85
- if @request.has_key?(:page) then
86
- @request.delete(:page)
87
- @request.store(:before_id, id+1)
88
- end
89
- @worker = Thread.new do
90
- until (posts = @api.post_list(@request)) == Array.new do
91
- posts.each do |post|
92
- post = Post.new(post)
93
- @queue << post if post
94
- end
95
- @request[:before_id] = posts.last['id'].to_i
96
- end
97
- end
98
- end
99
- end
100
- end
data/lib/set.rb DELETED
@@ -1,59 +0,0 @@
1
- =begin
2
- Copyright 2014, 2015, 2017 Maxine Michalski <maxine@furfind.net>
3
-
4
- This file is part of rubyhexagon.
5
-
6
- rubyhexagon is free software: you can redistribute it and/or modify
7
- it under the terms of the GNU General Public License as published by
8
- the Free Software Foundation, either version 3 of the License, or
9
- (at your option) any later version.
10
-
11
- rubyhexagon is distributed in the hope that it will be useful,
12
- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- GNU General Public License for more details.
15
-
16
- You should have received a copy of the GNU General Public License
17
- along with rubyhexagon. If not, see <http://www.gnu.org/licenses/>.
18
- =end
19
-
20
- module E621
21
- class Set < Container
22
- def initialize(post)
23
- @api = API.new("set")
24
- if post.is_a?(Hash) then
25
- set_variables(post)
26
- elsif post.is_a?(Fixnum) then
27
- set_variables(@api.get("show",{"id"=>post}))
28
- else
29
- raise ArgumentError, "Parameter must be Hash or Number."
30
- end
31
- end
32
-
33
- def add_post(id)
34
- @api.post("add_post",{"post_id"=>id,"set_id"=>@id})
35
- end
36
-
37
- def keys
38
- return instance_variables.map{|i|i.to_s.sub("@","")}
39
- end
40
-
41
- def name=(name)
42
- @name = name
43
- end
44
-
45
- def to_json
46
- json_hash = Hash.new
47
- instance_variables.each do |i|
48
- json_hash.store(i.to_s.sub("@",""),instance_variable_get(i))
49
- end
50
- return json_hash.to_json
51
- end
52
-
53
- def each_post
54
- @posts.each do |post|
55
- yield post
56
- end
57
- end
58
- end
59
- end
@@ -1,24 +0,0 @@
1
- =begin
2
- Copyright 2014, 2015, 2017 Maxine Michalski <maxine@furfind.net>
3
-
4
- This file is part of rubyhexagon.
5
-
6
- rubyhexagon is free software: you can redistribute it and/or modify
7
- it under the terms of the GNU General Public License as published by
8
- the Free Software Foundation, either version 3 of the License, or
9
- (at your option) any later version.
10
-
11
- rubyhexagon is distributed in the hope that it will be useful,
12
- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- GNU General Public License for more details.
15
-
16
- You should have received a copy of the GNU General Public License
17
- along with rubyhexagon. If not, see <http://www.gnu.org/licenses/>.
18
- =end
19
-
20
- module E621
21
- # This is just a set of classes to better differenciate between common errors.
22
- class APIError < StandardError; end
23
- class AuthentificationError < StandardError; end
24
- end
@@ -1,29 +0,0 @@
1
- =begin
2
- Copyright 2014, 2015, 2017 Maxine Michalski <maxine@furfind.net>
3
-
4
- This file is part of rubyhexagon.
5
-
6
- rubyhexagon is free software: you can redistribute it and/or modify
7
- it under the terms of the GNU General Public License as published by
8
- the Free Software Foundation, either version 3 of the License, or
9
- (at your option) any later version.
10
-
11
- rubyhexagon is distributed in the hope that it will be useful,
12
- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- GNU General Public License for more details.
15
-
16
- You should have received a copy of the GNU General Public License
17
- along with rubyhexagon. If not, see <http://www.gnu.org/licenses/>.
18
- =end
19
-
20
- class Hash
21
- def method_missing(m,args=nil)
22
- m = m.to_s
23
- if self.has_key?(m) then
24
- return a[m]
25
- else
26
- raise NoMethodError, "undefined method #{m} for #{self.class}"
27
- end
28
- end
29
- end
@@ -1,92 +0,0 @@
1
- =begin
2
- Copyright 2014, 2015, 2017 Maxine Michalski <maxine@furfind.net>
3
-
4
- This file is part of rubyhexagon.
5
-
6
- rubyhexagon is free software: you can redistribute it and/or modify
7
- it under the terms of the GNU General Public License as published by
8
- the Free Software Foundation, either version 3 of the License, or
9
- (at your option) any later version.
10
-
11
- rubyhexagon is distributed in the hope that it will be useful,
12
- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- GNU General Public License for more details.
15
-
16
- You should have received a copy of the GNU General Public License
17
- along with rubyhexagon. If not, see <http://www.gnu.org/licenses/>.
18
- =end
19
-
20
- require "net/http"
21
-
22
- module E621
23
- class HTTP
24
- def initialize(host="e621.net",port=443)
25
- @http = Net::HTTP.new(host,port)
26
- @http.use_ssl = true if port == 443
27
- end
28
- def method_missing(method, *args)
29
- if [:get,:post].include?(method) then
30
- tries,url,request = 0, *args
31
- begin
32
- response = case method
33
- when :get then @http.get("#{url}?#{request}")
34
- when :post then @http.post(url,request)
35
- end
36
- rescue
37
- sleep 2**tries
38
- tries += 1
39
- raise if tries >= 8
40
- retry
41
- end
42
- response = parse_body(response)
43
- else
44
- raise NoMethodError, "undefined method #{method} for #{self.class}"
45
- end
46
- return response
47
- end
48
- private
49
- # As every body gets processed the same, just put it into a seperate method.
50
- def parse_body(resp)
51
- begin
52
- if resp["content-type"].sub(/;.+/,"") == "application/json" then
53
- body = resp.body.parse
54
- else
55
- {"body"=>resp.body}
56
- end
57
- rescue
58
- body = errorcode(resp.code,url) # Emulate a proper API response!
59
- end
60
- end
61
- # Parse all code that returns not in JSON to actual JSON like hashes.
62
- def errorcode(code,url)
63
- body = {"success"=>false,"reason"=>""}
64
- body["reason"] = if code.match(/3\d{2}/) then
65
- "We got redirected!"
66
- elsif code.match(/403/) then
67
- "Access denied!"
68
- elsif code.match(/404/) then
69
- "File not found!"
70
- elsif code.match(/420/) then
71
- "Record could not be saved!"
72
- elsif code.match(/421/) then
73
- "User is throttled, try again later!"
74
- elsif code.match(/422/) then
75
- "The resource is locked!"
76
- elsif code.match(/423/) then
77
- "Resource already exists!"
78
- elsif code.match(/4\d{2}/) then
79
- "We made a bad request!"
80
- elsif code.match(/500/) then
81
- "Internal server error!"
82
- elsif code.match(/503/) then
83
- "Server has too much load!"
84
- elsif code.match(/5\d{2}/) then
85
- "Some unkown server side error!"
86
- else
87
- "HTTP response code out of range!!"
88
- end
89
- return body
90
- end
91
- end
92
- end
@@ -1,36 +0,0 @@
1
- =begin
2
- Copyright 2014, 2015, 2017 Maxine Michalski <maxine@furfind.net>
3
-
4
- This file is part of rubyhexagon.
5
-
6
- rubyhexagon is free software: you can redistribute it and/or modify
7
- it under the terms of the GNU General Public License as published by
8
- the Free Software Foundation, either version 3 of the License, or
9
- (at your option) any later version.
10
-
11
- rubyhexagon is distributed in the hope that it will be useful,
12
- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- GNU General Public License for more details.
15
-
16
- You should have received a copy of the GNU General Public License
17
- along with rubyhexagon. If not, see <http://www.gnu.org/licenses/>.
18
- =end
19
-
20
- class Fixnum
21
- def to_kb
22
- (self/1024.0).round(2)
23
- end
24
-
25
- def to_mb
26
- (self/(1024*1024.0)).round(2)
27
- end
28
-
29
- def days
30
- 60*60*24*self
31
- end
32
-
33
- def pad(c,s=0)
34
- "#{s}"*(c-self.to_s.length)+self.to_s
35
- end
36
- end
@@ -1,91 +0,0 @@
1
- =begin
2
- Copyright 2014, 2015, 2017 Maxine Michalski <maxine@furfind.net>
3
-
4
- This file is part of rubyhexagon.
5
-
6
- rubyhexagon is free software: you can redistribute it and/or modify
7
- it under the terms of the GNU General Public License as published by
8
- the Free Software Foundation, either version 3 of the License, or
9
- (at your option) any later version.
10
-
11
- rubyhexagon is distributed in the hope that it will be useful,
12
- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- GNU General Public License for more details.
15
-
16
- You should have received a copy of the GNU General Public License
17
- along with rubyhexagon. If not, see <http://www.gnu.org/licenses/>.
18
- =end
19
-
20
- require "json"
21
- require "cgi"
22
-
23
- class String
24
- # Make json parsing a lot easier and less to write.
25
- def parse
26
- JSON.parser.new(self).parse
27
- end
28
- def to_ascii
29
- self.gsub("_"," ").encode("us-ascii", :invalid => :replace, :undef => :replace, :replace => "")
30
- end
31
- def url_encode
32
- CGI.escape(self)
33
- end
34
- # Remove all tags inside of a string.
35
- def clean
36
- self.gsub(/<.+?>/,"")
37
- end
38
- # Trim or expand a string to a certain length.
39
- def pad(c,s=" ")
40
- if self.length > c then
41
- self[0,c-3]+"..."
42
- elsif self.length < c then
43
- self+s.to_s*(c-self.length)
44
- else
45
- self
46
- end
47
- end
48
- # Make a string bold and colorful, with colors a user can understand. Not
49
- # those cryptic numbers.
50
- def bold(color=nil)
51
- c = case color
52
- when "black" then "\e[30;1m"
53
- when "red" then "\e[31;1m"
54
- when "green" then "\e[32;1m"
55
- when "yellow" then "\e[33;1m"
56
- when "blue" then "\e[34;1m"
57
- when "purple" then "\e[35;1m"
58
- when "cyan" then "\e[36;1m"
59
- else "\e[37;1m"
60
- end
61
- c+self+"\e[0m"
62
- end
63
- # Same as bold, but just make a string colorful.
64
- def color(color=nil)
65
- c = case color
66
- when "black" then "\e[30;0m"
67
- when "red" then "\e[31;0m"
68
- when "green" then "\e[32;0m"
69
- when "yellow" then "\e[33;0m"
70
- when "blue" then "\e[34;0m"
71
- when "purple" then "\e[35;0m"
72
- when "cyan" then "\e[36;0m"
73
- else "\e[37;0m"
74
- end
75
- c+self+"\e[0m"
76
- end
77
- # indent a string and all lines that belong to it
78
- def indent(indent)
79
- s = self.split("#$/")
80
- " "*indent+s.join("#$/"+" "*indent)
81
- end
82
- # It is just convenient to have a custom to_bool function. Only "true" gets
83
- # turned into true, anything else is false.
84
- def to_bool
85
- if self.downcase == "true" then
86
- true
87
- else
88
- false
89
- end
90
- end
91
- end
@@ -1,30 +0,0 @@
1
- =begin
2
- Copyright 2014, 2015, 2017 Maxine Michalski <maxine@furfind.net>
3
-
4
- This file is part of rubyhexagon.
5
-
6
- rubyhexagon is free software: you can redistribute it and/or modify
7
- it under the terms of the GNU General Public License as published by
8
- the Free Software Foundation, either version 3 of the License, or
9
- (at your option) any later version.
10
-
11
- rubyhexagon is distributed in the hope that it will be useful,
12
- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- GNU General Public License for more details.
15
-
16
- You should have received a copy of the GNU General Public License
17
- along with rubyhexagon. If not, see <http://www.gnu.org/licenses/>.
18
- =end
19
- class Time
20
- # A general to_s function for Time objects.
21
- def to_s
22
- self.strftime("%b %e,%Y %I:%M:%S %p")
23
- end
24
- # This methods takes a block and returns how long it took to execute that block.
25
- def self.measure
26
- s = Time.now
27
- yield
28
- return Time.now-s
29
- end
30
- end