summaryrefslogtreecommitdiff
path: root/app/helpers/users_helper.rb
blob: 93067e8101e1e03a0e35aa78ce8f93459577e745 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
44
45
46
47
48
49
50
51
52
53
module UsersHelper
require "open-uri"

  def mentions(content)
    users = []
    words = content.scan(/@[a-zA-Z0-9_]{1,16}/)
    words.each do |w|
      user = User.find_by_ign(w[1..-1])
      users << user if user && user.normal? && user.confirmed? && user.mail_mention?
    end
    users.uniq
  end

  def get_youtube(yt_name)
    yt = {channel: yt_name}
    if yt_name.blank?
      yt[:channel] = nil
      yt[:channel_name] = nil
      yt[:is_correct?] = true
    else
      begin
        yt[:channel_name] = JSON.parse(open("https://gdata.youtube.com/feeds/api/users/#{CGI.escape(yt_name)}?alt=json", :read_timeout => 1).read)["entry"]["title"]["$t"]
        yt[:is_correct?] = true
      rescue
        yt[:is_correct?] = false
      end
    end
    yt
  end

  def fetch_name(uuid)
    uri  = URI.parse("https://api.mojang.com/user/profiles/#{CGI.escape(uuid)}/names")
    http = Net::HTTP.new(uri.host, uri.port)
    http.open_timeout = 3
    http.read_timeout = 3
    http.use_ssl = true

    begin
      response = http.get(uri)
      if response.code == "200"
        data = JSON.load(response.body)
        return data.last["name"]
      end
    rescue => e
      Rails.logger.error "----"
      Rails.logger.error "Failed to get mojang profile for #{uuid}"
      Rails.logger.error e.message
      Rails.logger.error "----"
      return nil
    end
  end

end