import md5 import os import warnings import logging import urllib impo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import md5
import os
import warnings
import logging
import urllib
import urllib2
from datetime import datetime, timedelta
warnings.filterwarnings('ignore', r'tmpnam')
from pybb import settings as pybb_settings
def fetch_gravatar(email):
"""
Fetch avatar from gravatar.com service.
Return None if avatar was not found.
"""
hash = md5.new(email).hexdigest()
size = max(pybb_settings.AVATAR_WIDTH, pybb_settings.AVATAR_HEIGHT)
default = urllib.quote('http://spam.egg/')
url = 'http://www.gravatar.com/avatar/%s?s=%d&d=%s' % (hash, size, default)
fname = os.tmpnam()
class RedirectHandler(urllib2.HTTPRedirectHandler):
def http_error_302(*args):
raise IOError('Redirect found')
opener = urllib2.build_opener(RedirectHandler())
try:
file(fname, 'wb').write(opener.open(url, fname).read())
except IOError, ex:
logging.error(ex)
return None
else:
return fname
def check_gravatar(user, ignore_date_joined=False, ignore_saved_avatar=False):
if user.is_active:
if ignore_date_joined or (datetime.now() - user.date_joined) < timedelta(seconds=3):
if ignore_saved_avatar or not user.pybb_profile.avatar:
path = fetch_gravatar(user.email)
if path:
print path
# TODO