def _do_place(self): b = BaseInput(self.place_path) print("删除所有地点") Place.delete_all() def callback(info): class_name = info.pop('proxy', 'PLACE') info.update({'class': class_name}) place = Place(**info) place.save() print(info.get('slug', '')) b.input(callback)
def place_update(self, **kwargs): smart_print(kwargs, 'place_update') try: slug = kwargs.get('slug', '') if not slug: return {'success': False} Place.cls_update(**kwargs) except Exception: return {'success': False} else: return {'success': True}
def load(self, to_db=False, to_tag=False, delete=False): with open(name=self.origin_path, mode='r') as f: for each_line in f.readlines(): json_data = json.loads(each_line[:-1]) save, format_data = self.format_info(json_data) if not save: continue try: slug = json_data.pop('slug') if delete: place = Place.get_by_slug(slug) if place: print("Will delete %s" %slug) place.remove() name_zh = json_data['name_zh'] tag = Tag.get_by_name(name_zh) if tag: print("Will delete %s" %name_zh) tag.remove() if to_db: exists = Place.get_by_slug(slug=slug, json_format=True) if not exists: print(json_data['name_zh']) place = Place(slug=slug, **json_data) place.save() if to_tag: class_name = json_data.pop('class') name = json_data.get('name_zh', '') item = {'slug': slug, 'class': class_name} categories = json_data.get('categories', '') tag_type = class_name exists = Tag.get_by_name(name, json_format=True) if exists: print("存在 %s" %name) continue print(json_data['name_zh']) tag = Tag(name=name, items=[item], score=float(DEFAULT_SCORE), parents=categories, proxy=tag_type) tag.save() except Exception, err: print(err) continue
def add_tag(self, tag, force_add=False): name = tag.name if not name: return score = getattr(tag, 'score', 1.0) or 1.0 parents = getattr(tag, 'parents', []) or [] items = getattr(tag, 'items', []) or [] instance_items = [] equal_to = getattr(tag, 'equal_to', '') or '' for item in items: slug = item['slug'] or name if (not force_add) and (self.memory_normal_items.exists(slug) or self.memory_place_items.exists(slug)): continue category = item['class'] if category == 'NORMAL': item_info = Normal.get_by_slug(slug, json_format=True) if not item_info: continue item_instance = NormalItemEntity(tag_name=name, parents=parents, **item_info) self.memory_normal_items.add(slug, item_instance) else: item_info = Place.get_by_slug(slug, json_format=True) if not item_info: continue item_instance = PlaceItemEntity(tag_name=name, **item_info) self.memory_place_items.add(slug, item_instance) instance_items.append(item_instance) tag_instance = TagEntity(name, score=score, equal_to=equal_to, parents=parents, items=instance_items) self.memory_tags.add(name, tag_instance)
def to_cache(self): for tag in Tag.objects(): name = tag.name parents = getattr(tag, 'parents', []) items = tag.get_items()['places'] self.tags.update({ name: { 'parents': parents, 'items': items } }) for item in items: if not item['class'] in Tag.mapping: continue if self.has_slug(item['slug']): continue obj = Place.get_by_slug(item['slug'], json_format=True) if not obj: continue slug = obj.get('slug', '') if not slug: continue self.items.update({slug: obj})
def do_place(self): path = os.path.join(BASE, 'output_place.csv') file = open(path, 'w') for p in Place.objects(): slug = getattr(p, 'slug', '') if not slug: continue name_en = getattr(p, 'name_en', '') name_zh = getattr(p, 'name_zh', '') centroid = getattr(p, 'centroid', [0.0, 0.0]) lng, lat = centroid place_parent = getattr(p, 'place_parent', '') proxy = getattr(p, 'class', 'PLACE') info = '%s\t%s\t%s\t%.4f\t%.4f\t%s\t%s\n' % (slug, name_zh, name_en, lat, lng, place_parent, proxy) print(slug) file.write(info.encode('utf-8')) file.close()
def callback(info): class_name = info.pop('proxy', 'PLACE') info.update({'class': class_name}) place = Place(**info) place.save() print(info.get('slug', ''))