def _get_cities(): L = list(db.select("city", order="alias")) city_list = [] w_dict = dict() y_dict = dict() for c in L: oc = odict(name=c.name, alias=c.alias, yahoo_code=c.yahoo_code, weibo_code=c.weibo_code) city_list.append(oc) w_dict[oc.weibo_code] = oc.yahoo_code y_dict[oc.yahoo_code] = oc.weibo_code return (city_list, w_dict, y_dict)
def _get_user_from_cookie(): uid, hash = _extract_cookie(web.cookies().get("weibouser")) if not uid: logging.info("no cookie found.") return None users = list(db.select("user", where="uid=$uid", vars=dict(uid=uid))) if not users: logging.info("no such user: %s" % uid) return None u = users[0] if hashlib.md5(str(u"%s:%s" % (uid, u.access_token))).hexdigest() != hash: logging.info("user found, but hash not match.") return None return u
def _get_user_from_cookie(): uid, hash = _extract_cookie(web.cookies().get('weibouser')) if not uid: logging.info('no cookie found.') return None users = list(db.select('user', where='uid=$uid', vars=dict(uid=uid))) if not users: logging.info('no such user: %s' % uid) return None u = users[0] if hashlib.md5(str(u'%s:%s' % (uid, u.access_token))).hexdigest()!=hash: logging.info('user found, but hash not match.') return None return u
def _get_weather(city): if not city: return r'{"code":"500","Message":"Missing Input: city"}' data = cache.get("city-%s" % city) if data: logging.info("got data from cache") web.header("X-Cache", "Hit from cache") return data # check city: cities = list(db.select("city", where="yahoo_code=$code", vars=dict(code=city))) if not cities: return r'{"code":"500","Message":"Invalid Input: city"}' c = cities[0] w = c.yahoo_code logging.info("fetch from yahoo weather...") url = "http://weather.yahooapis.com/forecastjson?w=%s&u=c" % w resp = urllib2.urlopen(url) if resp.code == 200: logging.info("begin fetch...") data = json.loads(resp.read()) data["city"] = c.name cond = data["forecast"][0]["condition"] codes = _get_weather_code(cond) data["forecast"][0]["code"] = codes[0] data["forecast"][0]["condition"] = codes[1] data["forecast"][0]["image"] = codes[2] cond = data["forecast"][1]["condition"] codes = _get_weather_code(cond) data["forecast"][1]["code"] = codes[0] data["forecast"][1]["condition"] = codes[1] data["forecast"][1]["image"] = codes[2] # cache it, but first calculate expires time: now = datetime.now() t = datetime(now.year, now.month, now.day) delta = now - t exp = 86400 - delta.seconds # makes it expires in midnight (24:00) if exp > 3600: exp = 3600 logging.info("fetched and cache for %d seconds." % exp) json_data = json.dumps(data) cache.set("city-%s" % city, json_data, exp) web.header("X-Cache", "Miss from cache") return json_data return None
def get_blogs_from_tag(tag): blogs = db.select('select blogs.id,blogs.title,blogs.content,blogs.image,blogs.created_at from \ blogs,blogtag where blogs.id=blogtag.blog_id and blogtag.tag_id="%s"' % tag.id) return blogs
def get_tags_from_blog(blog): tags = db.select('select tags.id,tags.name from tags,blogtag where tags.id=blogtag.tag_id and blogtag.blog_id="%s"' % blog.id) return tags
def get_blogs_from_tag(tag): blogs = db.select( 'select blogs.id,blogs.title,blogs.content,blogs.image,blogs.created_at from \ blogs,blogtag where blogs.id=blogtag.blog_id and blogtag.tag_id="%s"' % tag.id) return blogs
def get_tags_from_blog(blog): tags = db.select( 'select tags.id,tags.name from tags,blogtag where tags.id=blogtag.tag_id and blogtag.blog_id="%s"' % blog.id) return tags
def list_city(): cities = list(db.select("city")) return dict(cities=cities)
def list_city(): cities = list(db.select('city')) return dict(cities=cities)
def POST(self): for i in db.select('account_info'): self.client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, token=OAuthToken(oauth_token=i.oauth_token, oauth_token_secret=i.oauth_token_secret)) if self.process_new_rts(i.rt_since_id): db.update('account_info', where = 'id = %s' % i.id, rt_since_id = self.since_id)