module Plexts

Constants

VERSION
ZOOM_TO_NUM_TILES_PER_EDGE

Public Class Methods

get_artifacts() click to toggle source
# File lib/plexts/artifacts.rb, line 3
def self.get_artifacts
    get_intel_data('https://www.ingress.com/r/artifacts')
end
get_entities(minLatE6, minLngE6, maxLatE6, maxLngE6, zoom=20) click to toggle source

minLatE6, minLngE6: south west point maxLatE6, maxLngE6: north east point

# File lib/plexts/entities.rb, line 7
def self.get_entities(minLatE6, minLngE6, maxLatE6, maxLngE6, zoom=20)
    if !(minLatE6.between?(-90, 90) && minLngE6.between?(-180, 180)) || !(maxLatE6.between?(-90, 90) && maxLngE6.between?(-180, 180))
        raise StandardError, "irregular parameter"
    end

    tiles = get_mercator_tiles(minLatE6, minLngE6, maxLatE6, maxLngE6, zoom)
    get_entities_from_tiles(tiles)
end
get_entities_from_tiles(tiles) click to toggle source
# File lib/plexts/entities.rb, line 16
def self.get_entities_from_tiles(tiles)
    body = {
        "tileKeys" => tiles,
    }
    get_intel_data('https://www.ingress.com/r/getEntities', body)
end
get_intel_data(url, body={}) click to toggle source
# File lib/plexts/intel_data.rb, line 8
def self.get_intel_data(url, body={})
  intel = YAML::load(File.open(File.join(Dir.pwd, 'intel.yml')))
  
  cookie = {
    'csrftoken' => intel['csrftoken'],
    'SACSID' => intel['SACSID']
  }
  headers = {
    'content-Type' => 'application/json; charset=UTF-8',
    'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36',
    'x-csrftoken' => intel['csrftoken'],
    'referer' => 'https://www.ingress.com/intel',
    'cookie' => cookie.map{|k,v|
                    "#{k}=#{v}"
                }.join('; ')
  }

  uri = URI(url)
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true
  req = Net::HTTP::Post.new(uri.path, headers)
  body[:v] = intel["v"]
  req.body = body.to_json
  res = https.request(req)

  case res
    when Net::HTTPSuccess then
      if res.body.include?('Welcome to Ingress')
        json = {error: 'SACSID incorrect'}.to_json
      else
        json = res.body
      end
    when Net::HTTPServerException, Net::HTTPForbidden then
      json = {error: 'csrftoken incorrect'}.to_json
  end
  
  JSON.parse(json)
end
get_mercator_tiles(minLatE6, minLngE6, maxLatE6, maxLngE6, zoom=17, pMinLevel=0, pMaxLevel=8, maxHealth=100) click to toggle source

parameter sample 17_7994_3544_0_8_100 17: zoom level 7994: mercator tile lan param 3544: mercator tile lng param 0: min portal level(0-8) 8: max portal level(0-8) 100: max portal health (25, 50, 75, 100)

# File lib/plexts/entities.rb, line 31
def self.get_mercator_tiles(minLatE6, minLngE6, maxLatE6, maxLngE6, zoom=17, pMinLevel=0, pMaxLevel=8, maxHealth=100)
    z = ZOOM_TO_NUM_TILES_PER_EDGE[zoom] || 32000
    lat1_tile = self.get_tile_for_lat(minLatE6, z)
    lat2_tile = self.get_tile_for_lat(maxLatE6, z)
    lng1_tile = self.get_tile_for_lng(minLngE6, z)
    lng2_tile = self.get_tile_for_lng(maxLngE6, z)
    tiles = []
    for x in lng1_tile..lng2_tile
        for y in lat2_tile..lat1_tile
            tiles.push([zoom, x, y, pMinLevel, pMaxLevel, maxHealth].join('_'))
        end
    end
    tiles
end
get_plexts(minLatE6, minLngE6, maxLatE6, maxLngE6, maxTimestampMs=-1, tab='all') click to toggle source
# File lib/plexts/plexts.rb, line 3
def self.get_plexts(minLatE6, minLngE6, maxLatE6, maxLngE6, maxTimestampMs=-1, tab='all')
    body = {
        "minLatE6" => ("%.06f" % minLatE6).gsub(/\./, '').to_i,
        "minLngE6" => ("%.06f" % minLngE6).gsub(/\./, '').to_i,
        "maxLatE6" => ("%.06f" % maxLatE6).gsub(/\./, '').to_i,
        "maxLngE6" => ("%.06f" % maxLngE6).gsub(/\./, '').to_i,
        "minTimestampMs" => -1,
        "maxTimestampMs" => maxTimestampMs,
        "tab" => tab
    }
    get_intel_data('https://www.ingress.com/r/getPlexts', body)
end
get_portal_details(guid) click to toggle source
# File lib/plexts/portal_details.rb, line 3
def self.get_portal_details(guid)
    body = {
            "guid" => guid
    }
    get_intel_data('https://www.ingress.com/r/getPortalDetails', body)
end
get_tile_for_lat(lat, z) click to toggle source
# File lib/plexts/entities.rb, line 46
def self.get_tile_for_lat(lat, z)
    ((1 - Math.log(Math.tan(lat * Math::PI / 180) + 1 / Math.cos(lat * Math::PI / 180)) / Math::PI) / 2 * z).to_i
end
get_tile_for_lng(lng, z) click to toggle source
# File lib/plexts/entities.rb, line 50
def self.get_tile_for_lng(lng, z)
    ((lng + 180) / 360 * z).to_i
end
to_console(minLatE6, maxLatE6, minLngE6, maxLngE6, maxTimestampMs=-1, tab='all') click to toggle source
# File lib/plexts/to_console.rb, line 3
def self.to_console(minLatE6, maxLatE6, minLngE6, maxLngE6, maxTimestampMs=-1, tab='all')

    json = get_plexts(minLatE6, maxLatE6, minLngE6, maxLngE6, maxTimestampMs, tab)
    
    if json.has_key?('error')
        puts json['error']
    else
        json["result"].reverse!.each do |plext| 
            s = plext[1].to_s
            s = s[0, s.length - 3] 

            print Time.at(s.to_i).strftime("%Y-%m-%d %R | ")
            text = ""
            plext[2]["plext"]["markup"].each do |markups|
                if markups[0] == "PLAYER"
                    if markups[1]["team"] == "RESISTANCE"
                        text << TermColor.blue
                    elsif markups[1]["team"] == "ENLIGHTENED"
                        text << TermColor.green
                    end
                    text << markups[1]["plain"]
                    text << TermColor.reset
                elsif markups[0] == "PORTAL" 
                    if markups[1]["team"] == "RESISTANCE"
                        text << TermColor.blue
                    else
                        text << TermColor.green
                    end
                    text << markups[1]["name"]
                    text << TermColor.reset
                    lat = markups[1]["latE6"]  / 1e6
                    lag = markups[1]["lngE6"]  / 1e6
                    text << " https://www.ingress.com/intel?ll=" + lat.to_s + "," + lag.to_s + "&z=19&pll=" + lat.to_s + "," + lag.to_s
                elsif markups[0] == "TEXT" 
                    if plext[2]["plext"]["categories"] == 4
                        text << TermColor.red
                        text << markups[1]["plain"]
                        text << TermColor.reset
                    else
                        text << markups[1]["plain"]
                    end
                elsif markups[0] == "SECURE" 
                    text << markups[1]["plain"]
                elsif markups[0] == "SENDER" 
                    text << TermColor.blue
                    text << markups[1]["plain"]
                    text << TermColor.reset
                end
            end
            puts text
        end
    end
end