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 post(self): user=self.request.get('user',None) user=urllib.unquote(user) url=self.request.get('url',None) domain=self.request.get('domain',None) title=self.request.get('title',None) share_mode=self.request.get('share_mode',None) if not RequestUtils.checkUrl(url): logging.info('skipping since url is not good!') return lu = LinkUtils() link_info = lu.getLinkInfo(url) description = link_info["d"] embeded = link_info["e"] logging.info('got post title %s' % title) title_new = link_info["t"] if title is None and title_new is not None and len(title_new) > 0: title = title_new if title is None or title == 'None' or title == 'null': title=LinkUtils.getLinkTitle(url) if title is not None: title = title[:199] logging.info('final link title %s' %title) logging.info("link info desc: %s embede: %s" %( description, embeded)) version=self.request.get('version',None) client=self.request.get('client',None) selection = self.request.get('selection', None) user_agent = self.request.get('user_agent',None) UserScoreUtility.updateLinkScore(user,url) UserScoreUtility.updateDomainScore(user, domain) taskqueue.add(url='/user/badge/task', queue_name='badge-queue', params={'url':url, 'domain':domain, 'user':user, 'version': version, 'client': client}) taskqueue.add(url='/link/traction/task', queue_name='link-queue', params={'url':url, 'user': user, 'title': title}) taskqueue.add(url='/link/recommendation/task', queue_name='default', params={'url':url }) name = "url" generic_counter.increment(name) url_cnt = generic_counter.get_count(name) logging.info("total url count %s " % url_cnt) e = EncodeUtils() enbased=e.encode(url_cnt) url_encode26 = e.enbase(enbased) logging.info("url encode: %s and enbase : %s" % (enbased, url_encode26)) url_hash = LinkUtils.getUrlHash(url) today = datetime.datetime.now().date() model = SessionModel.gql('WHERE instaright_account = :1 and url_hash = :2 and date > :3', user, url_hash, today).get() new_entity=False if model is None: logging.info('did not find save dafined by: %s %s for date %s', user, url, str(today)) model = SessionModel() new_entity=True else: logging.info('existing url(key %s) updating certain params' %str(model.key())) logging.info('link: %s title: %s' %(url, title)) try: #remove for local testing model.ip = self.request.remote_addr model.instaright_account = user model.date = datetime.datetime.now() if new_entity == True: model.url = url model.url_hash = url_hash model.url_counter_id = url_cnt model.url_encode26 = url_encode26 model.title = title model.user_agent=user_agent model.domain = domain model.short_link = None model.feed_link = None model.version = version model.client = client model.selection = selection model.embeded = embeded while True: timeout_ms= 100 try: model.put() break except datastore_errors.Timeout: logging.info('model save timeout retrying in %s' % timeout_ms) time.sleep(timeout_ms) timeout_ms *= 2 logging.info('send link : url_hash %s title %s user_id %s updated %s client: %s' %(model.url_hash, model.title, str(model.key()), str(model.date), model.client)) except BadValueError, apiproxy_errors.DeadlineExceededError: e0, e1 = sys.exc_info()[0], sys.exc_info()[1] logging.error('error while saving url %s ( %s, %s)' % (url, e0, e1))