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
- #nsfw! ⇒ Object
- #nsfw? ⇒ Boolean
- #random ⇒ Object
- #request!(&block) ⇒ Object
- #result!(result) ⇒ Object
- #sfw! ⇒ Object
- #sfw? ⇒ Boolean
- #sfw_cookie ⇒ Object
- #tag(tag) ⇒ Object
Class Method Details
.version ⇒ Object
6 7 8 |
# File 'lib/fukung.rb', line 6 def Fukung.version '0.4.2' end |
Instance Method Details
#nsfw! ⇒ Object
53 54 55 |
# File 'lib/fukung.rb', line 53 def nsfw! @nsfw = true end |
#nsfw? ⇒ Boolean
56 57 58 59 |
# File 'lib/fukung.rb', line 56 def nsfw? @nsfw ||= nil !!@nsfw end |
#random ⇒ Object
15 16 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 |
# File 'lib/fukung.rb', line 15 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 |
#request!(&block) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/fukung.rb', line 94 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
118 119 120 |
# File 'lib/fukung.rb', line 118 def result!(result) throw(:result, result) end |
#sfw! ⇒ Object
45 46 47 |
# File 'lib/fukung.rb', line 45 def sfw! @nsfw = false end |
#sfw? ⇒ Boolean
48 49 50 51 |
# File 'lib/fukung.rb', line 48 def sfw? @nsfw ||= nil !!!@nsfw end |
#sfw_cookie ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/fukung.rb', line 61 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
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/fukung.rb', line 73 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 |