summaryrefslogtreecommitdiff
path: root/app/helpers/users_helper.rb
blob: 73704ed2d12fc264c7521232b8ab844a6422ef91 (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
54
require "open-uri"
require "rexml/document"

module UsersHelper
  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_channel)
    yt = {channel: yt_channel}
    if yt_channel.blank?
      yt[:channel] = nil
      yt[:channel_name] = nil
      yt[:is_correct?] = true
    else
      begin
        # TODO: This whole thing needs to be gone badly
        yt[:channel_name] = REXML::Document.new(open("https://www.youtube.com/feeds/videos.xml?channel_id=#{CGI.escape(yt_channel)}", :read_timeout => 1)).root.elements.find{ |n| n.name == "title" }.text
        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