Module: Fukung

Extended by:
Fukung
Included in:
Fukung
Defined in:
lib/fukung.rb

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

Class Method Details

.versionObject



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

Returns:

  • (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

#randomObject



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'] = sfw_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

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/fukung.rb', line 50

def sfw?
  @nsfw ||= nil 
  !!!@nsfw
end


63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fukung.rb', line 63

def sfw_cookie
  return @sfw_cookie if defined?(@sfw_cookie)

  @sfw_cookie =
    request! do |http|
      response = http.get('/actions/toggleSFW.php')
      set_cookie = response['Set-Cookie']
      sfw_cookie = set_cookie.to_s.split(/;/).first + ';'
      result!(sfw_cookie)
    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