-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimgur-oauth.rb
executable file
·81 lines (65 loc) · 2.11 KB
/
imgur-oauth.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env ruby
require 'curb'
require 'json'
require 'clipboard'
CLIENT_ID = 'xxxxxxxxxxxxxxx'
CLIENT_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
TOKEN_FILE = ENV['HOME'] + '/.imgur_token'
def auth_app
puts 'Follow the link to allow the application access to your account and enter the pin'
puts "https://api.imgur.com/oauth2/authorize?client_id=#{CLIENT_ID}&response_type=pin"
print 'Pin: '
pin = STDIN.gets.chomp
imgur = Curl.post "https://api.imgur.com/oauth2/token", {
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
grant_type: :pin,
pin: pin
}
response = JSON.parse(imgur.body_str)
abort 'Authorization failed' unless response['access_token']
tokens = {
'access_token' => response['access_token'],
'refresh_token' => response['refresh_token']
}
File.write(TOKEN_FILE, tokens.to_json)
tokens
end
def refresh_token(tokens)
imgur = Curl.post "https://api.imgur.com/oauth2/token", {
refresh_token: tokens['refresh_token'],
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
grant_type: :refresh_token
}
response = JSON.parse(imgur.body_str)
if response['access_token']
tokens['access_token'] = response['access_token']
File.write(TOKEN_FILE, tokens.to_json)
end
response['access_token']
end
def upload_image(image, access_token)
imgur = Curl::Easy.new "https://api.imgur.com/3/upload.json"
imgur.multipart_form_post = true
imgur.headers['Authorization'] = "Bearer #{access_token}"
imgur.http_post(Curl::PostField.file('image', image))
response = JSON.parse(imgur.body_str)
response['data']['link']
end
tokens = File.exists?(TOKEN_FILE) ? JSON.parse(File.read(TOKEN_FILE)) : auth_app
if Time.new - File.mtime(TOKEN_FILE) >= 3600
unless refresh_token(tokens)
system('notify-send -t 2000 "Upload error"')
exit 1
end
end
unless link = upload_image(ARGV[0], tokens['access_token'])
refresh_token(tokens) && link = upload_image(ARGV[0], tokens['access_token'])
end
if link
Clipboard.copy link
system('notify-send "Upload complete"')
else
system('notify-send "Upload error"')
end