Class: Datahen::CLI::ScraperPage

Inherits:
Thor
  • Object
show all
Defined in:
lib/datahen/cli/scraper_page.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details



6
7
8
# File 'lib/datahen/cli/scraper_page.rb', line 6

def self.banner(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 options[:job]
      client = Client::JobPage.new(options)
      puts "#{client.enqueue(options[:job], page, options)}"
    else
      client = Client::ScraperJobPage.new(options)
      puts "#{client.enqueue(scraper_name, page, options)}"
    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 options[:job]
    client = Client::JobPage.new(options)
    result = JSON.parse(client.find_content(options[:job], gid).to_s)
  else
    client = Client::ScraperJobPage.new(options)
    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 options[:job]
    client = Client::JobPage.new(options)
    result = JSON.parse(client.find_failed_content(options[:job], gid).to_s)
  else
    client = Client::ScraperJobPage.new(options)
    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 options[:job]
      client = Client::JobPage.new(options)
      puts "#{client.get_gid(options[:job], page,  options)}"
    else
      client = Client::ScraperJobPage.new(options)
      puts "#{client.get_gid(scraper_name, page, options)}"
    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 !options.key?(:gid) && !options.key?(:fetch_fail) && !options.key?(:parse_fail) && !options.key?(:status) && !options.key?(:page_type)
    puts "Must specify either a --gid, --fetch-fail, --parse-fail, --status or --page-type"
    return
  end

  if options[:job]
    client = Client::JobPage.new(options)
    puts "#{client.limbo(options[:job])}"
  else
    client = Client::ScraperJobPage.new(options)
    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 options[:job]
    client = Client::JobPage.new(options)
    json = JSON.parse(client.all(options[:job]).body)
    if json['error'] == ""
      puts "#{JSON.pretty_generate(json['data'])}"
    else 
      puts "#{JSON.pretty_generate(json['error'])}"
    end
  else
    client = Client::ScraperJobPage.new(options)
    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(options)

  query = {}
  query["order"] = options.delete(:head) if options[:head]
  query["job_type"] = "parsing" if options[:parsing]

  query["page_token"] = options.delete(:more) if options[:more]
  query["per_page"] = options.delete(:per_page) if options[:per_page]

  puts "Fetching page logs..."

  if options[:job]
    result = client.all_job_page_log(options[: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 !options.key?(:gid) && !options.key?(:fetch_fail) && !options.key?(:parse_fail) && !options.key?(:status) && !options.key?(:page_type)
    puts "Must specify either a --gid, --fetch-fail, --parse-fail, --status or --page-type"
    return
  end

  if options[:job]
    client = Client::JobPage.new(options)
    puts "#{client.refetch(options[:job])}"
  else
    client = Client::ScraperJobPage.new(options)
    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 !options.key?(:gid) && !options.key?(:fetch_fail) && !options.key?(:parse_fail) && !options.key?(:status) && !options.key?(:page_type)
    puts "Must specify either a --gid, --fetch-fail, --parse-fail, --status or --page-type"
    return
  end

  if options[:job]
    client = Client::JobPage.new(options)
    puts "#{client.reparse(options[:job])}"
  else
    client = Client::ScraperJobPage.new(options)
    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 options[:job]
    client = Client::JobPage.new(options)
    puts "#{client.find(options[:job], gid)}"
  else
    client = Client::ScraperJobPage.new(options)
    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
    options[:vars] = JSON.parse(options[:vars]) if options[:vars]
    options[:browserforge_config] = JSON.parse(options[:browserforge_config]) if options[:browserforge_config]
    
    if options[:job]
      client = Client::JobPage.new(options)
      puts "#{client.update(options[:job], gid, options)}"
    else
      client = Client::ScraperJobPage.new(options)
      puts "#{client.update(scraper_name, gid, options)}"
    end

  rescue JSON::ParserError
    if options[:vars]
      puts "Error: #{options[:vars]} on vars is not a valid JSON"
    end
  end
end