Gravatar scraper.

master
Nick Sergeant 2013-04-14 23:35:05 -04:00
parent b6e36aed98
commit e16e3f8d2d
5 changed files with 90 additions and 36 deletions

120
fabfile.py vendored
View File

@ -1,9 +1,12 @@
#!/usr/bin/env python
# -- coding: utf-8 --
from fabric.api import cd, local, env, run, sudo from fabric.api import cd, local, env, run, sudo
from boto.s3.connection import S3Connection from boto.s3.connection import S3Connection
from boto.s3.key import Key from boto.s3.key import Key
import datetime, sys import datetime, hashlib, sys
from settings_local import AMAZON_API_KEY, AMAZON_API_SECRET, ENV_HOST from settings_local import AMAZON_API_KEY, AMAZON_API_SECRET, ENV_HOST
@ -12,9 +15,21 @@ env.hosts = [ENV_HOST]
env.site_path = '/var/www/snipt' env.site_path = '/var/www/snipt'
env.venv_path = '/home/nick/.virtualenvs/snipt' env.venv_path = '/home/nick/.virtualenvs/snipt'
def _display_message(message, extra_line=True):
if extra_line:
msg = '\n{}\n========================\n\n'.format(message)
else:
msg = '{}\n========================\n\n'.format(message)
try:
from fabric.colors import cyan
sys.stderr.write(cyan(msg))
except ImportError:
print(msg)
def _python(cmd): def _python(cmd):
return env.venv_path.rstrip('/') + '/bin/python ' + cmd return env.venv_path.rstrip('/') + '/bin/python ' + cmd
def dep(): def dep():
_display_message('Collect static (local)') _display_message('Collect static (local)')
@ -48,28 +63,6 @@ def dep():
run(_python('manage.py collectstatic --ignore cache --noinput')) run(_python('manage.py collectstatic --ignore cache --noinput'))
def db():
with cd(env.site_path):
_display_message('Sync DB and migrate')
################
run(_python('manage.py syncdb'))
run(_python('manage.py migrate'))
def re():
with cd(env.site_path):
_display_message('Kill gunicorn process')
################
sudo('supervisorctl stop snipt')
_display_message('Restart gunicorn process')
################
sudo('supervisorctl start snipt')
def db_backup(): def db_backup():
filename = datetime.datetime.now().strftime('%h-%d-%y__%I-%M-%S_%p.pgdump') filename = datetime.datetime.now().strftime('%h-%d-%y__%I-%M-%S_%p.pgdump')
@ -85,14 +78,75 @@ def db_backup():
local('rm {}'.format(filename)) local('rm {}'.format(filename))
def _display_message(message, extra_line=True): def db():
if extra_line: with cd(env.site_path):
msg = '\n{}\n========================\n\n'.format(message)
else: _display_message('Sync DB and migrate')
msg = '{}\n========================\n\n'.format(message) ################
try:
from fabric.colors import cyan run(_python('manage.py syncdb'))
sys.stderr.write(cyan(msg)) run(_python('manage.py migrate'))
except ImportError:
print(msg) def gravatars():
from fabric.contrib import django
django.settings_module('settings')
from django.contrib.auth.models import User
import requests
_display_message('Updating all users\' Gravatar flags')
################
for user in User.objects.all().order_by('id'):
_display_message('{}. {}'.format(user.pk, user.username.encode('ascii', 'ignore')))
################
email_md5 = hashlib.md5(user.email.lower()).hexdigest()
print 'Email MD5: {}'.format(email_md5)
if user.profile.has_gravatar == False:
greq = requests.get('http://en.gravatar.com/{}.json'.format(email_md5))
if greq.json() == 'User not found':
has_gravatar = False
else:
has_gravatar = True
else:
has_gravatar = True
print 'Already had Gravatar! Not checking again.'
if has_gravatar and not user.profile.has_gravatar:
profile = user.profile
profile.has_gravatar = True
profile.save()
try:
from fabric.colors import green, red
if has_gravatar:
print 'Has Gravatar: {}'.format(green(has_gravatar))
else:
print 'Has Gravatar: {}'.format(red(has_gravatar))
except ImportError:
print 'Has Gravatar: {}'.format(has_gravatar)
def re():
with cd(env.site_path):
_display_message('Kill gunicorn process')
################
sudo('supervisorctl stop snipt')
_display_message('Restart gunicorn process')
################
sudo('supervisorctl start snipt')

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1 B

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@ -76,7 +76,7 @@ STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.AppDirectoriesFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
) )
SECRET_KEY = '' SECRET_KEY = 'changethis'
INSTALLED_APPS = ( INSTALLED_APPS = (
'gunicorn', 'gunicorn',

View File

@ -59,7 +59,7 @@
<div class="inner"> <div class="inner">
{% for coder in coders %} {% for coder in coders %}
<a href="/{{ coder.username }}/" title="{{ coder.username }}"> <a href="/{{ coder.username }}/" title="{{ coder.username }}">
<img alt="{{ coder.username }}" title="{{ coder.username }}" src="https://secure.gravatar.com/avatar/{{ coder.email_md5 }}?s=50" /> <img alt="{{ coder.username }}" title="{{ coder.username }}" src="https://secure.gravatar.com/avatar/{{ coder.email_md5 }}?s=50&d=https://snipt.net/static/img/default-gravatar-homepage.gif" />
</a> </a>
{% endfor %} {% endfor %}
</div> </div>

View File

@ -18,7 +18,7 @@ from settings_local import STRIPE_SECRET_KEY
@render_to('homepage.html') @render_to('homepage.html')
def homepage(request): def homepage(request):
random_users = UserProfile.objects.filter(has_gravatar=True).order_by('?')[:100] random_users = UserProfile.objects.filter(has_gravatar=True).order_by('?')[:50]
coders = [] coders = []
for random_user in random_users: for random_user in random_users: