Module: TempmailSdk::Providers::Lroid
- Defined in:
- lib/tempmail_sdk/providers/lroid.rb
Overview
Lroid 渠道 — https://lroid.com HTML 解析模式,session cookies 维持邮箱身份
Constant Summary collapse
- CHANNEL =
"lroid"- BASE_URL =
"https://lroid.com"- EMAIL_RE =
/<input[^>]+id=["']eposta_adres["'][^>]+value=["']([^"']+@[^"']+)["']/i- EMAIL_RE_ALT =
/<input[^>]+value=["']([^"']+@[^"']+)["'][^>]+id=["']eposta_adres["']/i- HEADERS =
{ "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language" => "en-US,en;q=0.9", "Referer" => "#{BASE_URL}/", "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " \ "(KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36 Edg/146.0.0.0" }.freeze
Class Method Summary collapse
-
.cookie_header_from_resp(resp) ⇒ Object
从响应中收集并序列化 cookies.
-
.decode_token(token) ⇒ Object
解码 token.
-
.extract_email(html_str) ⇒ Object
从 HTML 中提取邮箱地址.
-
.generate_email ⇒ EmailInfo
创建临时邮箱.
-
.get_emails(token, email = "") ⇒ Array<Email>
获取邮件列表.
- .parse_html_emails(cookie, recipient) ⇒ Object
- .parse_json_emails(items, recipient) ⇒ Object
Class Method Details
.cookie_header_from_resp(resp) ⇒ Object
从响应中收集并序列化 cookies
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/tempmail_sdk/providers/lroid.rb', line 27 def (resp) map = {} resp..each do |line| pair = line.split(";", 2).first.to_s.strip next unless pair.include?("=") k, v = pair.split("=", 2) map[k.strip] = v.to_s.strip unless k.strip.empty? end map.keys.sort.map { |k| "#{k}=#{map[k]}" }.join("; ") end |
.decode_token(token) ⇒ Object
解码 token
51 52 53 54 55 56 57 58 59 |
# File 'lib/tempmail_sdk/providers/lroid.rb', line 51 def decode_token(token) data = JSON.parse(token) = (data["cookie"] || "").strip raise "lroid: session token 中缺少 cookie" if .empty? rescue JSON::ParserError raise "lroid: 无效的 session token" end |
.extract_email(html_str) ⇒ Object
从 HTML 中提取邮箱地址
40 41 42 43 44 45 46 47 48 |
# File 'lib/tempmail_sdk/providers/lroid.rb', line 40 def extract_email(html_str) m = html_str.match(EMAIL_RE) || html_str.match(EMAIL_RE_ALT) raise "lroid: 无法从 HTML 响应中解析邮箱地址" unless m addr = m[1].strip raise "lroid: 解析到的邮箱地址无效" unless addr.include?("@") addr end |
.generate_email ⇒ EmailInfo
创建临时邮箱
63 64 65 66 67 68 69 70 |
# File 'lib/tempmail_sdk/providers/lroid.rb', line 63 def generate_email resp = Http.get(BASE_URL, headers: HEADERS, timeout: 15) resp.raise_for_status email = extract_email(resp.body) = (resp) token = JSON.generate("cookie" => ) EmailInfo.new(channel: CHANNEL, email: email, token: token) end |
.get_emails(token, email = "") ⇒ Array<Email>
获取邮件列表
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/tempmail_sdk/providers/lroid.rb', line 76 def get_emails(token, email = "") = decode_token(token) address = (email || "").strip raise "lroid: 邮箱地址不能为空" if address.empty? # 尝试 kontrol API begin resp = Http.get( "#{BASE_URL}/en/api-kontrol/", headers: { "Accept" => "application/json, text/html, */*;q=0.8", "Referer" => "#{BASE_URL}/", "Cookie" => , "User-Agent" => HEADERS["User-Agent"] }, timeout: 15 ) resp.raise_for_status data = resp.json if data.is_a?(Array) return parse_json_emails(data, address) elsif data.is_a?(Hash) %w[mails emails messages data inbox].each do |key| items = data[key] return parse_json_emails(items, address) if items.is_a?(Array) end if data["id"] || data["subject"] || data["from"] return parse_json_emails([data], address) end end rescue StandardError # API 不可用,回退到 HTML 解析 end parse_html_emails(, address) end |
.parse_html_emails(cookie, recipient) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/tempmail_sdk/providers/lroid.rb', line 123 def parse_html_emails(, recipient) resp = Http.get( BASE_URL, headers: { "Accept" => "text/html,application/xhtml+xml,*/*;q=0.8", "Referer" => "#{BASE_URL}/", "Cookie" => , "User-Agent" => HEADERS["User-Agent"] }, timeout: 15 ) resp.raise_for_status html_str = resp.body # 解析 <li class="mail"> 元素 mail_items = html_str.scan(/<li[^>]*class=["'][^"']*\bmail\b[^"']*["'][^>]*>(.*?)<\/li>/mi) return [] if mail_items.empty? mail_items.each_with_index.filter_map do |(item_html), idx| subject = "" from_addr = "" date = "" mail_id = (idx + 1).to_s # 提取主题 sm = item_html.match(/class=["'][^"']*\bsubject\b[^"']*["'][^>]*>(.*?)<\//mi) subject = sm[1].gsub(/<[^>]+>/, "").strip if sm # 提取发件人 fm = item_html.match(/class=["'][^"']*\b(?:from|sender)\b[^"']*["'][^>]*>(.*?)<\//mi) from_addr = fm[1].gsub(/<[^>]+>/, "").strip if fm # 提取日期 dm = item_html.match(/class=["'][^"']*\b(?:date|time)\b[^"']*["'][^>]*>(.*?)<\//mi) date = dm[1].gsub(/<[^>]+>/, "").strip if dm Normalize.normalize_email({ "id" => mail_id, "from" => from_addr, "to" => recipient, "subject" => subject, "date" => date }, recipient) end end |
.parse_json_emails(items, recipient) ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/tempmail_sdk/providers/lroid.rb', line 113 def parse_json_emails(items, recipient) items.filter_map do |raw| next unless raw.is_a?(Hash) normalized = raw.dup normalized["html"] = raw["body"] || raw["content"] if !raw.key?("html") Normalize.normalize_email(normalized, recipient) end end |