def currentDataOfCity(cityName, useJSON=True, useMemcache=True): memcacheKey = cityName if useMemcache: result = memcache.get(memcacheKey, namespace=memcacheNamespace) #@UndefinedVariable if result is not None: if useJSON: return json.dumps(result) else: return result # Check valid cityName cityNameCheckFlag = False chtCityName = "" for item in cityList: if cityName==item[1]: chtCityName = item[0] cityNameCheckFlag = True if not cityNameCheckFlag: return errorMsg(201, "City is not found.") pageURL = dataDict[cityName][0] tablePosition = dataDict[cityName][1] # Start to fetch cwb for city list fetchResult = None try: fetchResult = urlfetch.fetch(pageURL) except DownloadError: return errorMsg(101, "Fetching city list is timeout!") # Check for result if fetchResult is None or fetchResult.status_code!=200: return errorMsg(300, "Fetching current data is failed!") # Make a soup and fetch necessary information soup = BeautifulSoup(fetchResult.content) soup.head.extract() currentInfos = soup.html.find("div", attrs={'class':'Current_info'}).findAll("table", attrs={"class":"datatable"}) soup.extract() # Process information resultDict = { "city": chtCityName, "description": unicode(currentInfos[tablePosition].findAll("tr")[2].td.img["alt"].strip()), "image": unicode("http://www.cwb.gov.tw"+currentInfos[tablePosition].findAll("tr")[2].td.img["src"].strip()), "temperature": unicode(currentInfos[tablePosition].findAll("tr")[3].td.contents[0][:-6].strip()), } # Return memcache.set(memcacheKey, resultDict, 4200, namespace=memcacheNamespace) #@UndefinedVariable if useJSON: return json.dumps(resultDict) else: return resultDict
def get(self): if self.request.get("flush")=="true": memcache.flush_all() #@UndefinedVariable resultDict = {"result": 0} self.response.out.write(json.dumps(resultDict)) else: self.response.out.write(errorMsg(202))
def get(self): if self.request.get("category")=="": self.response.out.write(errorMsg(202, "category is required.")) return templateDict = {"category": self.request.get("category")} htmlPath = os.path.join(os.path.dirname(__file__), "html/example.html") self.response.out.write(template.render(htmlPath, templateDict))
def get(self): if self.request.get("flush") == "true": memcache.flush_all() #@UndefinedVariable resultDict = {"result": 0} self.response.out.write(json.dumps(resultDict)) else: self.response.out.write(errorMsg(202))
def get(self): # Flush all memcache keys if self.request.get("flush")=="true": if memcache.flush_all(): #@UndefinedVariable self.response.out.write("{\"result\": 0, \"message\": \"All memcache has been flushed.\"}") else: self.response.out.write(errorMsg(100, "Memcache opration failed"))
def get(self): if self.request.get("category") == "": self.response.out.write(errorMsg(202, "category is required.")) return templateDict = {"category": self.request.get("category")} htmlPath = os.path.join(os.path.dirname(__file__), "document/example.html") self.response.out.write(template.render(htmlPath, templateDict))
def main(): print("Status: 404") print("Content-Type: text/html") print("<html><body>") print(errorMsg(200)) print("</body></html>")
def get(self): self.response.set_status(400) self.response.out.write(errorMsg(200, "Add doesn't support GET request."))
def forecastDataByCity(cityName, recentOnly=True, useMemcache=True, useJSON=True): # Memcache key memcacheKey = cityName if recentOnly: memcacheKey += "_recent" # Get From memcache if useMemcache: result = memcache.get(memcacheKey, namespace=memcacheNamespace) #@UndefinedVariable if result is not None: if useJSON: return json.dumps(result) else: return result # Find city URL cityURL = urlByCityName(cityName) if cityURL is None: return errorMsg(201, "City is not found.") # Start to fetch cwb for city list fetchResult = None try: fetchResult = urlfetch.fetch(cityURL) except DownloadError: return errorMsg(101, "Fetching city list is timeout!") # Check for result if fetchResult is None or fetchResult.status_code!=200: return errorMsg(300, "Fetching city list is failed!") resultDict = {} # Make a soup and fetch necessary information soup = BeautifulSoup(fetchResult.content) soup.head.extract() contentTables = soup.body.table.table.contents[1].findAll("div", attrs={'class':'box'}) recentTableRows = contentTables[0].table.findAll("tr")[1:] if not recentOnly: weekTableRow = contentTables[1].table.findAll("tr")[1] touristTableRows = contentTables[2] soup.html.extract() # Recent part recentList = [] for item in recentTableRows: rowCells = item.findAll("td") tmpDict = {"temperature": unicode(rowCells[0].contents[0]), "description": unicode(rowCells[1].img["alt"]), "image": unicode("http://www.cwb.gov.tw"+rowCells[1].img["src"]), "feel": unicode(rowCells[2].contents[0]), "rainProbability": unicode(rowCells[3].contents[0])} recentList += [tmpDict] resultDict["recent"] = recentList # Week part if not recentOnly: weekList = [] for item in weekTableRow.findAll("td"): tmpDict = {"description": unicode(item.contents[1]["alt"]), "image": unicode("http://www.cwb.gov.tw"+item.contents[1]["src"]), "temperature": unicode(item.contents[3].strip())} weekList += [tmpDict] resultDict["week"] = weekList # Tourist part if not recentOnly: tourists = touristTableRows.table.findAll("tr")[1:] touristsList = [] for item in tourists: tmpDict = {"name": unicode(item.find("th"))} tmpList = [] for cell in item.findAll("td"): subDict = {"description": unicode(cell.contents[1]["alt"]), "image": unicode("http://www.cwb.gov.tw"+cell.contents[1]["src"]), "temperature": unicode(cell.contents[3].strip())} tmpList += [subDict] tmpDict["forecast"] = tmpList touristsList += [tmpDict] resultDict["tourist"] = touristsList # Memcache memcache.set(memcacheKey, resultDict, 22200, namespace=memcacheNamespace) #@UndefinedVariable if useJSON: return json.dumps(resultDict) else: return resultDict