imgur-auto-uploader 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a655fef40d7a88b05b30694730334cdaf5a8b50b
4
+ data.tar.gz: 3834b2b9f0d8e465c2e0cfc34cf45f6f3018e4c7
5
+ SHA512:
6
+ metadata.gz: 24dc2b762df29edee4434e7853634a08b1a29683f7464b4410836311439d6147b8288e94ae22544ce99b06765464f94dc37a1d339a49e422eaf6e35015420890
7
+ data.tar.gz: 05f77e9c1241d6ef6cec6958baf7d9ef94703e03c7f42ef6c92d529438f329431ec57428d120420c205c12fa6b46f2cd2914e58d0c348c7d04321c56e5a1b201
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), *%w{ .. lib })
4
+
5
+ require 'imgur_up'
6
+ require 'mercenary'
7
+
8
+ imgur_up = ImgurUp::ImgurUp.new("~/.imgur-auto-uploader")
9
+
10
+ Mercenary.program("imgur-up") do |p|
11
+ p.description 'Imgur Auto-Uploader watches folders and uploads new images to Imgur.'
12
+ p.version ImgurUp::VERSION
13
+ p.syntax 'imgur-up <subcommand> [options]'
14
+
15
+ p.define_singleton_method(:command_with_development_mode) do |name, &block|
16
+ p.command(name) do |c|
17
+ c.option 'dev', '--dev', 'Run in development mode.'
18
+
19
+ c.action do |_, options|
20
+ imgur_up = ImgurUp::ImgurUp.new("~/.imgur-auto-uploader-dev") if options["dev"]
21
+ end
22
+
23
+ block.call(c)
24
+ end
25
+ end
26
+
27
+ p.command_with_development_mode(:config) do |c|
28
+ c.syntax 'config'
29
+ c.description 'Configure Imgur Auto-Uploader. This command needs to be run at least once.'
30
+
31
+ c.action do
32
+ imgur_up.prompt_for_configuration
33
+ end
34
+ end
35
+
36
+ p.command_with_development_mode(:watch) do |c|
37
+ c.syntax 'watch PATH1 [PATH2 [PATH3 ...]]'
38
+ c.description 'Watches PATHs for new images and upload them to Imgur.'
39
+
40
+ c.action do |args|
41
+ imgur_up.watch(args)
42
+
43
+ Signal.trap("INT") { exit 0 }
44
+ Signal.trap("TERM") { exit 0 }
45
+
46
+ ImgurUp.logger.info "Ready."
47
+ sleep
48
+ end
49
+ end
50
+
51
+ p.action do |args|
52
+ puts p if args.empty? || !p.has_command?(args.first)
53
+ end
54
+ end
@@ -0,0 +1,17 @@
1
+ require 'imgur_up/imgur'
2
+ require 'imgur_up/imgur_up'
3
+ require 'imgur_up/version'
4
+ require 'logger'
5
+
6
+ module ImgurUp
7
+ def self.logger
8
+ @logger ||=
9
+ if File.directory?(File.expand_path("~/Library/Logs"))
10
+ Logger.new(File.expand_path("~/Library/Logs/com.yihangho.imgur-auto-uploader.log"))
11
+ elsif File.directory?("/var/log")
12
+ Logger.new("/var/log/com.yihangho.imgur-auto-uploader.log")
13
+ else
14
+ Logger.new(STDOUT)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,50 @@
1
+ require 'rest_client'
2
+ require 'json'
3
+
4
+ module ImgurUp
5
+ class Imgur
6
+ def initialize(client_id, client_secret, refresh_token = nil)
7
+ @client_id = client_id
8
+ @client_secret = client_secret
9
+ @refresh_token = refresh_token
10
+ end
11
+
12
+ def pin_request_url
13
+ "https://api.imgur.com/oauth2/authorize?client_id=#{@client_id}&response_type=pin"
14
+ end
15
+
16
+ def authorize(pin)
17
+ url = "https://api.imgur.com/oauth2/token"
18
+ response = RestClient.post(url, client_id: @client_id, client_secret: @client_secret, grant_type: "pin", pin: pin)
19
+ output = JSON.parse(response)
20
+
21
+ @access_token = output["access_token"]
22
+ @refresh_token = output["refresh_token"]
23
+
24
+ output
25
+ end
26
+
27
+ def albums
28
+ url = "https://api.imgur.com/3/account/me/albums"
29
+ response = RestClient.get(url, Authorization: "Bearer #{access_token}")
30
+
31
+ JSON.parse(response)["data"]
32
+ end
33
+
34
+ def upload(image, album)
35
+ path = "https://api.imgur.com/3/image"
36
+
37
+ ::ImgurUp.logger.info "Uploading #{image}"
38
+ response = RestClient.post(path, {image: File.open(image, "rb"), album: album}, Authorization: "Bearer #{access_token}")
39
+ JSON.parse(response)["data"]
40
+ end
41
+
42
+ def access_token
43
+ return @access_token if @access_token
44
+
45
+ url = "https://api.imgur.com/oauth2/token"
46
+ response = RestClient.post(url, client_id: @client_id, client_secret: @client_secret, grant_type: "refresh_token", refresh_token: @refresh_token)
47
+ @access_token = JSON.parse(response)["access_token"]
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,118 @@
1
+ require 'listen'
2
+ require 'clipboard'
3
+ require 'terminal-notifier'
4
+
5
+ module ImgurUp
6
+ class ImgurUp
7
+ def initialize(config_path)
8
+ @config_path = File.expand_path(config_path)
9
+ end
10
+
11
+ def needs_configuration?
12
+ %w(client_id client_secret refresh_token album).any? do |key|
13
+ config[key].nil?
14
+ end
15
+ end
16
+
17
+ def prompt_for_configuration
18
+ config["client_id"] = prompt("Enter your client ID", config["client_id"])
19
+ config["client_secret"] = prompt("Enter your client secret", config["client_secret"])
20
+
21
+ self.imgur = Imgur.new(config["client_id"], config["client_secret"])
22
+
23
+ puts "Log on to the following URL to obtain a PIN."
24
+ puts imgur.pin_request_url
25
+ pin = prompt("Enter PIN")
26
+ authorization = imgur.authorize(pin)
27
+ config["refresh_token"] = authorization["refresh_token"]
28
+
29
+ puts "Your albums:"
30
+ albums = imgur.albums
31
+ albums.each_with_index do |album, index|
32
+ puts "#{index+1}: #{album["title"]}"
33
+ end
34
+
35
+ default_index = albums.find_index { |album| album["id"] == config["album"] }
36
+ album_index =
37
+ if default_index.nil?
38
+ prompt("Select the album to upload new files to").to_i - 1
39
+ else
40
+ prompt("Select the album to upload new files to", default_index + 1).to_i - 1
41
+ end
42
+ config["album"] = albums[album_index]["id"]
43
+
44
+ save_config
45
+ end
46
+
47
+ def watch(directories)
48
+ directories = directories.map { |dir| File.expand_path(dir) }
49
+ directories.each do |dir|
50
+ ::ImgurUp.logger.info "Listening to #{dir}"
51
+ end
52
+
53
+ listener = Listen.to(directories, only: /\.(?:jpg|png|gif)$/i) do |_, added, _|
54
+ threads = added.map do |path|
55
+ ::ImgurUp.logger.info "File added: #{path}"
56
+
57
+ Thread.new(path) do |path|
58
+ response = imgur.upload(path, config["album"])
59
+ ::ImgurUp.logger.info "Link for #{path}: #{response["link"]}"
60
+
61
+ response["link"]
62
+ end
63
+ end
64
+
65
+ threads.each do |thread|
66
+ link = thread.value
67
+
68
+ Clipboard.copy(link)
69
+ TerminalNotifier.notify("File uploaded, link copied.")
70
+ end
71
+ end
72
+ listener.start
73
+ end
74
+
75
+ private
76
+
77
+ def config
78
+ return @config if @config
79
+
80
+ @config =
81
+ if File.file?(@config_path)
82
+ begin
83
+ JSON.load(File.open(@config_path))
84
+ rescue
85
+ {}
86
+ end
87
+ else
88
+ {}
89
+ end
90
+ end
91
+
92
+ def save_config
93
+ JSON.dump(config, File.open(@config_path, "w"))
94
+ end
95
+
96
+ def imgur
97
+ @imgur ||= Imgur.new(config["client_id"], config["client_secret"], config["refresh_token"])
98
+ end
99
+
100
+ attr_writer :imgur
101
+
102
+ def prompt(message, default = nil)
103
+ if default.nil?
104
+ print "#{message}: "
105
+ else
106
+ print "#{message} (#{default}): "
107
+ end
108
+
109
+ output = STDIN.gets.strip
110
+
111
+ if output.empty? && !default.nil?
112
+ default
113
+ else
114
+ output
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,3 @@
1
+ module ImgurUp
2
+ VERSION = '0.0.3'
3
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: imgur-auto-uploader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Yihang Ho
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: clipboard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: listen
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.8.5
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.8.5
41
+ - !ruby/object:Gem::Dependency
42
+ name: mercenary
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.3.5
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.3.5
55
+ - !ruby/object:Gem::Dependency
56
+ name: rest-client
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.7.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.7.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: terminal-notifier
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.6.2
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.6.2
83
+ description:
84
+ email:
85
+ executables:
86
+ - imgur-up
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - bin/imgur-up
91
+ - lib/imgur_up.rb
92
+ - lib/imgur_up/imgur.rb
93
+ - lib/imgur_up/imgur_up.rb
94
+ - lib/imgur_up/version.rb
95
+ homepage: https://github.com/yihangho/Imgur-Auto-Uploader
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.4.5
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Automatically upload images to Imgur.
119
+ test_files: []