Skip to content

Commit

Permalink
Update ameba version.
Browse files Browse the repository at this point in the history
  • Loading branch information
zw963 committed Mar 7, 2023
1 parent b331dc5 commit b6f39f3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 42 deletions.
13 changes: 13 additions & 0 deletions .ameba.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This configuration file was generated by `ameba --gen-config`
# on 2023-03-07 14:14:39 UTC using Ameba version 1.4.2.
# The point is for the user to remove these configuration records
# one by one as the reported problems are removed from the code base.

# Problems found: 1
# Run `ameba --only Lint/NotNil` for details
Lint/NotNil:
Description: Identifies usage of `not_nil!` calls
Excluded:
- src/myip.cr
Enabled: false
Severity: Warning
2 changes: 1 addition & 1 deletion shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2.0
shards:
ameba:
git: https://github.com/crystal-ameba/ameba.git
version: 1.1.0
version: 1.4.2+git.commit.3fbbe3986eef7e0e22b9decc3e37c2110f2a10eb

lexbor:
git: https://github.com/kostya/lexbor.git
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies:
development_dependencies:
ameba:
github: crystal-ameba/ameba
version: ~> 1.1.0
branch: master

crystal: 1.5.0

Expand Down
76 changes: 36 additions & 40 deletions src/myip.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ require "json"
chan = Channel(Tuple(String, String)).new

def from_url(url : String, follow : Bool = false) : Lexbor::Parser
begin
response = HTTP::Client.get url
if response.status_code == 200
return Lexbor::Parser.new(response.body)
elsif follow && response.status_code == 301
from_url response.headers["Location"], follow: true
else
raise ArgumentError.new "Host returned #{response.status_code}"
end
rescue Socket::Error
raise Socket::Error.new "Host #{url} cannot be fetched"
response = HTTP::Client.get url
if response.status_code == 200
Lexbor::Parser.new(response.body)
elsif follow && response.status_code == 301
from_url response.headers["Location"], follow: true
else
raise ArgumentError.new "Host returned #{response.status_code}"
end
rescue Socket::Error
raise Socket::Error.new "Host #{url} cannot be fetched"
end

def get_ip_from_ib_sb(chan)
Expand Down Expand Up @@ -58,37 +56,35 @@ def get_ip_from_ip138(chan)
end

def get_ip_from_ip111(chan)
begin
ip111_url = "http://www.ip111.cn"
doc = from_url(ip111_url, follow: true)

iframe = doc.nodes("iframe").map do |node|
spawn do
url = node.attribute_by("src").not_nil!
ip = from_url(url).body!.tag_text.strip
title = node.parent!.parent!.parent!.css(".card-header").first.tag_text.strip

chan.send({"ip111.cn:#{title}", ip})
rescue Socket::Error
STDERR.puts "visit #{url} failed, please check internet connection."
rescue ArgumentError
STDERR.puts "#{url} return 500"
rescue ex
STDERR.puts ex.message
end
ip111_url = "http://www.ip111.cn"
doc = from_url(ip111_url, follow: true)

iframe = doc.nodes("iframe").map do |node|
spawn do
url = node.attribute_by("src").not_nil!
ip = from_url(url).body!.tag_text.strip
title = node.parent!.parent!.parent!.css(".card-header").first.tag_text.strip

chan.send({"ip111.cn:#{title}", ip})
rescue Socket::Error
STDERR.puts "visit #{url} failed, please check internet connection."
rescue ArgumentError
STDERR.puts "#{url} return 500"
rescue ex
STDERR.puts ex.message
end

{doc, iframe.size}
rescue Socket::Error
STDERR.puts "visit #{ip111_url} failed, please check internet connection."
exit
rescue ArgumentError
STDERR.puts "#{ip111_url} return 500"
exit
rescue ex
STDERR.puts ex.message
exit
end

{doc, iframe.size}
rescue Socket::Error
STDERR.puts "visit #{ip111_url} failed, please check internet connection."
exit
rescue ArgumentError
STDERR.puts "#{ip111_url} return 500"
exit
rescue ex
STDERR.puts ex.message
exit
end

at_exit do
Expand Down

0 comments on commit b6f39f3

Please sign in to comment.