Module: Fukung
Constant Summary collapse
- Host =
'fukung.net'
- MediaHost =
'media.fukung.net'
- Debug =
ENV['FUKUNG_DEBUG']
- Nothing =
Object.new.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #goto(a_tag = false) ⇒ Object
- #goto_one(a_tag = false) ⇒ Object
- #nsfw! ⇒ Object
- #nsfw? ⇒ Boolean
- #one(a_tag = false) ⇒ Object
- #random ⇒ Object
- #random_or_tag(a_tag = false) ⇒ Object
- #request!(&block) ⇒ Object
- #result!(result) ⇒ Object
- #sfw! ⇒ Object
- #sfw? ⇒ Boolean
- #sfw_cookie ⇒ Object
- #tag(tag) ⇒ Object
Class Method Details
.version ⇒ Object
7 8 9 |
# File 'lib/fukung.rb', line 7 def Fukung.version '1.1.0' end |
Instance Method Details
#goto(a_tag = false) ⇒ Object
134 135 136 |
# File 'lib/fukung.rb', line 134 def goto(a_tag = false) random_or_tag(a_tag).each { |url| ::Launchy.open( url ) } end |
#goto_one(a_tag = false) ⇒ Object
138 139 140 |
# File 'lib/fukung.rb', line 138 def goto_one(a_tag = false) ::Launchy.open(one(a_tag)) end |
#nsfw! ⇒ Object
55 56 57 |
# File 'lib/fukung.rb', line 55 def nsfw! @nsfw = true end |
#nsfw? ⇒ Boolean
58 59 60 61 |
# File 'lib/fukung.rb', line 58 def nsfw? @nsfw ||= nil !!@nsfw end |
#one(a_tag = false) ⇒ Object
130 131 132 |
# File 'lib/fukung.rb', line 130 def one(a_tag = false) random_or_tag(a_tag).sort_by{ rand }.first end |
#random ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/fukung.rb', line 17 def random @random ||= nil retries = 0 begin location = request! do |http| headers = {} headers['Cookie'] = if sfw? response = http.get('/random', headers) result! response['Location'] end path = location.gsub(%r|^/v|, '') raise if path.strip.empty? raise if path.strip.downcase=='random' path = "http://" + "#{ MediaHost }/images/#{ path }".squeeze('/') raise if path=="http://#{ MediaHost }/images/random" @random = path return @random rescue if retries < 42 retries += 1 retry end raise unless @random @random end end |
#random_or_tag(a_tag = false) ⇒ Object
124 125 126 127 128 |
# File 'lib/fukung.rb', line 124 def random_or_tag(a_tag = false) list = a_tag ? Fukung.tag(a_tag.to_s) : [Fukung.random] raise "nothing found" if list.empty? return list end |
#request!(&block) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/fukung.rb', line 96 def request!(&block) error = nil result = catch(:result) do 4.times do |i| begin http = Net::HTTP::new(Host) http.set_debug_output(STDERR) if Debug http.start do block.call(http) if block end rescue Object => e error = e sleep rand end end Nothing end raise(error || 'unknown error') if result == Nothing return(result) end |
#result!(result) ⇒ Object
120 121 122 |
# File 'lib/fukung.rb', line 120 def result!(result) throw(:result, result) end |
#sfw! ⇒ Object
47 48 49 |
# File 'lib/fukung.rb', line 47 def sfw! @nsfw = false end |
#sfw? ⇒ Boolean
50 51 52 53 |
# File 'lib/fukung.rb', line 50 def sfw? @nsfw ||= nil !!!@nsfw end |
#sfw_cookie ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/fukung.rb', line 63 def return @sfw_cookie if defined?(@sfw_cookie) @sfw_cookie = request! do |http| response = http.get('/actions/toggleSFW.php') = response['Set-Cookie'] = .to_s.split(/;/).first + ';' result!() end end |
#tag(tag) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/fukung.rb', line 75 def tag(tag) page = 1 max = 100 imgs = [] loop do body = request! do |http| path = "/tag/#{ CGI.escape(tag) }/page,#{ page }" response = http.get(path) result! response.body end uris = URI.extract(body, 'http').select{|uri| uri =~ /#{ MediaHost }/} break if uris.empty? uris.map!{|uri| uri.sub!('thumbs', 'images')} imgs.push(*uris) break if page > max page += 1 end imgs.flatten.compact.uniq end |