Module: TempmailSdk::Providers::Mailnesia

Defined in:
lib/tempmail_sdk/providers/mailnesia.rb

Overview

Mailnesia 渠道 — https://mailnesia.com HTML 解析公开邮箱

Constant Summary collapse

CHANNEL =
"mailnesia"
BASE_URL =
"https://mailnesia.com"
DOMAIN =
"mailnesia.com"
HEADERS =
{ "Accept" => "text/html,*/*" }.freeze
ROW_RE =
/<tr\s+id="([^"]+)"[^>]*class="[^"]*\bemailheader\b[^"]*"[^>]*>(.*?)<\/tr>/mi
TIME_RE =
/<time\s+datetime="([^"]+)"/i
ANCHOR_RE =
/<a\b[^>]*class="email"[^>]*>(.*?)<\/a>/mi
TAG_RE =
/<[^>]+>/

Class Method Summary collapse

Class Method Details

.clean_text(raw) ⇒ Object



47
48
49
# File 'lib/tempmail_sdk/providers/mailnesia.rb', line 47

def clean_text(raw)
  CGI.unescapeHTML(raw.to_s.gsub(TAG_RE, " ")).split.join(" ")
end

.detail_url(local, message_id) ⇒ Object



37
38
39
# File 'lib/tempmail_sdk/providers/mailnesia.rb', line 37

def detail_url(local, message_id)
  "#{mailbox_url(local)}/#{URI.encode_www_form_component(message_id)}"
end

.extract_div_by_id(page, div_id, next_id = "") ⇒ Object

从详情页提取指定 div 内容



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tempmail_sdk/providers/mailnesia.rb', line 72

def extract_div_by_id(page, div_id, next_id = "")
  needle = "id=\"#{div_id}\""
  pos = page.index(needle)
  return "" unless pos

  open_end = page.index(">", pos)
  return "" unless open_end

  start = open_end + 1
  end_pos = -1
  end_pos = page.index("<div id=\"#{next_id}\"", start) unless next_id.empty?
  if end_pos < 0
    close = page.index("</div>", start)
    end_pos = close + 6 if close
  end
  return "" if end_pos < 0

  content = page[start...end_pos].strip
  content = content[0...-6].strip if content.end_with?("</div>")
  content
end

.extract_plain(page, message_id) ⇒ Object

提取纯文本部分



95
96
97
98
99
# File 'lib/tempmail_sdk/providers/mailnesia.rb', line 95

def extract_plain(page, message_id)
  pattern = /<div\s+id="text_plain_#{Regexp.escape(message_id)}"[^>]*>\s*<pre>(.*?)<\/pre>\s*<\/div>/mi
  m = page.match(pattern)
  m ? CGI.unescapeHTML(m[1]).strip : ""
end

.fetch_detail(local, row) ⇒ Object

获取单封详情



102
103
104
105
106
107
108
109
110
111
# File 'lib/tempmail_sdk/providers/mailnesia.rb', line 102

def fetch_detail(local, row)
  message_id = (row["id"] || "").strip
  return row if message_id.empty?

  page = fetch_html(detail_url(local, message_id))
  detail = row.dup
  detail["text"] = extract_plain(page, message_id)
  detail["html"] = extract_div_by_id(page, "text_html_#{message_id}", "text_plain_#{message_id}")
  detail
end

.fetch_html(url) ⇒ Object



41
42
43
44
45
# File 'lib/tempmail_sdk/providers/mailnesia.rb', line 41

def fetch_html(url)
  resp = Http.get(url, headers: HEADERS, timeout: 15)
  resp.raise_for_status
  resp.body
end

.generate_emailEmailInfo

创建临时邮箱

Returns:



115
116
117
118
119
# File 'lib/tempmail_sdk/providers/mailnesia.rb', line 115

def generate_email
  local = random_local
  fetch_html(mailbox_url(local))
  EmailInfo.new(channel: CHANNEL, email: "#{local}@#{DOMAIN}")
end

.get_emails(email) ⇒ Array<Email>

获取邮件列表

Parameters:

  • email (String)

Returns:



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/tempmail_sdk/providers/mailnesia.rb', line 124

def get_emails(email)
  local = local_part(email)
  raise "mailnesia: empty email" if local.empty?

  page = fetch_html(mailbox_url(local))
  rows = parse_rows(page)

  rows.map do |row|
    begin
      Normalize.normalize_email(fetch_detail(local, row), email)
    rescue StandardError
      Normalize.normalize_email(row, email)
    end
  end
end

.local_part(email) ⇒ Object



29
30
31
# File 'lib/tempmail_sdk/providers/mailnesia.rb', line 29

def local_part(email)
  (email || "").strip.split("@").first.to_s.strip
end

.mailbox_url(local) ⇒ Object



33
34
35
# File 'lib/tempmail_sdk/providers/mailnesia.rb', line 33

def mailbox_url(local)
  "#{BASE_URL}/mailbox/#{URI.encode_www_form_component(local)}"
end

.parse_rows(page) ⇒ Object

从邮箱列表页解析邮件条目



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tempmail_sdk/providers/mailnesia.rb', line 52

def parse_rows(page)
  rows = []
  page.scan(ROW_RE).each do |message_id, row_html|
    message_id = message_id.strip
    date_match = row_html.match(TIME_RE)
    anchors = row_html.scan(ANCHOR_RE).map { |m| clean_text(m[0]) }
    next if anchors.length < 3

    rows << {
      "id" => message_id,
      "date" => date_match ? CGI.unescapeHTML(date_match[1].strip) : "",
      "from" => anchors[0],
      "to" => anchors[1],
      "subject" => anchors[2]
    }
  end
  rows
end

.random_localObject



24
25
26
27
# File 'lib/tempmail_sdk/providers/mailnesia.rb', line 24

def random_local
  chars = ("a".."z").to_a + ("0".."9").to_a
  "sdk" + Array.new(16) { chars.sample }.join
end