Class: PhantomProxy::PhantomJS

Inherits:
Object
  • Object
show all
Includes:
Logable
Defined in:
lib/phantom_proxy/phantomjs/phantomjs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logable

#logger, #logger=, next_id

Constructor Details

#initializePhantomJS

Returns a new instance of PhantomJS.



11
12
13
# File 'lib/phantom_proxy/phantomjs/phantomjs.rb', line 11

def initialize()
  @ready = 503
end

Instance Attribute Details

#domObject

Returns the value of attribute dom.



5
6
7
# File 'lib/phantom_proxy/phantomjs/phantomjs.rb', line 5

def dom
  @dom
end

#imageObject

Returns the value of attribute image.



6
7
8
# File 'lib/phantom_proxy/phantomjs/phantomjs.rb', line 6

def image
  @image
end

#readyObject

Returns the value of attribute ready.



7
8
9
# File 'lib/phantom_proxy/phantomjs/phantomjs.rb', line 7

def ready
  @ready
end

Instance Method Details

#getAsImageResponse(type = 'png') ⇒ Object



89
90
91
# File 'lib/phantom_proxy/phantomjs/phantomjs.rb', line 89

def getAsImageResponse(type='png')
  return "HTTP/1.0 200 OK\r\nConnection: close\r\nContent-Type: image/"+type+"\r\n\r\n"+@image;
end

#getDOMText(data) ⇒ Object



76
77
78
79
80
# File 'lib/phantom_proxy/phantomjs/phantomjs.rb', line 76

def getDOMText data
  tmp = data.split('PHANTOMJS_DOMDATA_WRITE:')[1];
  tmp = tmp.split('PHANTOMJS_DOMDATA_END')[0]
  tmp
end

#getHTTPCode(data) ⇒ Object



82
83
84
85
86
87
# File 'lib/phantom_proxy/phantomjs/phantomjs.rb', line 82

def getHTTPCode data
  tmp = data.split('URL_ERROR_CODE: ')[1];
  tmp = tmp.split('URL_ERROR_CODE_END')[0]
  #tmp = /FAILED_LOADING_URL: (.*?)FAILED_LOADING_URL_END/.match(data)[1]
  tmp.to_i
end

#getUrl(url, pictureOnly = true, loadIFrames = true) ⇒ Object



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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/phantom_proxy/phantomjs/phantomjs.rb', line 15

def getUrl(url, pictureOnly=true, loadIFrames=true)
  logger.info("PhantomJS: "+url)
  @ready = 503
  
  pictureFile = nil
  picture = "none"
  
  loadFrames = "false"
  
  if loadIFrames
    loadFrames = "true"
  end
  
  if pictureOnly
    pictureFile = Tempfile.new(["phantom_proxy_page", ".png"])
    picture = pictureFile.path
  end
  
  url_args = ""
  url_args_ = []
  
  if /\?/.match(url)
    url_args = url.split('?')[1]
    url = url.split('?')[0]
    
    if url_args
      url_args_ = url_args.split('&')
      url_args = url_args_.join(' ')
    end
  end
  
  @dom = invokePhantomJS(PhantomProxy.script_path, [picture, loadFrames, "\""+url+"\"", url_args_.length, url_args])
  
  # logger.info("Opened page: "+ /Open page: (.*?) END/.match(@dom)[1])
  
  @ready = 503
  dom_text = "Failed to load page"
  
  if /DONE_LOADING_URL/.match(@dom)
    logger.info("LOAD_DOM_TEXT")
    dom_text = getDOMText @dom
    logger.info("LOAD_DOM_TEXT_DONE")
    if pictureOnly && File.exist?(picture) 
      logger.info("File is there")
      @image = File.open(picture, "rb")# {|f| f.read }
      pictureFile.close!
    else
      logger.info("No file to load at: "+picture)
      @image = ""
    end
    @ready = 200
  end
  if /URL_ERROR_CODE/.match(@dom)
    logger.info("LOAD_ERROR_CODE")
    @ready = getHTTPCode @dom
    logger.info("LOAD_ERROR_CODE_DONE: #{@ready}")
  end
  @dom = dom_text
  return @dom
end

#invokePhantomJS(script, args) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/phantom_proxy/phantomjs/phantomjs.rb', line 93

def invokePhantomJS(script, args)
  argString = " "+args.join(" ")
  logger.info("Call phantomJS with: "+argString)
  out = ""

  IO.popen(PhantomProxy.phantomjs_bin+" --ignore-ssl-errors=yes --web-security=false "+script+argString) {|io|
    out = io.readlines.join
  }
  #logger.info("PHANTOMJS_OUT: "+out)
  return out
end