Skip to content

Commit b331dc5

Browse files
committed
Change to use lexbor instead crystagiri.
1 parent f661a60 commit b331dc5

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

shard.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ shards:
44
git: https://github.com/crystal-ameba/ameba.git
55
version: 1.1.0
66

7-
crystagiri:
8-
git: https://github.com/madeindjs/crystagiri.git
9-
version: 0.3.5
7+
lexbor:
8+
git: https://github.com/kostya/lexbor.git
9+
version: 3.0.4
1010

shard.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ targets:
99
main: src/myip.cr
1010

1111
dependencies:
12-
crystagiri:
13-
github: madeindjs/crystagiri
12+
lexbor:
13+
github: kostya/lexbor
1414

1515
development_dependencies:
1616
ameba:

src/myip.cr

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1-
require "crystagiri"
1+
require "lexbor"
2+
require "http/client"
23
require "./myip/*"
34
require "option_parser"
45
require "json"
56

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

9+
def from_url(url : String, follow : Bool = false) : Lexbor::Parser
10+
begin
11+
response = HTTP::Client.get url
12+
if response.status_code == 200
13+
return Lexbor::Parser.new(response.body)
14+
elsif follow && response.status_code == 301
15+
from_url response.headers["Location"], follow: true
16+
else
17+
raise ArgumentError.new "Host returned #{response.status_code}"
18+
end
19+
rescue Socket::Error
20+
raise Socket::Error.new "Host #{url} cannot be fetched"
21+
end
22+
end
23+
824
def get_ip_from_ib_sb(chan)
925
spawn do
1026
url = "https://api.ip.sb/geoip"
11-
doc = Crystagiri::HTML.from_url url, follow: true
12-
result = JSON.parse(doc.content)
27+
response = HTTP::Client.get(url)
28+
result = JSON.parse(response.body)
1329
io = IO::Memory.new
1430
PrettyPrint.format(result, io, 79)
1531
io.rewind
@@ -25,12 +41,13 @@ end
2541

2642
def get_ip_from_ip138(chan)
2743
spawn do
28-
doc = Crystagiri::HTML.from_url "http://www.ip138.com", follow: true
29-
ip138_url = doc.at_css("iframe").not_nil!.node.attributes["src"].content
44+
url = "http://www.ip138.com"
45+
doc = from_url(url, follow: true)
46+
ip138_url = doc.css("iframe").first.attribute_by("src")
3047
url = "http:#{ip138_url}"
31-
doc = Crystagiri::HTML.from_url url
48+
doc = from_url url
3249

33-
chan.send({"ip138.com:", doc.at_css("body p").not_nil!.content.strip})
50+
chan.send({"ip138.com:", doc.css("body p").first.tag_text.strip})
3451
rescue Socket::Error
3552
STDERR.puts "visit #{url} failed, please check internet connection."
3653
rescue ArgumentError
@@ -43,13 +60,13 @@ end
4360
def get_ip_from_ip111(chan)
4461
begin
4562
ip111_url = "http://www.ip111.cn"
46-
doc = Crystagiri::HTML.from_url ip111_url
63+
doc = from_url(ip111_url, follow: true)
4764

48-
iframe = doc.where_tag("iframe") do |tag|
65+
iframe = doc.nodes("iframe").map do |node|
4966
spawn do
50-
url = tag.node.attributes["src"].content
51-
ip = Crystagiri::HTML.from_url(url).at_css("body").not_nil!.content
52-
title = tag.node.parent.try(&.parent).try(&.parent).not_nil!.xpath_node("div[@class='card-header']").not_nil!.content.strip
67+
url = node.attribute_by("src").not_nil!
68+
ip = from_url(url).body!.tag_text.strip
69+
title = node.parent!.parent!.parent!.css(".card-header").first.tag_text.strip
5370

5471
chan.send({"ip111.cn:#{title}", ip})
5572
rescue Socket::Error
@@ -113,8 +130,8 @@ USAGE
113130

114131
doc, iframe_size = get_ip_from_ip111(chan)
115132

116-
title = doc.at_css(".card-header").not_nil!.content.strip
117-
ip = doc.at_css(".card-body p").not_nil!.content.strip
133+
title = doc.css(".card-header").first.tag_text.strip
134+
ip = doc.css(".card-body p").first.tag_text.strip
118135

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

0 commit comments

Comments
 (0)