Commits

hoop  committed 7948543

Add script to cache recommendations

  • Participants
  • Parent commits a727304

Comments (0)

Files changed (1)

File cache-recs.py

+#!/usr/bin/env python
+
+"""Make recommendations based on profile prefs"""
+
+from pymongo import Connection
+from pymongo.code import Code
+from pymongo import ASCENDING,DESCENDING
+from bson.objectid import ObjectId
+from funcs import get_js
+import config
+
+def main():
+    conn = Connection(config.mongo_seeds)
+    db = conn[config.mongo_database]
+
+    for profile in db.profiles.find():
+        print 'Making recommendations for %s' % profile['name']
+        scope = {'user_prefs':profile['prefs']}
+
+        # find max(x) where x is any attribute
+        maxes = dict()
+        for key in profile['prefs'].iterkeys():
+            if key not in maxes:
+                maxes[key] = [c['tags'][key] for c in db.candidates.find().sort('tags.'+key,DESCENDING).limit(1)][0]
+        scope['maxes'] = maxes
+
+        mapf = get_js('js/vectorize.map.js')
+        reducef = get_js('js/vectorize.reduce.js')
+        mr_col = db.candidates.map_reduce(mapf, reducef, scope=scope)
+
+        # Cache the recommendations
+        profile['recs'] = [r for r in mr_col.find().sort('value',ASCENDING).limit(10)]
+        self.db.profiles.save(profile)
+
+if __name__ == '__main__':
+    try:
+        main()
+    except KeyboardInterrupt:
+        quit()
+
+