Class: AnswersEngine::CLI::ScraperPage
- Inherits:
-
Thor
- Object
- Thor
- AnswersEngine::CLI::ScraperPage
- Defined in:
- lib/answersengine/cli/scraper_page.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add(scraper_name, url) ⇒ Object
- #list(scraper_name) ⇒ Object
- #log(scraper_name, gid) ⇒ Object
- #refetch(scraper_name) ⇒ Object
- #reparse(scraper_name) ⇒ Object
- #show(scraper_name, gid) ⇒ Object
- #update(scraper_name, gid) ⇒ Object
Class Method Details
.banner(command, namespace = nil, subcommand = false) ⇒ Object
6 7 8 |
# File 'lib/answersengine/cli/scraper_page.rb', line 6 def self.(command, namespace = nil, subcommand = false) "#{basename} #{@package_name} #{command.usage}" end |
Instance Method Details
#add(scraper_name, url) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/answersengine/cli/scraper_page.rb', line 47 def add(scraper_name, url) begin [:headers] = JSON.parse([:headers]) if [:headers] [:vars] = JSON.parse([:vars]) if [:vars] method = [:method] if [:job] client = Client::JobPage.new() puts "#{client.enqueue(options[:job], method, url, options)}" else client = Client::ScraperJobPage.new() puts "#{client.enqueue(scraper_name, method, url, options)}" end rescue JSON::ParserError if [:headers] puts "Error: #{options[:headers]} on headers is not a valid JSON" end if [:vars] puts "Error: #{options[:vars]} on vars is not a valid JSON" end end end |
#list(scraper_name) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/answersengine/cli/scraper_page.rb', line 20 def list(scraper_name) if [:job] client = Client::JobPage.new() puts "#{client.all(options[:job])}" else client = Client::ScraperJobPage.new() puts "#{client.all(scraper_name)}" end end |
#log(scraper_name, gid) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/answersengine/cli/scraper_page.rb', line 166 def log(scraper_name, gid) client = Client::JobLog.new() query = {} query["order"] = .delete(:head) if [:head] query["job_type"] = "parsing" if [:parsing] query["page_token"] = .delete(:more) if [:more] query["per_page"] = .delete(:per_page) if [:per_page] puts "Fetching page logs..." if [:job] result = client.all_job_page_log([:job], gid, {query: query}) else result = client.scraper_all_job_page_log(scraper_name, gid, {query: query}) end if result['entries'].nil? || result["entries"].length == 0 puts "No logs yet, please try again later." else more_token = result["more_token"] result["entries"].each do |entry| puts "#{entry["timestamp"]} #{entry["severity"]}: #{entry["payload"]}" if entry.is_a?(Hash) end unless more_token.nil? puts "to see more entries, add: \"--more #{more_token}\"" end end end |
#refetch(scraper_name) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/answersengine/cli/scraper_page.rb', line 107 def refetch(scraper_name) if !.key?(:gid) && !.key?(:fetch_fail) && !.key?(:parse_fail) && !.key?(:status) puts "Must specify either a --gid, --fetch-fail, --parse-fail or --status" return end client = Client::ScraperJobPage.new() puts "#{client.refetch(scraper_name)}" end |
#reparse(scraper_name) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/answersengine/cli/scraper_page.rb', line 123 def reparse(scraper_name) begin [:vars] = JSON.parse([:vars]) if [:vars] if !.key?(:gid) && !.key?(:parse_fail) && !.key?(:status) puts "Must specify either a --gid, --parse-fail or --status" return end client = Client::ScraperJobPage.new() puts "#{client.reparse(scraper_name)}" rescue JSON::ParserError if [:vars] puts "Error: #{options[:vars]} on vars is not a valid JSON" end end end |
#show(scraper_name, gid) ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'lib/answersengine/cli/scraper_page.rb', line 147 def show(scraper_name, gid) if [:job] client = Client::JobPage.new() puts "#{client.find(options[:job], gid)}" else client = Client::ScraperJobPage.new() puts "#{client.find(scraper_name, gid)}" end end |
#update(scraper_name, gid) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/answersengine/cli/scraper_page.rb', line 80 def update(scraper_name, gid) begin [:vars] = JSON.parse([:vars]) if [:vars] if [:job] client = Client::JobPage.new() puts "#{client.update(options[:job], gid, options)}" else client = Client::ScraperJobPage.new() puts "#{client.update(scraper_name, gid, options)}" end rescue JSON::ParserError if [:vars] puts "Error: #{options[:vars]} on vars is not a valid JSON" end end end |