def test_debase(self): e = EncodeUtils() debased = e.debase('lhskyi') self.assertEquals(debased,134217728) debased = e.debase('fqwfme') self.assertEquals(debased, 67108864) debased = e.debase('qyoqkm') self.assertEquals(debased, 201326592) debased = e.debase('cvlctc') self.assertEquals(debased, 33554432)
def test_incr(self): name='url' generic_counter.increment(name) url_counter_id = generic_counter.get_count(name) non_none = True if url_counter_id is None: non_none = False self.assertEquals(True, non_none) e = EncodeUtils() url_encode26 = e.encode(url_counter_id) logging.info('counter %s encoded %s' %( url_counter_id, url_encode26))
def test_update_encode(self): e = EncodeUtils() ss = SessionModel.gql('ORDER by url_counter_id desc').fetch(1000) test = SessionModel.countAllForUser('*****@*****.**') logging.info('count for user %s' % test) logging.info("fetch %s " %len(ss)) for s in ss: cnt = s.url_counter_id encode26 = e.enbase(cnt) logging.info("e26: before %s after %s" % (s.url_encode26, encode26)) s.url_encode26 = encode26 #s.put() self.assertEquals(True, True)
def get(self): e = EncodeUtils() query = SessionModel.all() query.filter("url_counter_id <= " ,6000) query.order("-url_counter_id") results=query.fetch(1000) logging.info("fetch %s " %query.count()) for s in results: cnt = s.url_counter_id encoded = e.encode(cnt) enbased26 = e.enbase(encoded) logging.info("cnt: %s => encoded: %s enbased %s (before %s)" % (str(s.url_counter_id), str(encoded), enbased26, s.url_encode26 )) s.url_encode26 = enbased26 s.put()
def test_encode(self): e = EncodeUtils() encoded = e.encode(1) self.assertEquals(encoded, 134217728) encoded = e.encode(2) self.assertEquals(encoded, 67108864) encoded = e.encode(3) self.assertEquals(encoded, 201326592) encoded = e.encode(4) self.assertEquals(encoded, 33554432) encoded = e.encode(55) self.assertEquals(encoded, 247463936)
def test_decode(self): e = EncodeUtils() decoded = e.decode(134217728) self.assertEquals(decoded, 1)
def test_enbase(self): e = EncodeUtils() enbased = e.enbase(134217728) self.assertEquals(enbased, 'lhskyi')
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))