-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2 Added command random for lgtm.in fetching
- Loading branch information
Showing
8 changed files
with
162 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,8 +11,8 @@ Gem::Specification.new do |spec| | |
spec.authors = ["Huy Dinh"] | ||
spec.email = ["[email protected]"] | ||
|
||
spec.summary = %q{Generating LGTM image to clipboard.} | ||
spec.description = %q{Generating images from user input with LGTM text on it, or fetching images from LGTM.in based on user's query. Finally put the image to clipboard.} | ||
spec.summary = %q{Generate LGTM image from a source image and copy to clipboard.} | ||
spec.description = %q{Generate LGTM image from URL or file with smart text colors and positions. Support direct clipboard paste to github's comment box or Slack on MacOSX.} | ||
spec.homepage = "http://github.com/phradion/lgtm_hd" | ||
spec.license = "MIT" | ||
|
||
|
@@ -34,7 +34,9 @@ Gem::Specification.new do |spec| | |
spec.add_development_dependency "rake", "~> 10.0" | ||
spec.add_development_dependency "rspec", "3.6.0" | ||
spec.add_development_dependency "simplecov" | ||
spec.add_development_dependency "pry" | ||
spec.add_development_dependency "codeclimate-test-reporter", "~> 1.0.0" | ||
spec.add_runtime_dependency "open_uri_redirections" | ||
spec.add_runtime_dependency "commander", "4.4.3" | ||
spec.add_runtime_dependency "clipboard", "1.1.1" | ||
spec.add_runtime_dependency "os", "1.0.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
require "lgtm_hd/version" | ||
require "lgtm_hd/configuration" | ||
require "lgtm_hd/utilities" | ||
require "lgtm_hd/lgtmdotin" | ||
require "lgtm_hd/meme_generator" | ||
require "lgtm_hd/cli" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,14 @@ module LgtmHD | |
module Configuration | ||
# Program configurations | ||
PROGRAM_NAME = "lgtm_hd".freeze | ||
AUTHOR = "Huy Dinh <[email protected]>".freeze | ||
DESCRIPTION = "Generating images from user input with LGTM text on it, or fetching images from LGTM.in based on user's query. Finally put the image to clipboard.".freeze | ||
|
||
# Output Image configurations | ||
OUTPUT_PATH_DEFAULT = "/tmp".freeze | ||
if (File.exist?("~/Desktop")) | ||
OUTPUT_PATH_DEFAULT = File.expand("~/Desktop").freeze | ||
end | ||
OUTPUT_PREFIX = "lgtm_hd_".freeze | ||
OUTPUT_MAX_WIDTH = 500.freeze | ||
OUTPUT_MAX_HEIGHT = 500.freeze | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require 'resolv-replace' | ||
require 'open-uri' | ||
|
||
module LgtmHD | ||
class LgtmDotIn | ||
API_STARTING_ENDPOINT = "http://www.lgtm.in/g".freeze | ||
ACTUAL_IMAGE_URL_USE_SSL = false | ||
TRY_FETCHING_IMAGE_LIMIT = 3 | ||
TRY_FETCHING_META_LIMIT = 3 | ||
|
||
def self.fetch_random_image(dest_path = nil, file_prefix = nil) | ||
# LGTM.in has so many broken images | ||
# So we loop until a good image is found | ||
limit = TRY_FETCHING_IMAGE_LIMIT | ||
begin | ||
json_data = fetch_meta_data | ||
image_url = json_data["actualImageUrl"] | ||
image_markdown = json_data["markdown"] | ||
yield image_url, image_markdown | ||
|
||
# fetching image data | ||
dest_file = File.join(dest_path ||= '/tmp', (file_prefix ||= 'lgtmdotin_') + File.extname(image_url)) | ||
uri = URI.parse(image_url) | ||
|
||
uri.open(redirect: false, ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE) do |input_stream| | ||
File.open(dest_file, 'wb') do |output_stream| | ||
IO.copy_stream(input_stream, output_stream) | ||
end | ||
end | ||
[dest_file, image_markdown] | ||
rescue OpenURI::HTTPError, SocketError, Net::ReadTimeout => error | ||
retry if (limit -= 1) > 0 | ||
raise error, "We have tried 3 times but all images are broken. Either LGTM.in is trash or you are super unlucky" | ||
end | ||
end | ||
|
||
|
||
private | ||
def self.fetch_meta_data(uri = API_STARTING_ENDPOINT, limit = TRY_FETCHING_META_LIMIT) | ||
uri = URI.parse(API_STARTING_ENDPOINT) | ||
## | ||
# LGTM.in has a JSON endpoint that | ||
# .forwards to an SSL HTTP address that | ||
# .has no valid SSL certificate | ||
# Hence the loop | ||
# | ||
begin | ||
data = uri.open('Accept' => 'application/json', redirect: false, ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE) | ||
json = JSON.parse data.readlines.join("") | ||
rescue OpenURI::HTTPRedirect => redirect | ||
uri = redirect.uri # assigned from the "Location" response header | ||
retry if (limit -= 1) > 0 | ||
raise IOError, "There maybe a network issue. The program failed to contact LGTM.in JSON API" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
require 'open-uri' | ||
require 'net/http' | ||
|
||
module LgtmHD | ||
def self.gem_root | ||
File.expand_path '../../..', __FILE__ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module LgtmHD | ||
VERSION = "0.1.4".freeze | ||
VERSION = "0.1.5".freeze | ||
end |