def weather(cityname, timeout=1): """ :param cityname: 城市名字 :return: 返回实况天气 """ key = WEATHER_AppKey api = 'http://v.juhe.cn/weather/index' params = 'cityname=%s&key=%s' % (cityname, key) url = api + '?' + params print(url) # 如果请求在1秒内没有返回,那么聚合api可能就发生故障了 response = requests.get(url=url, proxies=proxy.proxy(), timeout=1) data = json.loads(response.text) print(data) result = data.get('result') sk = result.get('sk') # CommonWeatherResult用来统一两个接口返回的结果 response = CommonWeatherResult() # 温度 response.temperature = sk.get('temp') # 风向 response.wind_direction = sk.get('wind_direction') # 风强度 response.wind_strength = sk.get('wind_strength') # 湿度 response.humidity = sk.get('humidity') response.time = sk.get('time') return response
def stock(market, code): """ 沪深股票 :param market: 上交所 = sh, 深交所 = sz :param code: 股票编号 :return: """ key = STOCK_AppKey api = 'http://web.juhe.cn:8080/finance/stock/hs' params = 'gid=%s&key=%s' % (market + code, key) url = api + '?' + params print(url) response = requests.get(url=url, proxies=proxy.proxy()) data = json.loads(response.text) data = data.get('result')[0].get('data') response = { 'name': data.get('name'), 'now_price': data.get('nowPri'), 'today_min': data.get('todayMin'), 'today_max': data.get('todayMax'), 'start_price': data.get('todayStartPri'), 'date': data.get('date'), 'time': data.get('time') } response['is_rising'] = data.get('nowPri') > data.get('todayStartPri') sub = abs(float(data.get('nowPri')) - float(data.get('todayStartPri'))) # 差值 response['sub'] = float('%.3f' % sub) return response
def weather(cityname): ''' :param cityname: 城市名字 :return: 返回实况天气 ''' key = '9a3e1fa6cb79d69f1594af5cb219a469' api = 'http://v.juhe.cn/weather/index' params = 'cityname=%s&key=%s' % (cityname, key) url = api + '?' + params print(url) response = requests.get(url=url, proxies=proxy.proxy()) data = json.loads(response.text) print(data) result = data.get('result') sk = result.get('sk') response = CommonWeatherResult() # 温度 response.temperature = sk.get('temp') # 风向 response.wind_direction = sk.get('wind_direction') # 风强度 response.wind_strength = sk.get('wind_strength') # 湿度 response.humidity = sk.get('humidity') response.time = sk.get('time') return response
def weather(cityname): ''' :param cityname: 城市名字 :return: 返回实况天气 ''' key = '47438d83fccfe7d30f50b3b72ee13647' api = 'http://apis.juhe.cn/simpleWeather/query' cityname = cityname.replace("市", "") params = 'city=%s&key=%s' % (cityname, key) url = api + '?' + params print(url) response = requests.get(url=url, proxies=proxy.proxy()) data = json.loads(response.text) print(data) now = int(time.time()) timeArray = time.localtime(now) otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray) result = data.get('result') sk = result.get('realtime') response = {} response['temperature'] = sk.get('temperature') response['wind_direction'] = sk.get('direct') response['wind_strength'] = sk.get('power') response['humidity'] = sk.get('humidity') # 湿度 response['time'] = otherStyleTime response['future'] = otherStyleTime print(response) return response
def weather(cityname, timeout=1): ''' :param cityname: city name :param timeout: second :return: return weather ''' key = '13d36defa81488f1a438adbeb94622f1' api = 'http://v.juhe.cn/weather/index' # key = '70c9a4425bc67bbe16ba982b8b76dc78' # api = 'http://apis.juhe.cn/simpleWeather/query' params = 'cityname=%s&key=%s' % (cityname, key) url = api + '?' + params print(url) response = requests.get(url=url, proxies=proxy.proxy(), timeout=timeout) data = json.loads(response.text) print(data) result = data.get('result') print('from weather.py', result) sk = result.get('sk') response = {} response = CommonWeatherResult() response.temperature = sk.get('temp') response.wind_direction = sk.get('wind_direction') response.wind_strength = sk.get('wind_strength') response.humidity = sk.get('humidity') response.time = sk.get('time') response = response.to_dict() print('from weather.py, response, ', response) return response
def code2session(appid, code): API = 'https://api.weixin.qq.com/sns/jscode2session' params = 'appid=%s&secret=%s&js_code=%s&grant_type=authorization_code' %(appid, backend.settings.WX_APP_SECRET, code) url = API + '?' + params response_data = requests.get(url, proxies=proxy.proxy()) data = json.loads(response_data.text) print(data) return data
def constellation(cons_name): key = 'fed29157ba4d3fb9aec2f0625d07838d' api = 'http://web.juhe.cn:8080/constellation/getAll' type = 'today' params = '?consName=%s&type=%s&key=%s' % (cons_name, type, key) url = api + params print(url) response = requests.get(url, proxies=proxy.proxy()) data = json.loads(response.text) return {"name": cons_name, "text": data['summary']}
def code2session(appid, code): API = 'https://api.weixin.qq.com/sns/jscode2session' params = 'appid=%s&secret=%s&js_code=%s&grant_type=authorization_code' % \ (appid, myWX_l_ningmo.settings.WX_APP_SECRET, code) url = API + '?' + params response = requests.get(url=url, proxies=proxy.proxy()) data = json.loads(response.text) logger.info('result from weixinServer : ',data) return data
def history_today(): key = '6c6b318d983b6b4ac8cc5cda0da92155' api = 'http://api.juheapi.com/japi/toh' month = time.localtime().tm_mon day = time.localtime().tm_mday params = 'v=1.0&month=%d&day=%d&key=%s' % (month, day, key) url = api + '?' + params response = requests.get(url=url, proxies=proxy.proxy()) data = json.loads(response.text) result_list = data.get('result') result = [] for item in result_list: result.append({'title': item.get('title'), 'content': item.get('des')}) return result
def constellation(cons_name): ''' :param cons_name: 星座名字 :return: json 今天运势 ''' key = '638590d043a54639f3560b5381f5c4f0' api = 'http://web.juhe.cn:8080/constellation/getAll' types = ('today', 'tomorrow', 'week', 'month', 'year') params = 'consName=%s&type=%s&key=%s' % (cons_name, types[0], key) url = api + '?' + params print(url) response = requests.get(url=url, proxies=proxy.proxy()) data = json.loads(response.text) return {'name': cons_name, 'text': data['summary']}
def constellation(cons_name): """ :param cons_name: 星座名字 :return: json 今天运势 """ key = CONSTELLATION_AppKey api = 'http://web.juhe.cn:8080/constellation/getAll' types = ('today', 'tomorrow', 'week', 'month', 'year') params = 'consName=%s&type=%s&key=%s' % (cons_name, types[0], key) url = api + '?' + params print(url) response = requests.get(url=url, proxies=proxy.proxy()) data = json.loads(response.text) return {'name': cons_name, 'text': data['summary']}
def weather(cityname): ''' :param cityname: 城市名字 :return: 返回实况天气 ''' key = '629792b24dd8148180b3e2e93f1cce9b' api = 'http://v.juhe.cn/weather/index' params = 'cityname=%s&key=%s' % (cityname, key) url = api + '?' + params print(url) response = requests.get(url=url, proxies=proxy.proxy()) data = json.loads(response.text) print(data) result = data.get('result') sk = result.get('sk') response = {} response['temperature'] = sk.get('temp') response['wind_direction'] = sk.get('wind_direction') response['wind_strength'] = sk.get('wind_strength') response['humidity'] = sk.get('humidity') # 湿度 response['time'] = sk.get('time') return response
def simpleWeather(cityname): key = '4f01e5d94ec76504320ebc495f85a211' api = 'http://apis.juhe.cn/simpleWeather/query' params = 'city=%s&key=%s' % (cityname, key) url = api + '?' + params print(url) r = requests.get(url=url, proxies=proxy.proxy()) data = json.loads(r.text) result = data.get('result') city = result.get('city') realtime = result.get('realtime') response = {} response['city'] = city response['info'] = realtime.get('info') # 天气情况 response['temperature'] = realtime.get('temperature') # 温度 response['humidity'] = realtime.get('humidity') # 湿度 response['direct'] = realtime.get('direct') # 风向 response['power'] = realtime.get('power') # 风力 response['aqi'] = realtime.get('aqi') # 空气质量指数 return response
def stock(market, code): key = 'f887b09847c9bcde9801ca7aaa86513e' api = 'http://web.juhe.cn:8080/finance/stock/hs' params = 'gid=%s&key=%s' % (market + code, key) url = api + '?' + params print(url) response = requests.get(url=url, proxies=proxy.proxy()) data = json.loads(response.text) data = data.get('result')[0].get('data') response = { 'name': data.get('name'), 'now_price': data.get('nowPri'), 'today_min': data.get('todayMin'), 'today_max': data.get('todayMax'), 'start_price': data.get('todayStartPri'), 'date': data.get('date'), 'time': data.get('time') } response['is_rising'] = data.get('nowPri') > data.get('todayStartPri') sub = abs(float(data.get('nowPri')) - float(data.get('todayStartPri'))) # 差值 response['sub'] = float('%.3f' % sub) return response
def get_weather(cls, cityname, timeout=1): """ Data Returned { "HeWeather6": [ { "basic": { "cid": "CN101280601", "location": "深圳", "parent_city": "深圳", "admin_area": "广东", "cnty": "中国", "lat": "22.54700089", "lon": "114.08594513", "tz": "+8.00" }, "update": { "loc": "2018-09-21 10:45", "utc": "2018-09-21 02:45" }, "status": "ok", "now": { "cloud": "0", "cond_code": "101", "cond_txt": "多云", "fl": "33", "hum": "65", "pcpn": "0.0", "pres": "1013", "tmp": "30", "vis": "10", "wind_deg": "255", "wind_dir": "西南风", "wind_sc": "1", "wind_spd": "3" } } ] } :param city_info: :return: """ weather_result = CommonWeatherResult() location = cityname params = list() params.append('key=%s' % HeWeather.key) params.append('location=%s' % location) params = '&'.join(params) url = HeWeather.weather_api + '?' + params p = proxy.proxy() response = requests.get(url, timeout=timeout) text = response.text if not text: print('request error!') result = json.loads(text) now = result.get('HeWeather6')[0].get('now') weather_result.temperature = now.get('tmp') weather_result.wind_direction = now.get('wind_dir') weather_result.humidity = now.get('hum') + '%' weather_result.wind_strength = now.get('wind_sc') + '级' return weather_result