Skip to content

Commit

Permalink
script for updating user details
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe committed May 9, 2011
1 parent 3a61abb commit 1a52235
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,16 @@ run this:

./bin/run_migrations.py

Adding more indexing is basically the same as running migrations.
Adding more indexing is basically the same as running migrations.


Updating user details
---------------------

To update the user details, run the script
``bin/update-user-details.py`` periodically. You can specify how many
updates to make maximum. Default is 100. Example usage:

./bin/update-user-details.py


2 changes: 1 addition & 1 deletion bin/run_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@
print '\n'.join(['\t%s'%x for x in locals().keys()
if re.findall('[A-Z]\w+|db|con', x)])
print "Database available as 'db'"
code.interact(local=locals())
code.interact(local=locals())
52 changes: 52 additions & 0 deletions bin/update-user-details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python
import datetime
from urllib import urlopen
import json

def pull_details(username):
url = "http://github.com/api/v2/json/user/show/%s" % username
content = urlopen(url).read()
try:
return json.loads(content)['user']
except KeyError:
return None


def run(max_updates=5):
from apps.main.models import User
from mongokit import Connection
con = Connection()
con.register([User])
import settings
db = con[settings.DEFAULT_DATABASE_NAME]
print db.User.find().count(), "users in total"
today = datetime.date.today()
today = datetime.datetime(*(today.timetuple()[:6]))
search = {'modify_date': {'$lt': today}}
print db.User.find(search).count(), "left to update today"
for user in db.User.find(search).sort('modify_date', 1).limit(max_updates):
print repr(user.login), user.modify_date
details = pull_details(user.login)
if details is None:
print "FAIL!"
print "??? http://github.com/api/v2/json/user/show/%s" % user.login
continue
for key in 'name company email gravatar_id'.split():
if key in details:
if getattr(user, key, '') != details[key]:
print "\t", key, repr(getattr(user, key, '*blank*')),
print '-->', repr(details[key])
setattr(user, key, details[key])
user.modify_date = datetime.datetime.now()
user.save()

def main(*args):
max_updates = 100
if args and args[0].isdigit():
max_updates = int(args[0])
run(max_updates=max_updates)
return 0

if __name__ == '__main__':
import sys
sys.exit(main(*sys.argv[1:]))

0 comments on commit 1a52235

Please sign in to comment.