|
1 | | -require "crystagiri" |
| 1 | +require "lexbor" |
| 2 | +require "http/client" |
2 | 3 | require "./myip/*" |
3 | 4 | require "option_parser" |
4 | 5 | require "json" |
5 | 6 |
|
6 | 7 | chan = Channel(Tuple(String, String)).new |
7 | 8 |
|
| 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 | + |
8 | 24 | def get_ip_from_ib_sb(chan) |
9 | 25 | spawn do |
10 | 26 | 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) |
13 | 29 | io = IO::Memory.new |
14 | 30 | PrettyPrint.format(result, io, 79) |
15 | 31 | io.rewind |
|
25 | 41 |
|
26 | 42 | def get_ip_from_ip138(chan) |
27 | 43 | 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") |
30 | 47 | url = "http:#{ip138_url}" |
31 | | - doc = Crystagiri::HTML.from_url url |
| 48 | + doc = from_url url |
32 | 49 |
|
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}) |
34 | 51 | rescue Socket::Error |
35 | 52 | STDERR.puts "visit #{url} failed, please check internet connection." |
36 | 53 | rescue ArgumentError |
|
43 | 60 | def get_ip_from_ip111(chan) |
44 | 61 | begin |
45 | 62 | ip111_url = "http://www.ip111.cn" |
46 | | - doc = Crystagiri::HTML.from_url ip111_url |
| 63 | + doc = from_url(ip111_url, follow: true) |
47 | 64 |
|
48 | | - iframe = doc.where_tag("iframe") do |tag| |
| 65 | + iframe = doc.nodes("iframe").map do |node| |
49 | 66 | 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 |
53 | 70 |
|
54 | 71 | chan.send({"ip111.cn:#{title}:", ip}) |
55 | 72 | rescue Socket::Error |
@@ -113,8 +130,8 @@ USAGE |
113 | 130 |
|
114 | 131 | doc, iframe_size = get_ip_from_ip111(chan) |
115 | 132 |
|
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 |
118 | 135 |
|
119 | 136 | STDERR.puts "ip111.cn:#{title}:#{ip}" |
120 | 137 |
|
|
0 commit comments