def get(self): output = memcache.get('feed_read_output') if output is None: self.values['site_domain'] = self.site.domain self.values['site_name'] = self.site.title self.values['site_slogan'] = self.site.slogan self.values['feed_url'] = 'http://' + self.values['site_domain'] + '/read.xml' self.values['site_updated'] = datetime.datetime.now(pytz.timezone('Asia/Shanghai')) topics = memcache.get('feed_home') if topics is None: #q = db.GqlQuery("SELECT * FROM Topic ORDER BY created DESC LIMIT 10") q = Topic.select(orderBy='-created').limit(10) topics = [] IGNORED = ['newbie', 'in', 'flamewar', 'pointless', 'tuan', '528491', 'chamber', 'autistic', 'blog', 'love', 'flood'] for topic in q: if topic.node.name not in IGNORED: topics.append(topic) memcache.set('feed_home', topics, 3600) self.values['topics'] = topics self.values['feed_title'] = self.site.title path = os.path.join(os.path.dirname(__file__), 'tpl', 'feed') t=self.get_template(path, 'read.xml') output = t.render(self.values) memcache.set('feed_read_output', output, 3600) self.set_header('Content-type', 'application/xml;charset=UTF-8') self.write(output)
def CheckAuth(handler): ip = GetIP(handler) cookies = handler.request.cookies if 'auth' in cookies: auth = cookies['auth'].value member_num = memcache.get(auth) if (member_num > 0): member = memcache.get('Member_' + str(member_num)) if member is None: #q = db.GqlQuery("SELECT * FROM Member WHERE num = :1", member_num) q = Member.selectBy(num=member_num) if q.count() == 1: member = q[0] memcache.set(auth, member.num) memcache.set('Member_' + str(member_num), member) else: member = False if member: member.ip = ip return member else: #q = db.GqlQuery("SELECT * FROM Member WHERE auth = :1", auth) q = Member.selectBy(auth=auth) if (q.count() == 1): member_num = q[0].num member = q[0] memcache.set(auth, member_num) memcache.set('Member_' + str(member_num), member) member.ip = ip return member else: return False else: return False
def get(self): output = memcache.get('feed_read_output') if output is None: self.values['site_domain'] = self.site.domain self.values['site_name'] = self.site.title self.values['site_slogan'] = self.site.slogan self.values['feed_url'] = 'http://' + self.values[ 'site_domain'] + '/read.xml' self.values['site_updated'] = datetime.datetime.now( pytz.timezone('Asia/Shanghai')) topics = memcache.get('feed_home') if topics is None: #q = db.GqlQuery("SELECT * FROM Topic ORDER BY created DESC LIMIT 10") q = Topic.select(orderBy='-created').limit(10) topics = [] IGNORED = [ 'newbie', 'in', 'flamewar', 'pointless', 'tuan', '528491', 'chamber', 'autistic', 'blog', 'love', 'flood' ] for topic in q: if topic.node.name not in IGNORED: topics.append(topic) memcache.set('feed_home', topics, 3600) self.values['topics'] = topics self.values['feed_title'] = self.site.title path = os.path.join(os.path.dirname(__file__), 'tpl', 'feed') t = self.get_template(path, 'read.xml') output = t.render(self.values) memcache.set('feed_read_output', output, 3600) self.set_header('Content-type', 'application/xml;charset=UTF-8') self.write(output)
def member(self): if not hasattr(self, "_member"): ip = False member = False if 'X-Real-IP' in self.request.headers: ip = self.request.headers['X-Real-IP'] else: ip = self.request.remote_ip #cookies = Cookies(self, max_age = 86400 * 14, path = '/') auth = self.get_cookie('auth') #if 'auth' in cookies: if auth is not None: #auth = cookies['auth'] member_num = memcache.get(auth) if member_num > 0: member = memcache.get('Member_' + str(member_num)) if member is None: #q = db.GqlQuery("SELECT * FROM Member WHERE num = :1", member_num) q = Member.selectBy(num=member_num) if q.count() == 1: member = q[0] memcache.set('Member_' + str(member_num), member, 86400 * 14) if member: member.ip = ip else: #q = db.GqlQuery("SELECT * FROM Member WHERE auth = :1", auth) q = Member.selectBy(auth=auth) if (q.count() == 1): member_num = q[0].num member = q[0] memcache.set(auth, member_num, 86400 * 14) memcache.set('Member_' + str(member_num), member, 86400 * 14) member.ip = ip self._member = member return self._member
def CheckAuth(handler): ip = GetIP(handler) cookies = handler.request.cookies if 'auth' in cookies: auth = cookies['auth'].value member_num = memcache.get(auth) if (member_num > 0): member = memcache.get('Member_' + str(member_num)) if member is None: #q = db.GqlQuery("SELECT * FROM Member WHERE num = :1", member_num) q = Member.selectBy(num=member_num) if q.count() == 1: member = q[0] memcache.set(auth, member.num) memcache.set('Member_' + str(member_num), member) else: member = False if member: member.ip = ip return member else: #q = db.GqlQuery("SELECT * FROM Member WHERE auth = :1", auth) q = Member.selectBy(auth=auth) if (q.count() == 1): member_num = q[0].num member = q[0] memcache.set(auth, member_num) memcache.set('Member_' + str(member_num), member) member.ip = ip return member else: return False else: return False
def get(self, screen_name): site = GetSite() member = CheckAuth(self) if member: if member.twitter_oauth == 1: template_values = {} template_values['site'] = site template_values['rnd'] = random.randrange(1, 100) template_values['member'] = member l10n = GetMessages(self, member, site) template_values['l10n'] = l10n template_values[ 'page_title'] = site.title + u' › Twitter › ' + screen_name template_values['screen_name'] = screen_name access_token = OAuthToken.from_string( member.twitter_oauth_string) twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token) rate_limit = memcache.get( str(member.twitter_id) + '::rate_limit') if rate_limit is None: try: rate_limit = twitter.GetRateLimit() memcache.set( str(member.twitter_id) + '::rate_limit', rate_limit, 60) except: logging.info('Failed to get rate limit for @' + member.twitter_screen_name) template_values['rate_limit'] = rate_limit cache_tag = 'twitter::' + screen_name + '::home' statuses = memcache.get(cache_tag) if statuses is None: statuses = twitter.GetUserTimeline(user=screen_name, count=50) i = 0 for status in statuses: statuses[i].source = statuses[i].source.replace( '<a', '<a class="dark"') statuses[i].datetime = datetime.datetime.fromtimestamp( time.mktime( time.strptime(status.created_at, '%a %b %d %H:%M:%S +0000 %Y'))) statuses[i].text = twitter.ConvertMentions(status.text) #statuses[i].text = twitter.ExpandBitly(status.text) i = i + 1 memcache.set(cache_tag, statuses, 120) template_values['statuses'] = statuses path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop') t = self.get_template(path, 'twitter_user.html') self.finish(t.render(template_values)) else: self.redirect('/settings') else: self.redirect('/')
def get(self): member = CheckAuth(self) site = GetSite() if member: if member.twitter_oauth == 1: template_values = {} template_values['site'] = site template_values['rnd'] = random.randrange(1, 100) template_values['member'] = member l10n = GetMessages(self, member, site) template_values['l10n'] = l10n template_values[ 'page_title'] = site.title + u' › Twitter › Direct Messages › Inbox' access_token = OAuthToken.from_string( member.twitter_oauth_string) twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token) rate_limit = memcache.get( str(member.twitter_id) + '::rate_limit') if rate_limit is None: try: rate_limit = twitter.GetRateLimit() memcache.set( str(member.twitter_id) + '::rate_limit', rate_limit, 60) except: logging.info('Failed to get rate limit for @' + member.twitter_screen_name) template_values['rate_limit'] = rate_limit cache_tag = 'member::' + str( member.num) + '::twitter::dm::inbox' messages = memcache.get(cache_tag) if messages is None: messages = twitter.GetDirectMessages() i = 0 for message in messages: messages[i].datetime = datetime.datetime.fromtimestamp( time.mktime( time.strptime(message.created_at, '%a %b %d %H:%M:%S +0000 %Y'))) messages[i].text = twitter.ConvertMentions( message.text) #statuses[i].text = twitter.ExpandBitly(status.text) i = i + 1 memcache.set(cache_tag, messages, 120) template_values['messages'] = messages path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop') t = self.get_template(path, 'twitter_dm_inbox.html') self.finish(t.render(template_values)) else: self.redirect('/settings') else: self.redirect('/')
def __init__(self): self.sid = None self.key = None self.session = None string_cookie = os.environ.get('HTTP_COOKIE', '') self.cookie = Cookie.SimpleCookie() self.cookie.load(string_cookie) # check for existing cookie if self.cookie.get(COOKIE_NAME): self.sid = self.cookie[COOKIE_NAME].value self.key = "session-" + self.sid self.session = memcache.get(self.key) if self.session is None: logging.info("Invalidating session " + self.sid) self.sid = None self.key = None if self.session is None: self.sid = str(random.random())[5:] + str(random.random())[5:] self.key = "session-" + self.sid logging.info("Creating session " + self.key) self.session = dict() memcache.add(self.key, self.session, 3600) self.cookie[COOKIE_NAME] = self.sid self.cookie[COOKIE_NAME]['path'] = DEFAULT_COOKIE_PATH # Send the Cookie header to the browser print self.cookie
def get(self, node_name): node_name = node_name.lower() site = GetSite() node = GetKindByName('Node', node_name) if node is False: return self.write('node not found') output = memcache.get('feed_node_' + node_name) if output is None: template_values = {} template_values['site'] = site template_values['site_domain'] = site.domain template_values['site_name'] = site.title template_values['site_slogan'] = site.slogan template_values['feed_url'] = 'http://' + template_values['site_domain'] + '/index.xml' template_values['site_updated'] = datetime.datetime.now(pytz.timezone('Asia/Shanghai')) #q = db.GqlQuery("SELECT * FROM Topic WHERE node = :1 ORDER BY created DESC LIMIT 10", node) q = Topic.selectBy(node=node).orderBy('-created').limit(10) topics = [] for topic in q: topics.append(topic) template_values['topics'] = topics template_values['feed_title'] = site.title + u' › ' + node.title path = os.path.join(os.path.dirname(__file__), 'tpl', 'feed') t=self.get_template(template_values) output = t.render(self.values) memcache.set('feed_node_' + node.name, output, 7200) self.set_header('Content-type', 'application/xml;charset=UTF-8') self.write(output)
def __init__(self): self.sid = None self.key = None self.session = None string_cookie = os.environ.get('HTTP_COOKIE', '') self.cookie = Cookie.SimpleCookie() self.cookie.load(string_cookie) # check for existing cookie if self.cookie.get(COOKIE_NAME): self.sid = self.cookie[COOKIE_NAME].value self.key = "session-" + self.sid self.session = memcache.get(self.key) if self.session is None: logging.info("Invalidating session "+self.sid) self.sid = None self.key = None if self.session is None: self.sid = str(random.random())[5:]+str(random.random())[5:] self.key = "session-" + self.sid logging.info("Creating session "+self.key); self.session = dict() memcache.add(self.key, self.session, 3600) self.cookie[COOKIE_NAME] = self.sid self.cookie[COOKIE_NAME]['path'] = DEFAULT_COOKIE_PATH # Send the Cookie header to the browser print self.cookie
def site(self): if not hasattr(self, "_site"): site = memcache.get('site') if site is not None: self._site = site else: #q = db.GqlQuery("SELECT * FROM Site WHERE num = 1") q = Site.selectBy(num=1) if q.count() == 1: site = q[0] if site.l10n is None: site.l10n = 'en' if site.meta is None: site.meta = '' memcache.set('site', site, 86400) self._site = site else: site = Site() site.num = 1 site.title = 'V2EX' site.domain = 'v2ex.appspot.com' site.slogan = 'way to explore' site.l10n = 'en' site.description = '' site.meta = '' site.sync() store.commit() #jon add memcache.set('site', site, 86400) self._site = site return self._site
def get(self, node_name): node_name = node_name.lower() site = GetSite() node = GetKindByName('Node', node_name) if node is False: return self.write('node not found') output = memcache.get('feed_node_' + node_name) if output is None: template_values = {} template_values['site'] = site template_values['site_domain'] = site.domain template_values['site_name'] = site.title template_values['site_slogan'] = site.slogan template_values['feed_url'] = 'http://' + template_values[ 'site_domain'] + '/index.xml' template_values['site_updated'] = datetime.datetime.now( pytz.timezone('Asia/Shanghai')) #q = db.GqlQuery("SELECT * FROM Topic WHERE node = :1 ORDER BY created DESC LIMIT 10", node) q = Topic.selectBy(node=node).orderBy('-created').limit(10) topics = [] for topic in q: topics.append(topic) template_values['topics'] = topics template_values['feed_title'] = site.title + u' › ' + node.title path = os.path.join(os.path.dirname(__file__), 'tpl', 'feed') t = self.get_template(template_values) output = t.render(self.values) memcache.set('feed_node_' + node.name, output, 7200) self.set_header('Content-type', 'application/xml;charset=UTF-8') self.write(output)
def get(self, platform, soldier): ua = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13' referer = 'http://bfbcs.com/' + platform cache_tag = 'bfbcs::' + platform + '/' + soldier raw = memcache.get(cache_tag) url = 'http://bfbcs.com/stats_' + platform + '/' + soldier if raw is None: response = urlfetch.fetch(url, headers={'User-Agent' : ua, 'Referer' : referer }) raw = response.content memcache.set(cache_tag, raw, 600) pcode = re.findall('([a-z0-9]{32})', raw) self.response.out.write('<strong>PCODE</strong> ' + str(pcode[0]) + '<br />') if len(pcode) == 1: pcode = pcode[0] payload = 'request=addplayerqueue&pcode=' + pcode self.response.out.write('<strong>PAYLOAD</strong> ' + payload + ' (' + str(len(payload))+ ' bytes)<br />') headers = {'User-Agent' : ua, 'Referer' : url, 'X-Requested-With' : 'XMLHttpRequest', 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8', 'Content-Length' : '61', 'Accept' : 'application/json, text/javascript, */*', 'Accept-Language' : 'en-us,en;q=0.5', 'Accept-Encoding' : 'gzip,deflate', 'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'Keep-Alive' : 115, 'Host' : 'bfbcs.com', 'Pragma' : 'no-cache', 'Cache-Control' : 'no-cache', 'Cookie' : '__utma=7878317.1843709575.1297205447.1298572822.1298577848.12; __utmz=7878317.1297205447.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); sessid=enqd028n30d2tr4lv4ned04qi0; __utmb=7878317.21.10.1298577848; __utmc=7878317' } response = urlfetch.fetch(url, payload=payload, headers=headers, method='POST') if response.status_code == 500: response = urlfetch.fetch(url, payload=payload, headers=headers, method='POST') if response.status_code == 500: self.write('<strong>FAILED</strong>') else: self.write('<strong>RESULT</strong> OK ' + response.content) else: self.write('<strong>RESULT</strong> OK ' + response.content)
def get(self, private_token): n = memcache.get('n_' + private_token) if n is not None: self.values['notification'] = n self.set_header('Content-type', 'application/xml;charset=UTF-8') self.values['member'] = self.member self.finalize(template_name='notifications', template_root='feed', template_type='xml') else: #q = db.GqlQuery("SELECT * FROM Member WHERE private_token = :1", private_token) q = Member.selectBy(private_token=private_token) count = q.count() if count > 0: member = q[0] #q = db.GqlQuery("SELECT * FROM Notification WHERE for_member_num = :1 ORDER BY num DESC LIMIT 20", member.num) q = Notification.selectBy(for_member_num=member.num).orderBy('-num').limit(20) notifications = [] i = 0 for n in q: if n.type == 'reply': n.title = u'' + n.member.username + u' 在 ' + self.escape(n.label1) + u' 里回复了你' n.text = u'<a href="/member/' + n.member.username + u'"><strong>' + n.member.username + u'</strong></a> 在 <a href="' + n.link1 + '">' + self.escape(n.label1) + u'</a> 里回复了你' notifications.append(n) if n.type == 'mention_reply': n.title = u'' + n.member.username + u' 在回复 ' + self.escape(n.label1) + u' 时提到了你' n.text = u'<a href="/member/' + n.member.username + u'"><strong>' + n.member.username + u'</strong></a> 在回复 <a href="' + n.link1 + '">' + self.escape(n.label1) + u'</a> 时提到了你' notifications.append(n) i = i + 1 self.values['notifications'] = notifications memcache.set('n_' + private_token, notifications, 600) self.set_header('Content-type', 'application/xml;charset=UTF-8') self.values['site'] = GetSite() self.values['member'] = member self.finalize(template_name='notifications', template_root='feed', template_type='xml')
def hasFavorited(self, something): if type(something).__name__ == 'Node': n = 'r/n' + str(something.num) + '/m' + str(self.num) r = memcache.get(n) if r: return r else: #q = db.GqlQuery("SELECT * FROM NodeBookmark WHERE node =:1 AND member = :2", something, self) q = NodeBookmark.selectBy(node=something, member=self) if q.count() > 0: memcache.set(n, True, 86400 * 14) return True else: memcache.set(n, False, 86400 * 14) return False else: if type(something).__name__ == 'Topic': n = 'r/t' + str(something.num) + '/m' + str(self.num) r = memcache.get(n) if r: return r else: #q = db.GqlQuery("SELECT * FROM TopicBookmark WHERE topic =:1 AND member = :2", something, self) q = TopicBookmark.selectBy(topic=something, member=self) if q.count() > 0: memcache.set(n, True, 86400 * 14) return True else: memcache.set(n, False, 86400 * 14) return False else: if type(something).__name__ == 'Member': n = 'r/m' + str(something.num) + '/m' + str(self.num) r = memcache.get(n) if r: return r else: #q = db.GqlQuery("SELECT * FROM MemberBookmark WHERE one =:1 AND member_num = :2", something, self.num) q = MemberBookmark.selectBy(member=something, member_num=self.num) if q.count() > 0: memcache.set(n, True, 86400 * 14) return True else: memcache.set(n, False, 86400 * 14) return False else: return False
def get(self): output = memcache.get('api_nodes_all') if output is None: site = GetSite() template_values = {} template_values['site'] = site nodes = memcache.get('api_nodes_all') if nodes is None: #nodes = db.GqlQuery("SELECT * FROM Node") nodes = Node.select() memcache.set('api_nodes_all', nodes, 3600) template_values['nodes'] = nodes path = os.path.join(os.path.dirname(__file__), 'tpl', 'api') t = self.get_template(path, 'nodes_all.json') output = t.render(template_values) memcache.set('api_nodes_all', output, 86400) self.write(output)
def get(self, screen_name): site = GetSite() member = CheckAuth(self) if member: if member.twitter_oauth == 1: template_values = {} template_values['site'] = site template_values['rnd'] = random.randrange(1, 100) template_values['member'] = member l10n = GetMessages(self, member, site) template_values['l10n'] = l10n template_values['page_title'] = site.title + u' › Twitter › ' + screen_name template_values['screen_name'] = screen_name access_token = OAuthToken.from_string(member.twitter_oauth_string) twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token) rate_limit = memcache.get(str(member.twitter_id) + '::rate_limit') if rate_limit is None: try: rate_limit = twitter.GetRateLimit() memcache.set(str(member.twitter_id) + '::rate_limit', rate_limit, 60) except: logging.info('Failed to get rate limit for @' + member.twitter_screen_name) template_values['rate_limit'] = rate_limit cache_tag = 'twitter::' + screen_name + '::home' statuses = memcache.get(cache_tag) if statuses is None: statuses = twitter.GetUserTimeline(user=screen_name, count = 50) i = 0; for status in statuses: statuses[i].source = statuses[i].source.replace('<a', '<a class="dark"') statuses[i].datetime = datetime.datetime.fromtimestamp(time.mktime(time.strptime(status.created_at, '%a %b %d %H:%M:%S +0000 %Y'))) statuses[i].text = twitter.ConvertMentions(status.text) #statuses[i].text = twitter.ExpandBitly(status.text) i = i + 1 memcache.set(cache_tag, statuses, 120) template_values['statuses'] = statuses path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop') t=self.get_template(path,'twitter_user.html') self.finish(t.render(template_values)) else: self.redirect('/settings') else: self.redirect('/')
def get(self): member = CheckAuth(self) site = GetSite() if member: if member.twitter_oauth == 1: template_values = {} template_values['site'] = site template_values['rnd'] = random.randrange(1, 100) template_values['member'] = member l10n = GetMessages(self, member, site) template_values['l10n'] = l10n template_values['page_title'] = site.title + u' › Twitter › Direct Messages › Inbox' access_token = OAuthToken.from_string(member.twitter_oauth_string) twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token) rate_limit = memcache.get(str(member.twitter_id) + '::rate_limit') if rate_limit is None: try: rate_limit = twitter.GetRateLimit() memcache.set(str(member.twitter_id) + '::rate_limit', rate_limit, 60) except: logging.info('Failed to get rate limit for @' + member.twitter_screen_name) template_values['rate_limit'] = rate_limit cache_tag = 'member::' + str(member.num) + '::twitter::dm::inbox' messages = memcache.get(cache_tag) if messages is None: messages = twitter.GetDirectMessages() i = 0; for message in messages: messages[i].datetime = datetime.datetime.fromtimestamp(time.mktime(time.strptime(message.created_at, '%a %b %d %H:%M:%S +0000 %Y'))) messages[i].text = twitter.ConvertMentions(message.text) #statuses[i].text = twitter.ExpandBitly(status.text) i = i + 1 memcache.set(cache_tag, messages, 120) template_values['messages'] = messages path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop') t=self.get_template(path,'twitter_dm_inbox.html') self.finish(t.render(template_values)) else: self.redirect('/settings') else: self.redirect('/')
def GetMemberByUsername(name): one = memcache.get('Member::' + str(name).lower()) if one: return one else: #q = db.GqlQuery("SELECT * FROM Member WHERE username_lower = :1", str(name).lower()) q = Member.selectBy(username_lower=str(name).lower()) if q.count() == 1: one = q[0] memcache.set('Member::' + str(name).lower(), one, 86400) return one else: return False
def GetPlaceByIP(ip): cache = 'Place_' + ip place = memcache.get(cache) if place: return place else: #q = db.GqlQuery("SELECT * FROM Place WHERE ip = :1", ip) q = Place.selectBy(ip=ip) if q.count() == 1: place = q[0] memcache.set(cache, place, 86400) return place else: return False
def GetMemberByEmail(email): cache = 'Member::email::' + hashlib.md5(email.lower()).hexdigest() one = memcache.get(cache) if one: return one else: #q = db.GqlQuery("SELECT * FROM Member WHERE email = :1", str(email).lower()) q = Member.selectBy(email=str(email).lower()) if q.count() == 1: one = q[0] memcache.set(cache, one, 86400) return one else: return False
def get(self): site = GetSite() template_values = {} template_values['site'] = site topics = memcache.get('api_topics_latest') if topics is None: #topics = db.GqlQuery("SELECT * FROM Topic ORDER BY created DESC LIMIT 20") topics = Topic.selectBy(orderBy='-created').limit(20) memcache.set('api_topics_latest', topics, 120) template_values['topics'] = topics template_values['topics_count'] = topics.count() path = os.path.join(os.path.dirname(__file__), 'tpl', 'api') t = self.get_template(path, 'topics_latest.json') self.finish(t.render(template_values))
def GetKindByName(kind, name): K = str(kind.capitalize()) one = memcache.get(K + '::' + str(name)) if one: return one else: #q = db.GqlQuery("SELECT * FROM " + K + " WHERE name = :1", str(name)) selectBy = getattr(getObj(K), 'selectBy'); q = selectBy(name=name) if q.count() == 1: one = q[0] memcache.set(K + '::' + str(name), one, 86400) return one else: return False
def GetKindByNum(kind, num): K = str(kind.capitalize()) one = memcache.get(K + '_' + str(num)) if one: return one else: #q = db.GqlQuery("SELECT * FROM " + K + " WHERE num = :1", int(num)) #selectBy = getattr(globals()[K], 'selectBy'); selectBy = getattr(getObj(K), 'selectBy'); q = selectBy(num=num) if q.count() == 1: one = q[0] memcache.set(K + '_' + str(num), one, 86400) return one else: return False
def get(self): if self.member: if self.member.private_token is None: self.member.private_token = hashlib.sha256( str(self.member.num) + ';' + config.site_key).hexdigest() self.member.sync() store.commit() #jon add notifications = memcache.get('nn::' + self.member.username_lower) if notifications is None: #q = db.GqlQuery("SELECT * FROM Notification WHERE for_member_num = :1 ORDER BY num DESC LIMIT 20", self.member.num) q = Notification.selectBy( for_member_num=self.member_num).orderBy('-num').limit(20) notifications = [] i = 0 for n in q: if i == 0: if self.member.notification_position != n.num: self.member.notification_position = n.num self.member.sync() store.commit() #jon add if n.type == 'reply': n.text = u'<a href="/member/' + n.member.username + u'"><strong>' + n.member.username + u'</strong></a> 在 <a href="' + n.link1 + '">' + self.escape( n.label1) + u'</a> 里回复了你' notifications.append(n) if n.type == 'mention_reply': n.text = u'<a href="/member/' + n.member.username + u'"><strong>' + n.member.username + u'</strong></a> 在回复 <a href="' + n.link1 + '">' + self.escape( n.label1) + u'</a> 时提到了你' notifications.append(n) if n.type == 'mention_topic': n.text = u'<a href="/member/' + n.member.username + u'"><strong>' + n.member.username + u'</strong></a> 在创建主题 <a href="' + n.link1 + '">' + self.escape( n.label1) + u'</a> 时提到了你' notifications.append(n) i = i + 1 self.member.notifications = 0 self.member.sync() store.commit() #jon add memcache.set('Member_' + str(self.member.num), self.member, 86400) memcache.set('nn::' + self.member.username_lower, notifications, 360) self.values['notifications'] = notifications self.set_title(u'提醒系统') self.finalize(template_name='notifications', mobile_optimized=True) else: self.redirect('/signin')
def get(self, private_token): n = memcache.get('n_' + private_token) if n is not None: self.values['notification'] = n self.set_header('Content-type', 'application/xml;charset=UTF-8') self.values['member'] = self.member self.finalize(template_name='notifications', template_root='feed', template_type='xml') else: #q = db.GqlQuery("SELECT * FROM Member WHERE private_token = :1", private_token) q = Member.selectBy(private_token=private_token) count = q.count() if count > 0: member = q[0] #q = db.GqlQuery("SELECT * FROM Notification WHERE for_member_num = :1 ORDER BY num DESC LIMIT 20", member.num) q = Notification.selectBy( for_member_num=member.num).orderBy('-num').limit(20) notifications = [] i = 0 for n in q: if n.type == 'reply': n.title = u'' + n.member.username + u' 在 ' + self.escape( n.label1) + u' 里回复了你' n.text = u'<a href="/member/' + n.member.username + u'"><strong>' + n.member.username + u'</strong></a> 在 <a href="' + n.link1 + '">' + self.escape( n.label1) + u'</a> 里回复了你' notifications.append(n) if n.type == 'mention_reply': n.title = u'' + n.member.username + u' 在回复 ' + self.escape( n.label1) + u' 时提到了你' n.text = u'<a href="/member/' + n.member.username + u'"><strong>' + n.member.username + u'</strong></a> 在回复 <a href="' + n.link1 + '">' + self.escape( n.label1) + u'</a> 时提到了你' notifications.append(n) i = i + 1 self.values['notifications'] = notifications memcache.set('n_' + private_token, notifications, 600) self.set_header('Content-type', 'application/xml;charset=UTF-8') self.values['site'] = GetSite() self.values['member'] = member self.finalize(template_name='notifications', template_root='feed', template_type='xml')
def get(self): if self.member: if self.member.private_token is None: self.member.private_token = hashlib.sha256(str(self.member.num) + ';' + config.site_key).hexdigest() self.member.sync() store.commit() #jon add notifications = memcache.get('nn::' + self.member.username_lower) if notifications is None: #q = db.GqlQuery("SELECT * FROM Notification WHERE for_member_num = :1 ORDER BY num DESC LIMIT 20", self.member.num) q = Notification.selectBy(for_member_num=self.member_num).orderBy('-num').limit(20) notifications = [] i = 0 for n in q: if i == 0: if self.member.notification_position != n.num: self.member.notification_position = n.num self.member.sync() store.commit() #jon add if n.type == 'reply': n.text = u'<a href="/member/' + n.member.username + u'"><strong>' + n.member.username + u'</strong></a> 在 <a href="' + n.link1 + '">' + self.escape(n.label1) + u'</a> 里回复了你' notifications.append(n) if n.type == 'mention_reply': n.text = u'<a href="/member/' + n.member.username + u'"><strong>' + n.member.username + u'</strong></a> 在回复 <a href="' + n.link1 + '">' + self.escape(n.label1) + u'</a> 时提到了你' notifications.append(n) if n.type == 'mention_topic': n.text = u'<a href="/member/' + n.member.username + u'"><strong>' + n.member.username + u'</strong></a> 在创建主题 <a href="' + n.link1 + '">' + self.escape(n.label1) + u'</a> 时提到了你' notifications.append(n) i = i + 1 self.member.notifications = 0 self.member.sync() store.commit() #jon add memcache.set('Member_' + str(self.member.num), self.member, 86400) memcache.set('nn::' + self.member.username_lower, notifications, 360) self.values['notifications'] = notifications self.set_title(u'提醒系统') self.finalize(template_name='notifications', mobile_optimized=True) else: self.redirect('/signin')
def get(self): codes = [ 'EUR', 'JPY', 'CNY', 'CHF', 'AUD', 'TWD', 'CAD', 'GBP', 'HKD', 'MYR', 'NZD', 'PHP', 'SGD', 'THB' ] template_values = {} o = memcache.get('currency.json') if o is not None: pass else: for code in codes: url = 'http://www.google.com/ig/calculator?hl=en&q=1USD=?' + code response = urlfetch.fetch(url) m = re.findall('rhs: "([0-9\.]+)', response.content) if len(m) > 0: value = m[0].strip().replace(' ', '') else: value = 0 template_values[code.lower()] = value path = os.path.join(os.path.dirname(__file__), 'tpl', 'api') t = self.get_template(path, 'currency.json') o = t.render(template_values) memcache.set('currency.json', o, 86400) self.write(o)
def get(self, platform, soldier): ua = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13' referer = 'http://bfbcs.com/' + platform cache_tag = 'bfbcs::' + platform + '/' + soldier raw = memcache.get(cache_tag) url = 'http://bfbcs.com/stats_' + platform + '/' + soldier if raw is None: response = urlfetch.fetch(url, headers={ 'User-Agent': ua, 'Referer': referer }) raw = response.content memcache.set(cache_tag, raw, 600) pcode = re.findall('([a-z0-9]{32})', raw) self.response.out.write('<strong>PCODE</strong> ' + str(pcode[0]) + '<br />') if len(pcode) == 1: pcode = pcode[0] payload = 'request=addplayerqueue&pcode=' + pcode self.response.out.write('<strong>PAYLOAD</strong> ' + payload + ' (' + str(len(payload)) + ' bytes)<br />') headers = { 'User-Agent': ua, 'Referer': url, 'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Content-Length': '61', 'Accept': 'application/json, text/javascript, */*', 'Accept-Language': 'en-us,en;q=0.5', 'Accept-Encoding': 'gzip,deflate', 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'Keep-Alive': 115, 'Host': 'bfbcs.com', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache', 'Cookie': '__utma=7878317.1843709575.1297205447.1298572822.1298577848.12; __utmz=7878317.1297205447.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); sessid=enqd028n30d2tr4lv4ned04qi0; __utmb=7878317.21.10.1298577848; __utmc=7878317' } response = urlfetch.fetch(url, payload=payload, headers=headers, method='POST') if response.status_code == 500: response = urlfetch.fetch(url, payload=payload, headers=headers, method='POST') if response.status_code == 500: self.write('<strong>FAILED</strong>') else: self.write('<strong>RESULT</strong> OK ' + response.content) else: self.write('<strong>RESULT</strong> OK ' + response.content)
def get(self, member_username): site = GetSite() browser = detect(self.request) self.session = Session() template_values = {} template_values['site'] = site template_values['system_version'] = SYSTEM_VERSION member = CheckAuth(self) template_values['member'] = member template_values['show_extra_options'] = False if member: if member.num == 1: template_values['show_extra_options'] = True l10n = GetMessages(self, member, site) template_values['l10n'] = l10n one = False one = GetMemberByUsername(member_username) if one is not False: if one.followers_count is None: one.followers_count = 0 template_values['one'] = one template_values['page_title'] = site.title + u' › ' + one.username template_values['canonical'] = 'http://' + site.domain + '/member/' + one.username if one.github: github = memcache.get('Member::' + one.username_lower + '::github') if github is None: response = urlfetch.fetch("https://api.github.com/users/" + one.github + "/repos") if response.status_code == 200: github = response.content memcache.set('Member::' + one.username_lower + '::github', github, 86400) if github is not None: template_values['github_repos'] = sorted(json.loads(github), key=lambda x:x['stargazers_count'], reverse=True) if one is not False: member_blog = memcache.get('member::' + str(one.num) + '::blog') if member_blog == None: #blog = db.GqlQuery("SELECT * FROM Topic WHERE node_name = :1 AND member_num = :2 ORDER BY created DESC LIMIT 1", 'blog', one.num) blog = Topic.selectBy(member_num=one.num, node_name='blog').orderBy('-created').limit(1) if blog.count() > 0: template_values['blog'] = blog[0] memcache.set('member::' + str(one.num) + '::blog', blog[0], 7200) else: template_values['blog'] = member_blog member_topics = memcache.get('member::' + str(one.num) + '::topics') if member_topics != None: template_values['topics'] = member_topics else: #q2 = db.GqlQuery("SELECT * FROM Topic WHERE member_num = :1 ORDER BY created DESC LIMIT 10", one.num) q2 = Topic.selectBy(member_num=one.num).orderBy('-created').limit(10) template_values['topics'] = q2 memcache.set('member::' + str(one.num) + '::topics', q2, 7200) replies = memcache.get('member::' + str(one.num) + '::participated') if replies is None: #q3 = db.GqlQuery("SELECT * FROM Reply WHERE member_num = :1 ORDER BY created DESC LIMIT 10", one.num) q3 = Reply.selectBy(member_num=one.num).orderBy('-created').limit(10) ids = [] replies = [] i = 0 for reply in q3: if reply.topic.num not in ids: i = i + 1 if i > 10: break replies.append(reply) ids.append(reply.topic.num) if len(replies) > 0: memcache.set('member::' + str(one.num) + '::participated', replies, 7200) if len(replies) > 0: template_values['replies'] = replies template_values['show_block'] = False template_values['show_follow'] = False template_values['favorited'] = False if one and member: if one.num != member.num: template_values['show_follow'] = True template_values['show_block'] = True try: blocked = pickle.loads(member.blocked.encode('utf-8')) except: blocked = [] if one.num in blocked: template_values['one_is_blocked'] = True else: template_values['one_is_blocked'] = False if member.hasFavorited(one): template_values['favorited'] = True else: template_values['favorited'] = False if 'message' in self.session: template_values['message'] = self.session['message'] del self.session['message'] if one is not False: if browser['ios']: path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile' ) filename = 'member_home.html' else: path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop') filename = 'member_home.html' else: if browser['ios']: path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile') filename = 'member_not_found.html' else: path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop') filename = 'member_not_found.html' print one.created t=self.get_template(path, filename) self.finish(t.render(template_values))