Module: Plexts

Defined in:
lib/plexts.rb,
lib/plexts/plexts.rb,
lib/plexts/version.rb,
lib/plexts/entities.rb,
lib/plexts/artifacts.rb,
lib/plexts/intel_data.rb,
lib/plexts/to_console.rb,
lib/plexts/portal_details.rb

Defined Under Namespace

Classes: TermColor

Constant Summary collapse

VERSION =
"0.0.8"
ZOOM_TO_NUM_TILES_PER_EDGE =
[1,1,1,40,40,80,80,320,1000,2000,2000,4000,8000,16000,16000,32000]

Class Method Summary collapse

Class Method Details

.get_artifactsObject



3
4
5
# 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) ⇒ Object

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



7
8
9
10
11
12
13
14
# 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) ⇒ Object



16
17
18
19
20
21
# 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 = {}) ⇒ Object



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
# 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) ⇒ Object

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)



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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') ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/plexts/plexts.rb', line 3

def self.get_plexts(minLatE6, minLngE6, maxLatE6, maxLngE6, maxTimestampMs=-1, tab='all')
    body = {
        "minLatE6" => minLatE6.to_s.gsub(/\./, '').to_i,
        "minLngE6" => minLngE6.to_s.gsub(/\./, '').to_i,
        "maxLatE6" => maxLatE6.to_s.gsub(/\./, '').to_i,
        "maxLngE6" => maxLngE6.to_s.gsub(/\./, '').to_i,
        "minTimestampMs" => -1,
        "maxTimestampMs" => maxTimestampMs,
        "tab" => tab
    }
    get_intel_data('https://www.ingress.com/r/getPlexts', body)
end

.get_portal_details(guid) ⇒ Object



3
4
5
6
7
8
# 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) ⇒ Object



46
47
48
# 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) ⇒ Object



50
51
52
# 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') ⇒ Object



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
55
# 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