-
Notifications
You must be signed in to change notification settings - Fork 0
/
lgtmin.rb
37 lines (32 loc) · 996 Bytes
/
lgtmin.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
require 'net/http'
require 'fileutils'
require 'open-uri'
require 'json'
num = ENV['num'].to_i
list = {items: []}
work_dir = Dir.pwd
FileUtils.rm_rf Dir.glob("#{work_dir}/cache/*")
1.upto(num) do |i|
id = Net::HTTP.get_response('lgtm.in', '/g')['location'].split('/').last
uri = URI.parse("https://lgtm.in/i/#{id}")
req = Net::HTTP::Get.new(uri)
req['Accept'] = 'application/json'
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
json = JSON.parse(http.request(req).body)
suffix = json['actualImageUrl'].include?(".gif")? "gif" : "jpg"
File.open("cache/#{id}.#{suffix}", 'wb') do |file|
open(json['actualImageUrl'], 'rb') do |image|
file.write(image.read)
end
end
item = {
type: "file:skipcheck",
title: "#{id}.#{suffix}",
subtitle: "Likes: #{json['likes']}, Dislikes:#{json['dislikes']}",
arg: "#{work_dir}/cache/#{id}.#{suffix}",
icon: { path:"cache/#{id}.#{suffix}" }
}
list[:items].push item
end
print list.to_json