def getParamValue(name): try: opts, args = getopt(_getRawParams(), '', ['%s=' % name]) except GetoptError as err: print(err) raise ParamError() if len(opts) == 0: raise ParamError() for opt, value in opts: if opt == '--%s' % name: return value else: raise ParamError()
def next(self): if len(self.aks) < 1: raise ParamError(u'aks不能为空') ak = self.aks[self.cur_index] self.cur_index += 1 if self.cur_index == len(self.aks): self.cur_index = 0 return ak
def get(self): self.parser.add_argument('all', type=str, default='false', required=False, help='') get_all = self.parser.parse_args()['all'] proxy_type = self.parser.parse_args()['type'] if proxy_type == 'http': if get_all == 'true': return self.http_proxy.get_all() else: return self.http_proxy.get_one() elif proxy_type == 'https': if get_all == 'true': return self.https_proxy.get_all() else: return self.https_proxy.get_one() else: raise ParamError(msg='proxy type param error,must be http/https')
def get_detail_location_by_address(self, address, city=None): """ 根据地址信息获取地理坐标 :param address: address must include province and city information. :param city: city just functions as a filter for return data. :return: if success, return { 'location': # 经纬度坐标 { 'lat': , 'lng': }, 'precise': , # 位置的附加信息, 是否精确查找, 1为精确查找, 0为不精确 'confidence': , # 可信度 'level': , # 地址类型 } else return None. """ if address is None: raise ParamError(u'address不能为空') params = { 'address': address, 'ak': self.scheduler.next(), 'output': 'json' } if city: params['city'] = city try: r = requests.get(self.baidu_json_location_url, params=params) r.raise_for_status() data = json.loads(r.text) if data['status'] == 0: return data['result'] else: logger.debug(r.text) except Exception as e: pass logger.debug(u'failed to get address location for %s' % address) return None
def get_detail_address_by_location(self, location, **kwargs): """ 根据地理坐标获取地址信息 :param location: {'lat': , 'lng': } 地理坐标 :param kwargs: :return: if success return { 'location': {'lat':, 'lng'}, 'origin_location': {'lat':, 'lng'}, # 这是请求的location, 并非百度原始返回的 'formatted_address":, # 结构化地址信息 'business':, # 商圈 'addressComponent': # 地址信息 { 'country":, # 国家 'province':, # 省名 'city':, # 城市名 'district':, # 区县名 'street':, # 街道名 'street_number':, # 街道门牌号 'country_code':, # 国家code 'direction':, # 和当前坐标点的方向 'distance': # 和当前坐标点的距离 } 'pois': # 周边poi数组 { 'addr':, # 地址信息 'cp':, # 数据来源 'direction':, # 和当前坐标点方向 'distance':, # 离坐标点距离 'name':, # poi名称 'poiType':, # poi类型, 如'办公大楼, 商务大厦' 'point':, # poi坐标 'tel':, # 电话 'uid':, # poi唯一标识 'zip':, # 邮编 } 'sematic_description':, # 当前位置结合POI的语义化结果描述 } or return None. :raise ParamError: if location is None. """ if location is None: raise ParamError(u'location不能为空') params = { 'ak': self.scheduler.next(), 'output': 'json', 'location': '{lat},{lng}'.format(lat=location['lat'], lng=location['lng']), 'coordtype': kwargs.get('coordtype', 'bd09ll'), 'pois': kwargs.get('pois', 0) } try: r = requests.get(self.baidu_json_region_url, params=params) r.raise_for_status() data = json.loads(r.text) if data['status'] == 0: data['result']['origin_location'] = { 'lat': location['lat'], 'lng': location['lng'] } return data['result'] else: logger.debug(r.text) except Exception as e: pass logger.debug(u'failed to get province for %s' % str(location)) return None