コード例 #1
0
    def get_list_result(self,
                        p_url,
                        collect_keys=None,
                        page_size=10,
                        max_page_num=-1,
                        max_result_num=-1,
                        interval=0):
        '''
        get_list_result(p_url[, max_page_num[, max_result_num]])
        ->status: Status, result: list
        '''
        page_num = 0
        p_url.set_param('page_size', page_size)

        total_results = dict()
        status = None

        empty_flag = False  # use to check if there is a list not empty
        while page_num < max_page_num or max_page_num < 0:
            log.debug('page_num: %d' % page_num)

            empty_flag = True
            p_url.set_param('page_num', page_num)
            status, result = self.get_single_result(p_url, collect_keys)
            if not status.is_ok():
                return status, total_results
            else:
                # request Ok
                for key in result:
                    result_list = s_get(result, key)
                    if not isinstance(result_list, list):
                        total_results[key] = result_list
                        continue
                    else:
                        s_append(total_results, key, result_list)

                        # if there is a list not empty
                        current_num = len(result_list)
                        if current_num > 0:
                            empty_flag = False

                        total_list = s_get(total_results, key, list())
                        total_num = len(total_list)
                        if current_num == 0:
                            # iteration finish
                            continue
                        elif total_num > max_result_num and max_result_num > 0:
                            # iteration finish
                            total_results[key] = total_list[:max_result_num]
                            empty_flag = True
                            break

                # iterate or not
                if empty_flag:
                    break
                else:
                    sleep(interval)
                    page_num += 1
        return status, total_results
コード例 #2
0
ファイル: object.py プロジェクト: pjdu/baidumap
 def set_property(self, p_key, p_value):
     if p_key in self.__dict__:
         self.__dict__[p_key] = p_value
     else:
         for key in self.__dict__:
             value = s_get(self.__dict__, key)
             if isinstance(value, JsonLike):
                 s_get(self.__dict__, key,
                       JsonLike()).set_property(p_key, p_value)
         return
コード例 #3
0
ファイル: object.py プロジェクト: pjdu/baidumap
 def get_property_in_child(self, p_key, p_default=None):
     # print('search in %s' % self.keys())
     for key in self.keys():
         # print(' search in %s' % (key))
         value = s_get(self.__dict__, key)
         if isinstance(value, JsonLike):
             value = s_get(self.__dict__, key, JsonLike())
             if isinstance(value, JsonLike):
                 value = value.get_property(p_key, p_default)
             if value and value is not None and value != p_default:
                 # print('return')
                 return value
     return p_default
コード例 #4
0
ファイル: object.py プロジェクト: pjdu/baidumap
    def from_json(self, json, **kwargs):
        self.__dict__ = dict()
        if isinstance(json, dict) or isinstance(json, JsonLike):
            json.update(kwargs)
            for key in json:
                value = s_get(json, key, '')
                if isinstance(value, list):
                    value = BaiduMapObject(value)
                elif isinstance(value, dict):
                    value = JsonLike(value)

                # change location to Location
                if isinstance(value, JsonLike):
                    if 'lat' in value.keys() and 'lng' in value.keys():
                        value = Location(value.to_json())

                self.__dict__[key] = value
        elif isinstance(json, list):
            for index, value in enumerate(json):
                if isinstance(value, list):
                    self.__dict__[index] = BaiduMapObject(value)
                elif isinstance(value, dict):
                    self.__dict__[index] = JsonLike(value)
                else:
                    self.__dict__[index] = value
            self.__dict__['list_size'] = len(json)
コード例 #5
0
ファイル: object.py プロジェクト: pjdu/baidumap
    def __str__(self):
        copy_dict = self.__dict__.copy()
        for key in copy_dict:
            value = s_get(copy_dict, key)
            copy_dict[key] = str(value)

        return str(copy_dict)
コード例 #6
0
    def get_single_result(self, p_url, collect_keys=None):
        '''
        get_single_result(p_url)->status: Status, result: dict
        '''
        p_url.set_param('ak', self.ak_key)
        p_url.set_param('output', 'json')
        response = p_url.get()
        log.debug('requests GET: %s' % p_url)
        try:
            json = response.json()
        except ValueError:
            log.error('JSONDecodeError:\nraw:\n%s' % response.raw)
            raise HandleNotExistsError()
        result = dict()

        # default collect all
        if collect_keys is None:
            collect_keys = list(json.keys())
            # keys below are not information
            s_remove(collect_keys, 'status')
            s_remove(collect_keys, 'message')
            s_remove(collect_keys, 'msg')

        for key in collect_keys:
            result[key] = s_get(json, key, dict())

        status = get_status_from_json(json)
        log.debug('response status: %s' % status)
        return status, result
コード例 #7
0
def get_status_from_json(json):
    '''
    '''
    code = s_get(json, 'status', '')
    status = get_status(code)
    if 'msg' in json:
        status.msg = json['msg']
    elif 'message' in json:
        status.msg = json['message']
    return status
コード例 #8
0
ファイル: object.py プロジェクト: pjdu/baidumap
    def get_properties(self, p_keys, p_defaults=None):
        final_results = dict()
        for key in p_keys:
            default = s_get(p_defaults, key)
            results = self.get_property(key, default)
            if isinstance(results, dict):
                s_merge(final_results, results)
            else:
                s_set(final_results, key, results)

        return final_results
コード例 #9
0
ファイル: object.py プロジェクト: pjdu/baidumap
 def get_property(self, p_key, p_default=None):
     if p_key in self.__dict__:
         return {p_key: s_get(self.__dict__, p_key, p_default)}
     else:
         if self.is_list():
             results = dict()
             # print('search in %s' % self.keys())
             for key in self.keys():
                 # print(' search in %s' % key)
                 if key == 'list_size':
                     continue
                 value = s_get(self.__dict__, key, JsonLike())
                 if isinstance(value, JsonLike):
                     # print('debug iterator')
                     value = value.get_property(p_key, p_default)
                 if value and value is not None and value != p_default:
                     results[key] = value
             return results
         else:
             return self.get_property_in_child(p_key, p_default)
コード例 #10
0
def get_status(code):
    '''
    get_status(code)->Status:get status from code
    '''
    code = str(code)
    if code not in status_map:
        code = code[0] + 'xx'
        if code not in status_map:
            code = 'xxx'
    status_list = list(s_get(status_map, code))
    status = Status(*status_list)
    return status
コード例 #11
0
ファイル: object.py プロジェクト: pjdu/baidumap
 def to_json(self):
     if 'lat' in self.__dict__ and 'lng' in self.__dict__:
         lat = s_get(self.__dict__, 'lat', '-1')
         lng = s_get(self.__dict__, 'lng', '-1')
     return {'lat': lat, 'lng': lng}
コード例 #12
0
ファイル: object.py プロジェクト: pjdu/baidumap
 def __str__(self):
     lat = s_get(self.__dict__, 'lat', '-1')
     lng = s_get(self.__dict__, 'lng', '-1')
     return '%s,%s' % (lat, lng)