En otras palabras vemos que llegue todo en regla. - Se va a apollar en un paquete de excepciones para lanzar los try catch. resources es la capa de presentacion aquí se contemplan todas las vistas tomando en cuenta lo que devuelve mi api rest (para este caso devolvemos JSONs, entonces los json son nuestras vistas de procesar un resultado). cabe aclarar que esta vista no sera humana, sino que sera para otra maquina. ''' # Dependencias trasversales from api.config.config import Config from api.aspects.aspects import Aspects conf = Config() asp = Aspects() # Dependencias from api.controllers.controllers import Controllers from exceptions.exceptions import MBInputError, MBTransitionError from api.dtos.dtos import Dtos from api.entities.entities import Entities ctrl = Controllers() # Errores # err = MBInputError() o MBTransitionError() dtos = Dtos() enties = Entities()
class WeatherAPI: def __init__(self): self.session = requests.Session() self.config = Config() self.trans = Transmitter() def getWeather(self, name): cities_info = self.trans.cityCode(name) if len(cities_info) == 0: # 没有查询到城市 # TODO 抛出错误提示 raise RuntimeError(u'There is no city named %s.' % name) elif len(cities_info) == 1: # 只查询到一个城市 code = cities_info[0][3] else: # 查询到多个城市 # TODO 提示用户选择,这里先简单取第一个 code = cities_info[0][3] data = {u'id': code} response = self.session.post(self.config.getWeatherCNUrl(), data=data) # TODO 校验是否成功请求 return self._parse(response.content) @staticmethod def _parse(html): """ <ul class="earlywarning"> 包含天气预警 <div class="header-info"> 包含当前天气信息 <table class="sevendays"> 默认 7 天的天气 :param html: html data. :return: json data with weather info. """ result = dict() soup = BeautifulSoup(html, u'html.parser') # print(soup.prettify()) name = group(soup.find(u'div', class_=u'logo-info').getText().split()) result['city'] = name.replace('更换城市', '') div_current = soup.find(u'div', class_=u'header-info') # ==========当前日期信息 ptime = div_current.find(u'div', class_=u'curtime').get_text().split()[0] div_curdate = div_current.find(u'div', class_=u'curdate') curdate = div_curdate.get_text().split() result['date'] = curdate[0] result['weekday'] = curdate[1] result['lunar'] = curdate[2] result['ptime'] = ptime # ==========当前天气信息 div_curweather = div_current.find(u'div', class_=u'current-weather') curtemp = group( div_curweather.find(u'span', class_=u'cur-temp').text.split()) tempscope = group( div_curweather.find(u'span', class_=u'temperature').text.split()) desc = group( div_curweather.find(u'span', class_=u'description').text.split()) aqi = div_curweather.find(u'div', class_=u'aqi').text.split() result['curtemp'] = curtemp result['temp'] = tempscope result['desc'] = desc result['aqi'] = aqi[0] result['aqilevel'] = aqi[1] # ==========7日天气预报 table_7days = soup.find(u'table', class_=u'sevendays') data_7days = list() for child in table_7days.children: if not isinstance(child, Tag): continue infos = child.text.split() data_7days.append({ u'date': infos[0], u'temp': infos[1], u'desc': infos[2] }) result['7days'] = data_7days # pprint(result) return result
def __init__(self): self.session = requests.Session() self.config = Config() self.trans = Transmitter()
# -*- coding: utf-8 -*- __author__ = 'lovexiaov' import sqlite3 from api.config.config import Config config = Config() class Transmitter(object): def __init__(self): self.db = sqlite3.connect(config.getCityDBPath()) def listProvince(self, count=-1): """ :param count: 列出的个数, 默认值为 -1,即列出所有省份名称 :return: 省份名称列表 """ sql = u'''SELECT name FROM province LIMIT {}; '''.format(count) cursor = self._query(sql) return map(lambda x: x[0], cursor.fetchall()) def listCity(self, province, count=-1): """ 列出指定省份的城市列表 :param province: 省份 :param count: 列出多少个,默认列出全部 :return: 指定省份的城市列表