<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8">
<style>
div.markdown { white-space: normal; }
body { font-family: sans-serif; }
h1 { font-size: 1.4em; }
h2 { font-size: 1.2em; }
h3 { font-size: 1.1em; }
blockquote { margin: 0 0 5px; padding-left: 5px; border-left: 2px solid #777777; color: #777777; }
blockquote blockquote { border-left-color: #999999; color: #999999; }
blockquote blockquote blockquote { border-left-color: #BBBBBB; color: #BBBBBB; }
a { color: #3983C4; }
blockquote a { color: #777777; }
blockquote blockquote a { color: #999999; }
blockquote blockquote blockquote a { color: #BBBBBB; }
pre { margin-left: 15px; margin-right: 15px; padding: 5px; background-color: #F7F7F7; border-radius: 5px 5px 5px 5px; overflow-x: auto; max-width: 90vw; }
pre.highlighted { color: black; }
code { margin: 0; padding: 0 0.4em; border-radius: 3px; background-color: #F7F7F7; }
pre > code { padding: 0px; }
math[display="inline"] > mrow { padding:5px; }
div.footnotes li p { margin: 0.2em 0; }
</style>
</head>
<body>
<div class="markdown">
<p dir="auto">On 3 Mar 2019, at 12:22, Benny Kjær Nielsen wrote:</p>
<blockquote>
<p dir="auto">(But [libravatar.org] appears to be slower which emphasizes that MailMate does not cache these images.)</p>
</blockquote>
<p dir="auto">It might be because <a href="https://libravatar.org">https://libravatar.org</a> <a href="https://git.linux-kernel.at/oliver/ivatar/issues/50">didn't provide <code>Cache-Control</code></a> at that point?</p>
<p dir="auto">Is there a way for getting all the "Not Junk" mail adresses from MailMate?</p>
<p dir="auto">Anyway, here is one script for making a <code>RewriteMap</code> that your local Apache can use for a caching reverse proxy. </p>
<p dir="auto">(Only needs <code>pip install pylibravatar py3dns requests furl</code> if I recall correctly)</p>
<pre class="highlighted"><code><span style="color: #888888">#!/usr/bin/env python3</span>
<span style="color: #008800; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">libravatar</span>
<span style="color: #008800; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">requests</span>
<span style="color: #008800; font-weight: bold">from</span> <span style="color: #0e84b5; font-weight: bold">furl</span> <span style="color: #008800; font-weight: bold">import</span> furl
<span style="color: #008800; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">os</span>
<span style="color: #008800; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">sys</span>
<span style="color: #008800; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">argparse</span>
p <span style="color: #333333">=</span> argparse<span style="color: #333333">.</span>ArgumentParser()
p<span style="color: #333333">.</span>add_argument(
<span style="background-color: #fff0f0">"-i"</span>,
<span style="background-color: #fff0f0">"--infile"</span>,
<span style="color: #007020">type</span><span style="color: #333333">=</span>argparse<span style="color: #333333">.</span>FileType(<span style="background-color: #fff0f0">"r"</span>),
default<span style="color: #333333">=</span>sys<span style="color: #333333">.</span>stdin,
help<span style="color: #333333">=</span><span style="background-color: #fff0f0">"Input one email adress pr line"</span>,
)
p<span style="color: #333333">.</span>add_argument(
<span style="background-color: #fff0f0">"-o"</span>,
<span style="background-color: #fff0f0">"--outfile"</span>,
<span style="color: #007020">type</span><span style="color: #333333">=</span>argparse<span style="color: #333333">.</span>FileType(<span style="background-color: #fff0f0">"w"</span>),
default<span style="color: #333333">=</span>sys<span style="color: #333333">.</span>stdout,
help<span style="color: #333333">=</span><span style="background-color: #fff0f0">"Output result fitting for a Apache RewriteMap"</span>,
)
a <span style="color: #333333">=</span> p<span style="color: #333333">.</span>parse_args()
<span style="color: #008800; font-weight: bold">for</span> em <span style="color: #000000; font-weight: bold">in</span> a<span style="color: #333333">.</span>infile<span style="color: #333333">.</span>read()<span style="color: #333333">.</span>splitlines():
<span style="color: #888888"># Get libravatar_url, default to 404 with added query for</span>
<span style="color: #888888"># not using gravatarproxy and redirect if no libravatar</span>
<span style="color: #888888"># au = avatarurl</span>
<span style="color: #008800; font-weight: bold">try</span>:
au <span style="color: #333333">=</span> furl(
libravatar<span style="color: #333333">.</span>libravatar_url(
email<span style="color: #333333">=</span>em, https<span style="color: #333333">=</span><span style="color: #007020">True</span>, default<span style="color: #333333">=</span><span style="background-color: #fff0f0">"404"</span>
)
)<span style="color: #333333">.</span>add(args<span style="color: #333333">=</span>{<span style="background-color: #fff0f0">"gravatarproxy"</span>: <span style="background-color: #fff0f0">"n"</span>, <span style="background-color: #fff0f0">"gravatarredirect"</span>: <span style="background-color: #fff0f0">"n"</span>})
ah <span style="color: #333333">=</span> libravatar<span style="color: #333333">.</span>parse_user_identity(email<span style="color: #333333">=</span>em, openid<span style="color: #333333">=</span><span style="color: #007020">None</span>)[<span style="color: #0000DD; font-weight: bold">0</span>]
<span style="color: #008800; font-weight: bold">except</span> <span style="color: #FF0000; font-weight: bold">Exception</span>:
<span style="color: #008800; font-weight: bold">pass</span>
<span style="color: #888888"># Follow redirects</span>
aur <span style="color: #333333">=</span> requests<span style="color: #333333">.</span>head(au<span style="color: #333333">.</span>url, allow_redirects<span style="color: #333333">=</span><span style="color: #007020">True</span>)
<span style="color: #888888"># If no libravatar, check if a gravatar exist</span>
<span style="color: #008800; font-weight: bold">if</span> aur<span style="color: #333333">.</span>status_code <span style="color: #333333">==</span> <span style="color: #0000DD; font-weight: bold">404</span>:
au <span style="color: #333333">=</span> furl(<span style="background-color: #fff0f0">"https://www.gravatar.com/avatar/"</span>)<span style="color: #333333">.</span>add(
path<span style="color: #333333">=</span>ah, args<span style="color: #333333">=</span>{<span style="background-color: #fff0f0">"d"</span>: <span style="background-color: #fff0f0">"404"</span>}
)
aur <span style="color: #333333">=</span> requests<span style="color: #333333">.</span>head(au, allow_redirects<span style="color: #333333">=</span><span style="color: #007020">True</span>)
<span style="color: #888888"># If http = 200, (not e.g. 404) output result</span>
<span style="color: #008800; font-weight: bold">if</span> aur<span style="color: #333333">.</span>status_code <span style="color: #333333">==</span> <span style="color: #0000DD; font-weight: bold">200</span>:
<span style="color: #888888"># Remove query</span>
au <span style="color: #333333">=</span> furl(aur<span style="color: #333333">.</span>url)<span style="color: #333333">.</span>remove(query<span style="color: #333333">=</span><span style="color: #007020">True</span>)<span style="color: #333333">.</span>url
<span style="color: #888888"># Output result fitting for</span>
<span style="color: #888888"># <https://httpd.apache.org/docs/current/rewrite/rewritemap.html#txt></span>
<span style="color: #888888"># -> mailmd5hash avatarurl # mail</span>
a<span style="color: #333333">.</span>outfile<span style="color: #333333">.</span>write(<span style="background-color: #fff0f0">" "</span><span style="color: #333333">.</span>join([ah, au]) <span style="color: #333333">+</span> <span style="background-color: #fff0f0">" # "</span> <span style="color: #333333">+</span> em <span style="color: #333333">+</span> os<span style="color: #333333">.</span>linesep)
</code></pre>
</div>
</body>
</html>