Module: TempmailSdk::Providers::TempemailCo
- Defined in:
- lib/tempmail_sdk/providers/tempemail_co.rb
Overview
tempemail.co 渠道 — https://tempemail.co Cookie Session + REST JSON API
Constant Summary collapse
- CHANNEL =
"tempemail-co"- BASE_URL =
"https://tempemail.co"- MAIL_ID_RE =
从邮件列表 HTML 中提取邮件 ID(data-id="数字")
/data-id="(\d+)"/- HEADERS =
{ "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language" => "en-US,en;q=0.9", "Cache-Control" => "no-cache", "DNT" => "1", "Pragma" => "no-cache", "Upgrade-Insecure-Requests" => "1", "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
- .cookies_to_str(cookies) ⇒ Object
- .decode_token(token) ⇒ Object
- .encode_token(address, cookies) ⇒ Object
- .extract_cookies(resp) ⇒ Object
-
.generate_email ⇒ EmailInfo
创建临时邮箱.
-
.get_emails(email, token) ⇒ Array<Email>
获取邮件列表.
Class Method Details
.cookies_to_str(cookies) ⇒ Object
38 39 40 |
# File 'lib/tempmail_sdk/providers/tempemail_co.rb', line 38 def () .sort.map { |k, v| "#{k}=#{v}" }.join("; ") end |
.decode_token(token) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/tempmail_sdk/providers/tempemail_co.rb', line 46 def decode_token(token) data = JSON.parse(token) address = data["address"] || "" = data["cookies"] || {} raise "tempemail-co: token 数据不完整" if address.empty? || !.is_a?(Hash) [address, ] end |
.encode_token(address, cookies) ⇒ Object
42 43 44 |
# File 'lib/tempmail_sdk/providers/tempemail_co.rb', line 42 def encode_token(address, ) JSON.generate({ "address" => address, "cookies" => }) end |
.extract_cookies(resp) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/tempmail_sdk/providers/tempemail_co.rb', line 28 def (resp) = {} resp..each do |line| pair = line.split(";").first.to_s.strip k, v = pair.split("=", 2) [k] = v if k && v end end |
.generate_email ⇒ EmailInfo
创建临时邮箱
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/tempmail_sdk/providers/tempemail_co.rb', line 57 def generate_email r = Http.get("#{BASE_URL}/mail/random", headers: HEADERS.merge( "Referer" => "#{BASE_URL}/", "Accept" => "application/json, text/javascript, */*; q=0.01" )) r.raise_for_status data = r.json raise "tempemail-co: 创建邮箱失败" unless data["result"] address = (data["address"] || data["id"] || "").strip raise "tempemail-co: 返回的邮箱地址无效" if address.empty? || !address.include?("@") = (r) tok = encode_token(address, ) EmailInfo.new(channel: CHANNEL, email: address, token: tok) end |
.get_emails(email, token) ⇒ Array<Email>
获取邮件列表
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/tempmail_sdk/providers/tempemail_co.rb', line 78 def get_emails(email, token) raise "tempemail-co: token 不能为空" if token.nil? || token.empty? addr = (email || "").strip raise "tempemail-co: 邮箱地址不能为空" if addr.empty? address, = decode_token(token) = () # 获取邮件列表 r = Http.get("#{BASE_URL}/get-mails?mail_id=#{URI.encode_www_form_component(address)}&unseen=0&is_new=1", headers: HEADERS.merge( "Referer" => "#{BASE_URL}/", "Cookie" => , "Accept" => "application/json, text/javascript, */*; q=0.01" )) r.raise_for_status data = r.json count = data["count"] || 0 return [] if count <= 0 mails_html = data["mails"] || "" return [] unless mails_html.is_a?(String) && !mails_html.empty? mail_ids = mails_html.scan(MAIL_ID_RE).flatten.uniq return [] if mail_ids.empty? mail_ids.filter_map do |mail_id| begin detail_resp = Http.get("#{BASE_URL}/mail/info?id=#{mail_id}", headers: HEADERS.merge( "Referer" => "#{BASE_URL}/", "Cookie" => , "Accept" => "application/json, text/javascript, */*; q=0.01" )) next unless detail_resp.ok? detail_data = detail_resp.json next unless detail_data["result"] mail_info = detail_data["mail"] next unless mail_info.is_a?(Hash) from_name = (mail_info["fromName"] || "").strip from_addr = (mail_info["fromAddress"] || "").strip from_display = from_name.empty? ? from_addr : "#{from_name} <#{from_addr}>" raw = { "id" => mail_id.to_s, "from" => from_display, "from_address" => from_addr, "to" => addr, "subject" => mail_info["subject"] || "", "html" => mail_info["textHtml"] || "", "date" => mail_info["displayDate"] || "" } Normalize.normalize_email(raw, addr) rescue StandardError next end end end |