Skip to content

Commit

Permalink
Add option_parser
Browse files Browse the repository at this point in the history
  • Loading branch information
zw963 committed Sep 17, 2022
1 parent 8003b6f commit cf274ab
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# myip

返回本机的公网 IP 以及访问外网的 IP,使用 http:/www.ip111.cn 提供的服务。
返回本机的公网 IP 以及访问外网的公网 IP,使用 http:/www.ip111.cn 以及 http://www.ip138.com 提供的服务。

```sh
╰─ $ bin/myip
Expand Down
112 changes: 79 additions & 33 deletions src/myip.cr
Original file line number Diff line number Diff line change
@@ -1,52 +1,98 @@
require "crystagiri"
require "./myip/*"
require "option_parser"

chan = Channel(Tuple(String, String)).new

spawn do
doc = Crystagiri::HTML.from_url "http://www.ip138.com", follow: true
ip138_url = doc.at_css("iframe").not_nil!.node.attributes["src"].content
doc = Crystagiri::HTML.from_url "http:#{ip138_url}"
def get_ip_from_ip138(chan)
spawn do
doc = Crystagiri::HTML.from_url "http://www.ip138.com", follow: true
ip138_url = doc.at_css("iframe").not_nil!.node.attributes["src"].content
doc = Crystagiri::HTML.from_url "http:#{ip138_url}"

chan.send({"ip138.com:", doc.at_css("body p").not_nil!.content.strip})
rescue Socket::Error | OpenSSL::SSL::Error
STDERR.puts "visit http://www.ip138.com failed, please check internet connection."
exit
chan.send({"ip138.com:", doc.at_css("body p").not_nil!.content.strip})
rescue Socket::Error | OpenSSL::SSL::Error
STDERR.puts "visit http://www.ip138.com failed, please check internet connection."
exit
end
end

begin
doc = Crystagiri::HTML.from_url "http://www.ip111.cn"
def get_ip_from_ip111(chan)
begin
doc = Crystagiri::HTML.from_url "http://www.ip111.cn"

iframe = doc.where_tag("iframe") do |tag|
spawn do
url = tag.node.attributes["src"].content
ip = Crystagiri::HTML.from_url(url).at_css("body").not_nil!.content
title = tag.node.parent.try(&.parent).try(&.parent).not_nil!.xpath_node("div[@class='card-header']").not_nil!.content.strip
iframe = doc.where_tag("iframe") do |tag|
spawn do
url = tag.node.attributes["src"].content
ip = Crystagiri::HTML.from_url(url).at_css("body").not_nil!.content
title = tag.node.parent.try(&.parent).try(&.parent).not_nil!.xpath_node("div[@class='card-header']").not_nil!.content.strip

chan.send({"ip111.cn:#{title}", ip})
rescue OpenSSL::SSL::Error
STDERR.puts "visit #{url} failed"
exit
chan.send({"ip111.cn:#{title}", ip})
rescue OpenSSL::SSL::Error
STDERR.puts "visit #{url} failed"
exit
end
end

{doc, iframe.size}
rescue Socket::Error | OpenSSL::SSL::Error
STDERR.puts "visit http://getip.pub failed, please check internet connection."
exit
end
rescue Socket::Error | OpenSSL::SSL::Error
STDERR.puts "visit http://getip.pub failed, please check internet connection."
exit
end

title = doc.at_css(".card-header").not_nil!.content.strip
ip = doc.at_css(".card-body p").not_nil!.content.strip
def output(chan, size)
end

at_exit do
output_i138 = false

STDERR.puts "ip111.cn:#{title}#{ip}"
OptionParser.parse do |parser|
parser.banner = <<-USAGE
Usage: myip <option>
USAGE

3.times do |i|
select
when value = chan.receive
title, ip = value
parser.on("-l", "--location", "Use ip138.com to get more accurate ip location information.") do
get_ip_from_ip138(chan)
output_i138 = true
end

STDERR.puts "#{title}#{ip}"
when timeout 5.seconds
STDERR.puts "Timeout!"
exit
parser.on("-h", "--help", "Show this help message and exit") do
puts parser
exit
end

parser.invalid_option do |flag|
STDERR.puts "Invalid option: #{flag}.\n\n"
STDERR.puts parser
exit 1
end

parser.missing_option do |flag|
STDERR.puts "Missing option for #{flag}\n\n"
STDERR.puts parser
exit 1
end
end

doc, iframe_size = get_ip_from_ip111(chan)

title = doc.at_css(".card-header").not_nil!.content.strip
ip = doc.at_css(".card-body p").not_nil!.content.strip

STDERR.puts "ip111.cn:#{title}#{ip}"

size = output_i138 ? iframe_size + 1 : iframe_size

size.times do |i|
select
when value = chan.receive
title, ip = value

STDERR.puts "#{title}#{ip}"
when timeout 5.seconds
STDERR.puts "Timeout!"
exit
end
end
end

0 comments on commit cf274ab

Please sign in to comment.