Class: Datahen::CLI::ScraperPage
- Inherits:
-
Thor
- Object
- Thor
- Datahen::CLI::ScraperPage
- Defined in:
- lib/datahen/cli/scraper_page.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add(scraper_name, page_json) ⇒ Object
- #content(scraper_name, gid) ⇒ Object
- #failedcontent(scraper_name, gid) ⇒ Object
- #getgid(scraper_name, page_json) ⇒ Object
- #limbo(scraper_name) ⇒ 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/datahen/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, page_json) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/datahen/cli/scraper_page.rb', line 50 def add(scraper_name, page_json) begin page = JSON.parse(page_json) if [:job] client = Client::JobPage.new() puts "#{client.enqueue([:job], page, )}" else client = Client::ScraperJobPage.new() puts "#{client.enqueue(scraper_name, page, )}" end rescue JSON::ParserError puts "Error: Invalid JSON" end end |
#content(scraper_name, gid) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/datahen/cli/scraper_page.rb', line 262 def content(scraper_name, gid) result = nil if [:job] client = Client::JobPage.new() result = JSON.parse(client.find_content([:job], gid).to_s) else client = Client::ScraperJobPage.new() result = JSON.parse(client.find_content(scraper_name, gid).to_s) end if result['available'] == true puts "Preview content url: \"#{result['preview_url']}\"" begin `open "#{result['preview_url']}"` rescue end else puts "Content does not exist" end end |
#failedcontent(scraper_name, gid) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/datahen/cli/scraper_page.rb', line 285 def failedcontent(scraper_name, gid) result = nil if [:job] client = Client::JobPage.new() result = JSON.parse(client.find_failed_content([:job], gid).to_s) else client = Client::ScraperJobPage.new() result = JSON.parse(client.find_failed_content(scraper_name, gid).to_s) end if result['available'] == true puts "Preview failed content url: \"#{result['preview_url']}\"" begin `open "#{result['preview_url']}"` rescue end else puts "Failed Content does not exist" end end |
#getgid(scraper_name, page_json) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/datahen/cli/scraper_page.rb', line 73 def getgid(scraper_name, page_json) begin page = JSON.parse(page_json) if [:job] client = Client::JobPage.new() puts "#{client.get_gid([:job], page, )}" else client = Client::ScraperJobPage.new() puts "#{client.get_gid(scraper_name, page, )}" end rescue JSON::ParserError puts "Error: Invalid JSON" end end |
#limbo(scraper_name) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/datahen/cli/scraper_page.rb', line 187 def limbo(scraper_name) if !.key?(:gid) && !.key?(:fetch_fail) && !.key?(:parse_fail) && !.key?(:status) && !.key?(:page_type) puts "Must specify either a --gid, --fetch-fail, --parse-fail, --status or --page-type" return end if [:job] client = Client::JobPage.new() puts "#{client.limbo([:job])}" else client = Client::ScraperJobPage.new() puts "#{client.limbo(scraper_name)}" end end |
#list(scraper_name) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/datahen/cli/scraper_page.rb', line 25 def list(scraper_name) if [:job] client = Client::JobPage.new() json = JSON.parse(client.all([:job]).body) if json['error'] == "" puts "#{JSON.pretty_generate(json['data'])}" else puts "#{JSON.pretty_generate(json['error'])}" end else client = Client::ScraperJobPage.new() json = JSON.parse(client.all(scraper_name).body) if json['error'] == "" puts "#{JSON.pretty_generate(json['data'])}" else puts "#{JSON.pretty_generate(json['error'])}" end end end |
#log(scraper_name, gid) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/datahen/cli/scraper_page.rb', line 226 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
137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/datahen/cli/scraper_page.rb', line 137 def refetch(scraper_name) if !.key?(:gid) && !.key?(:fetch_fail) && !.key?(:parse_fail) && !.key?(:status) && !.key?(:page_type) puts "Must specify either a --gid, --fetch-fail, --parse-fail, --status or --page-type" return end if [:job] client = Client::JobPage.new() puts "#{client.refetch([:job])}" else client = Client::ScraperJobPage.new() puts "#{client.refetch(scraper_name)}" end end |
#reparse(scraper_name) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/datahen/cli/scraper_page.rb', line 162 def reparse(scraper_name) if !.key?(:gid) && !.key?(:fetch_fail) && !.key?(:parse_fail) && !.key?(:status) && !.key?(:page_type) puts "Must specify either a --gid, --fetch-fail, --parse-fail, --status or --page-type" return end if [:job] client = Client::JobPage.new() puts "#{client.reparse([:job])}" else client = Client::ScraperJobPage.new() puts "#{client.reparse(scraper_name)}" end end |
#show(scraper_name, gid) ⇒ Object
207 208 209 210 211 212 213 214 215 |
# File 'lib/datahen/cli/scraper_page.rb', line 207 def show(scraper_name, gid) if [:job] client = Client::JobPage.new() puts "#{client.find([:job], gid)}" else client = Client::ScraperJobPage.new() puts "#{client.find(scraper_name, gid)}" end end |
#update(scraper_name, gid) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/datahen/cli/scraper_page.rb', line 107 def update(scraper_name, gid) begin [:vars] = JSON.parse([:vars]) if [:vars] [:browserforge_config] = JSON.parse([:browserforge_config]) if [:browserforge_config] if [:job] client = Client::JobPage.new() puts "#{client.update([:job], gid, )}" else client = Client::ScraperJobPage.new() puts "#{client.update(scraper_name, gid, )}" end rescue JSON::ParserError if [:vars] puts "Error: #{[:vars]} on vars is not a valid JSON" end end end |