def get(self): memcache_key = 'update_user_badges' cached_badges = memcache.get(memcache_key) #if cached_badges is not None: # logging.info('badge from cache') ## u_badges = cached_badges #else: user_badges = UserBadge.all() u_badges = [ u for u in user_badges if u.badge_property is None ] memcache.set(memcache_key, u_badges) logging.info('total badges %s' % len(u_badges)) for ub in u_badges: if ub.badge_property is not None: logging.info('skipping already defined badge_p') continue b = Badges.gql('WHERE badge_label = :1 ', ub.badge).get() if b is None: b = Badges() b.badge_label = ub.badge b.badge_icon = ub.badge b.put() ud = UserDetails.gql('WHERE instaright_account = :1' , ub.user).get() if ud is None: ud = UserDetails() ud.instaright_account = ub.user ud.put() ub.badge_property=b.key() ub.user_property = ud.key() ub.put()
def get(self, user): if user is None or len(user) == 0: logging.error('Empty user. Skipping') return user_decoded = urllib.unquote(user) logging.info('user: %s' %user_decoded) ud=UserDetails.gql('WHERE instapaper_account = :1' , user_decoded).get() if ud is None: logging.info('non existing user. redirect to home') self.redirect('/') return # sanity check if ud.instaright_account is None: ud.instaright_account = ud.instapaper_account ud.put() memcache_key ='user_info_' + user_decoded+'_'+str(datetime.datetime.now().date()) sessions = SessionModel.gql('WHERE instaright_account = :1 ORDER by date desc ' , user_decoded).fetch(100) links = [ s for s in sessions if s is not None ] cached_info = memcache.get(memcache_key) if cached_info: logging.info('getting from cache' ) template_variables = {'user':cached_info,'links':links} path= os.path.join(os.path.dirname(__file__), 'templates/user_info.html') self.response.headers["Content-type"] = "text/html" self.response.out.write(template.render(path,template_variables)) return user_detail= UserDetails.gql('WHERE mail = :1', user_decoded).get() if user_detail is None: logging.info('new user %s added to queue' %user_decoded) fetch_url = '/user/'+user+'/fetch' taskqueue.add(queue_name='user-info', url= fetch_url) ud = UserDetails() ud.name = user_decoded ud.instapaper_account = user_decoded ud.instaright_account = user_decoded ud.links_added = SessionModel.countAllForUser(user_decoded) # tmp put until we find more info for user ud.put() template_variables = {'user':ud, 'links': links} path= os.path.join(os.path.dirname(__file__), 'templates/user_info.html') self.response.headers["Content-type"] = "text/html" self.response.headers["Accept-Charset"] = "utf-8" self.response.out.write(template.render(path,template_variables)) return if user_detail.instaright_account is None: user_detail.instaright_account = user_decoded user_detail.put() memcache.set(memcache_key, user_detail) template_variables = {'user':user_detail, "links" : links} path= os.path.join(os.path.dirname(__file__), 'templates/user_info.html') self.response.headers["Content-type"] = "text/html" self.response.out.write(template.render(path,template_variables))
def getCurrentScore(cls, user): if user is None: return None userDetails = UserDetails.gql('WHERE instaright_account = :1', user).get() if userDetails is None: logging.info('no user with instaright account %s' % user) userDetails = UserDetails.gql('WHERE instapaper_account = :1', user).get() if userDetails is None: userDetails=UserDetails() userDetails.instapaper_account=user userDetails.instaright_account=user userDetails.put() now =datetime.datetime.now().date() currentScore=ScoreUsersDaily.gql('WHERE user = :1 and date = :2', userDetails.key(), now).get() if currentScore is None: currentScore = ScoreUsersDaily() currentScore.user=userDetails.key() currentScore.date = now return currentScore
def post(self): user=self.request.get('user', None) url=self.request.get('url', None) domain=self.request.get('domain', None) version=self.request.get('version', None) client=self.request.get('client', None) badge=None if user is None: logging.info('unknown user skipping') return user_details = UserDetails.gql('WHERE instaright_account = :1', user).get() if user_details is None: user_details = UserDetails() user_details.instaright_account = user user_details.put() client_badge = UserBadge.gql('WHERE badge = :1 and user = :2', client, user).get() if client_badge is None: badge = Badges.gql('WHERE badge_label = :1' , client).get() if badge is None: mail.send_mail(sender='*****@*****.**', to='*****@*****.**', subject='Unknown client!', html='user %s saved link %s using unrecognised client %s' %( user,url, client ), body='user %s saved link %s using unrecognised client %s' %( user,url, client)) return client_badge = UserBadge() client_badge.badge = client client_badge.badge_property = badge.key() client_badge.date = datetime.datetime.now().date() client_badge.user = user client_badge.user_property = user_details.key() client_badge.put() if version is not None and len(version) == 0: version = None currentBadge = memcache.get('badge_'+user) if currentBadge is not None and (currentBadge == '1' or currentBadge == '2' or currentBadge == '3' or currentBadge == '5'): logging.info('for user %s already using full day badge: %s' %(user,currentBadge)) return badger=BadgeUtil.getBadger(user, url, domain, version) if badger is None: logging.info('no badger initialized. skipping') return badge=badger.getBadge() if badge is not None: # midnight timestamp - memcache expiration midnight=datetime.datetime.now().date() + datetime.timedelta(days=1) midnight_ts=time.mktime(midnight.timetuple()) memcache.set('badge_'+user, badge, time=midnight_ts) logging.info('setting badge cache: %s for user badge_%s valid until midnight %s' % (badge,user,midnight_ts)) existingBadge=UserBadge.gql('WHERE badge = :1 and user = :2 and date = :3', badge, user, datetime.datetime.now().date()).get() if existingBadge is not None: self.response.out.write('request processed') return b = Badges.gql('WHERE badge_label = :1' , badge).get() if b is None: b = Badges() b.badge_label = badge b.badge_icon = badge b.put() userBadge=UserBadge() userBadge.user=user userBadge.badge=badge userBadge.badge_property = b.key() userBadge.user_property = user_details.key() userBadge.put() UserScoreUtility.updateBadgeScore(user, badge)
def gather_info(self, user): if not '@' in user: logging.info('not email address: skipping ...') user_detail = UserDetails() return user_detail user_detail = UserDetails.gql('where instapaper_account = :1' , user).get() if user_detail is None: user_detail = UserDetails() if user_detail.mail is None: logging.info('updating user details setting user mail %s' % user) user_detail.mail = user if user_detail.instaright_account is None: logging.info('update instaright account for user %s' % user) user_detail.instaright_account = user #PREPARE for request #time_milis = time.time() * 1000 logging.info("user encoded %s" % user) RAPPORTIVE_LINK = "https://rapportive.com/contacts/email/%s" % user logging.info('fetchin url:%s' %RAPPORTIVE_LINK) #TILL here try: response = urlfetch.fetch(RAPPORTIVE_LINK) except: logging.info('error downloading rapportive info. reTRYing') try: response = urlfetch.fetch(RAPPORTIVE_LINK) except: e, e1 = sys.exc_info()[0], sys.exc_info()[1] return user_detail if response.status_code == 200: json_string_response = simplejson.loads(simplejson.dumps(response.content)) json = simplejson.loads(json_string_response) name = None try: name = json["contact"]["name"] except: logging.info("unable to gather name from rapportive") if name is None or len(name) < 1 or unicode(name).startswith('Thanks') or unicode(name).startswith('Sorry'): logging.info('did not find name for account %s' % user) return user_detail logging.info('name: %s' % unicode(name)) user_detail.name = unicode(name) memberships = json["contact"]["memberships"] memships = {} for m in memberships: service_name = m['site_name'].lower().replace(' ','_').replace('.','_') memships[service_name]=m['profile_url'] try: setattr(user_detail, service_name, m['profile_url']) logging.info('service %s profile %s' % (service_name, m['profile_url'])) if service_name == 'twitter' and not user_detail.twitter_request_sent: # send twitter request and need to put since we need key for follow user_detail.twitter_request_sent=True user_detail.put() taskqueue.add(url='/util/twitter/follow/'+str(user_detail.key()), queue_name='twit-queue') except: logging.error('memberships error %s ::: more details %s ' % (sys.exc_info()[0], sys.exc_info()[1])) user_detail.social_data = simplejson.dumps(memships) occupations = json["contact"]["occupations"] occups = [] for o in occupations: tmp_o = o['job_title'] + " ^_^ " + o['company'] occups.append(tmp_o) user_detail.occupations = '; '.join(occups) logging.info('occupations %s' %user_detail.occupations) avatar = None try: avatar = json["contact"]["images"][0]["url"] logging.info('avatar %s' % avatar) except: logging.info('no image info cause of %s' % sys.exc_info()[0]) if avatar is not None and len(avatar) > 0: user_detail.avatar = avatar location = None try: location = json["contact"]["location"] except: logging.info('no location info cause of %' %sys.exc_info()[0]) if location is not None and len(location): location_lines = location.splitlines() user_detail.location = ' '.join(location_lines) return user_detail