import requests from loguru import logger import csv import os import random class Api: def __init__(self) -> None: # self.base_url = 'http://127.0.0.1:6060' self.base_url = 'http://192.168.11.67:6060' # 创建店铺 def create_shop(self, city: str, street: str, shop_name: str) -> dict: url = f'{self.base_url}/country/shop' item = { 'city': city, 'street': street, 'shop_name': shop_name, } response = requests.post(url, json=item).json() logger.info(response) return response # 查询店铺 def get_shop(self, city: str) -> dict: url = f'{self.base_url}/country/shop' response = requests.get(url).json() # logger.info(response) return response # 创建信息 def create_info(self, child_full_name: str, parent_full_name: str, child_birthday: str, address_str: str, city_name: str, parent_phone: str, postcode: str, province: str, email: str, text: str, status: bool = False, email_content: str | None = None) -> dict: """ 创建信息记录(孩子与家长字段) 参数: child_full_name (str): 孩子全名 parent_full_name (str): 家长全名 child_birthday (str): 孩子生日(字符串) address_str (str): 街道地址 city_name (str): 城市 parent_phone (str): 家长电话 postcode (str): 邮编 province (str): 省/州全称 email (str): 邮箱 text (str): 文本内容(如反馈地址) status (bool): 状态 email_content (str | None): 邮件内容 返回值: dict: 接口返回的数据 """ url = f'{self.base_url}/country/info' item = { "child_full_name": child_full_name, "parent_full_name": parent_full_name, "child_birthday": child_birthday, "address_str": address_str, "city_name": city_name, "parent_phone": parent_phone, "postcode": postcode, "province": province, "status": status, "email": email, "email_content": email_content, "text": text } response = requests.post(url, json=item).json() logger.info(response) return response # 根据城市 随机获取一个店铺 def get_random_shop(self) -> dict: url = f'{self.base_url}/country/shop/random' response = requests.get(url).json() # logger.info(response) if not response.get('street'): logger.error(f'没有店铺') return None return response def main(): """ 从同目录的 `bakeries.csv` 读取面包店数据,按列映射输出或创建店铺 列顺序:`Name,Address,City` """ api = Api() csv_path = os.path.join(os.path.dirname(__file__), 'data.csv') if not os.path.exists(csv_path): logger.error(f'CSV 文件不存在: {csv_path}') return with open(csv_path, 'r', encoding='utf-8') as file: reader = csv.reader(file) header = next(reader, None) for row in reader: if len(row) < 3: logger.warning(f'行列数不足,跳过: {row}') continue shop_name, street, city = row[1], row[2], row[0] if ' (city)' in city: city = city.replace(' (city)', '') if 'Quebec' in city: continue if ',' in city: city = city.split(',')[0] logger.info(f'city: {city}, street: {street}, shop_name: {shop_name}') api.create_shop(city, street, shop_name) # def main2(): # api = Api() # city = 'Toronto' # shop = api.get_random_shop() # if shop: # logger.info(shop) # if __name__ == '__main__': # main() api = Api()