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 callback(info): class_name = info.pop('proxy', 'PLACE') info.update({'class': class_name}) place = Place(**info) place.save() print(info.get('slug', ''))