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
  '2.0.0'
end

Instance Method Details

#goto(a_tag = false) ⇒ Object



136
137
138
# File 'lib/fukung.rb', line 136

def goto(a_tag = false)
  random_or_tag(a_tag).each { |url| ::Launchy.open( url ) }
end

#goto_one(a_tag = false) ⇒ Object



140
141
142
# File 'lib/fukung.rb', line 140

def goto_one(a_tag = false)
  ::Launchy.open(one(a_tag))
end

#nsfw!Object



57
58
59
# File 'lib/fukung.rb', line 57

def nsfw!
  @nsfw = true
end

#nsfw?Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/fukung.rb', line 60

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

#one(a_tag = false) ⇒ Object



132
133
134
# File 'lib/fukung.rb', line 132

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
46
47
# 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

    basename = File.basename(location)

    #path = location.gsub(%r|^/v|, '')
    #raise if path.strip.empty?
    #raise if path.strip.downcase=='random'
    path = "http://" + "#{ MediaHost }/imgs/#{ basename }".squeeze('/')
    raise if path=="http://#{ MediaHost }/imgs/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



126
127
128
129
130
# File 'lib/fukung.rb', line 126

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fukung.rb', line 98

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



122
123
124
# File 'lib/fukung.rb', line 122

def result!(result)
  throw(:result, result)
end

#sfw!Object



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

def sfw!
  @nsfw = false
end

#sfw?Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/fukung.rb', line 52

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


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

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fukung.rb', line 77

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