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 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