-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippets.rb
43 lines (41 loc) · 1.18 KB
/
snippets.rb
1
2
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
#!/usr/bin/env ruby
# build snippets file
#
require 'json'
snippets = {}
data = JSON.parse(File.read('scrape.json'))
data.each do |engine, db|
db['code'].each do |snippet|
snippet['ids'].each_with_index do |id, id_index|
code = snippet[id]
if code.nil?
puts "not found: #{id} in #{snippet.keys}"
next
end
code['language'].each_with_index do |lang, lang_index|
snippets[lang] ||= {}
title = snippet['title'][id_index]
shorttitle = title.downcase.gsub(/\(.*$/, '')
name = engine + ": " + title
content = snippet[id]['content']
raise 'empty content' unless content
body = content[lang_index]
body.gsub!(/secret_api_key/, '$1')
snippets[lang][name] = {
prefix: "serpapi #{engine} #{shorttitle}".strip,
body: body.split(/\n/),
description: name.strip
}
end
end
end
end
puts "#{snippets.keys.size} languages supported"
puts "#{snippets.values.first.size} snippets per language"
snippets.each do |language, snippet|
fn = "#{language}-snippets.json"
s = JSON.pretty_generate(snippet)
File.write(fn, s)
puts "save: #{fn}"
end
exit 0