[MlMt] Gravatar-hack for MailMate

Aslak Raanes aslakr at gmail.com
Sat Mar 16 10:23:38 EDT 2019


On 3 Mar 2019, at 12:22, Benny Kjær Nielsen wrote:

> (But [libravatar.org] appears to be slower which emphasizes that 
> MailMate does not cache these images.)

It might be because <https://libravatar.org> [didn't provide 
`Cache-Control`](https://git.linux-kernel.at/oliver/ivatar/issues/50) at 
that point?

Is there a way for getting all the "Not Junk" mail adresses from 
MailMate?

Anyway, here is one script for making a `RewriteMap` that your local 
Apache can use for a caching reverse proxy.

(Only needs `pip install pylibravatar py3dns requests furl` if I recall 
correctly)

```python
#!/usr/bin/env python3
import libravatar
import requests
from furl import furl
import os
import sys
import argparse

p = argparse.ArgumentParser()
p.add_argument(
     "-i",
     "--infile",
     type=argparse.FileType("r"),
     default=sys.stdin,
     help="Input one email adress pr line",
)
p.add_argument(
     "-o",
     "--outfile",
     type=argparse.FileType("w"),
     default=sys.stdout,
     help="Output result fitting for a Apache RewriteMap",
)
a = p.parse_args()


for em in a.infile.read().splitlines():
     # Get libravatar_url, default to 404 with added query for
     # not using gravatarproxy and redirect if no libravatar
     # au = avatarurl
     try:
         au = furl(
             libravatar.libravatar_url(
                 email=em, https=True, default="404"
             )
         ).add(args={"gravatarproxy": "n", "gravatarredirect": "n"})
         ah = libravatar.parse_user_identity(email=em, openid=None)[0]
     except Exception:
         pass

     # Follow redirects
     aur = requests.head(au.url, allow_redirects=True)

     # If no libravatar, check if a gravatar exist
     if aur.status_code == 404:
         au = furl("https://www.gravatar.com/avatar/").add(
             path=ah, args={"d": "404"}
         )
         aur = requests.head(au, allow_redirects=True)

     # If http = 200, (not e.g. 404) output result
     if aur.status_code == 200:
         # Remove query
         au = furl(aur.url).remove(query=True).url
         # Output result fitting for
         # 
<https://httpd.apache.org/docs/current/rewrite/rewritemap.html#txt>
         # -> mailmd5hash avatarurl # mail
         a.outfile.write(" ".join([ah, au]) + " # " + em + os.linesep)
```






-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freron.com/pipermail/mailmate/attachments/20190316/367fa764/attachment.html>


More information about the mailmate mailing list