cloudroam
昨天 90e496298b21f0594b85535ec47d26d2d7d5a9ed
test2.py
@@ -17,6 +17,7 @@
def generate_xiaohongshu_article(film_name, locations):
    """生成小红书风格的文章"""
    try:
    prompt = f"""请为电影《{film_name}》的拍摄地写一篇小红书风格的文章。要求:
1. 标题要吸引人,包含电影名称和拍摄地
2. 开头要吸引人,可以用电影中的经典台词或场景引入
@@ -37,9 +38,13 @@
    )
    
    return completion.choices[0].message.content
    except Exception as e:
        print(f"生成小红书文章失败: {str(e)}")
        return ""
def generate_travel_route(film_name, locations):
    """生成游玩路线"""
    try:
    prompt = f"""请为电影《{film_name}》的拍摄地设计一条最优游玩路线。要求:
1. 考虑各个景点之间的距离和交通方式
2. 合理安排游览顺序,避免来回奔波
@@ -84,9 +89,13 @@
    except Exception as e:
        print(f"解析路线失败: {str(e)}\n原始响应: {response_text}")
        return None
    except Exception as e:
        print(f"生成游玩路线失败: {str(e)}")
        return None
def generate_xiaohongshu_route(film_name, locations):
    """生成小红书风格的路线攻略"""
    try:
    prompt = f"""请为电影《{film_name}》的拍摄地写一篇小红书风格的路线攻略。要求:
1. 标题要吸引人,突出"最佳路线"或"完美行程"等关键词
2. 开头要说明这条路线是如何规划的,为什么这样安排
@@ -108,17 +117,27 @@
    )
    
    return completion.choices[0].message.content
    except Exception as e:
        print(f"生成小红书路线攻略失败: {str(e)}")
        return ""
def get_film_works():
    try:
    url = "http://192.168.1.213:8090/flower/api/filmWorks/pending/create"
    response = requests.get(url)
    if response.status_code == 200:
        return response.json().get("data", [])
        else:
            print(f"获取电影作品失败,状态码: {response.status_code}")
            return []
    except Exception as e:
        print(f"获取电影作品失败: {str(e)}")
    return []
def get_location_info_from_model(film_name):
    """使用模型获取多个拍摄地点信息"""
    try:
    prompt = f"""请为电影《{film_name}》生成所有主要拍摄地点的详细信息。每部电影通常有多个拍摄地点,请尽可能详细地列出所有重要的拍摄地点。
对于每个拍摄地点,请提供以下信息:
@@ -195,9 +214,13 @@
        except Exception as e:
            print(f"解析失败: {str(e)}\n原始响应: {response_text}")
            return None
    except Exception as e:
        print(f"获取拍摄地点信息失败: {str(e)}")
        return None
def create_film_location(film_id, film_name, location_info, article, route_article):
    try:
    url = "http://192.168.1.213:8090/flower/api/filmLocation/new"
    # 默认值设置
@@ -239,9 +262,13 @@
    response = requests.post(url, json=default_data)
    return response.json()
    except Exception as e:
        print(f"创建拍摄地点失败: {str(e)}")
        return {"error": str(e)}
def save_article_and_route(film_id, film_name, article, route, route_article):
    """保存文章和路线到文件"""
    try:
    # 创建输出目录
    output_dir = "output"
    if not os.path.exists(output_dir):
@@ -250,24 +277,34 @@
    # 保存文章
    article_file = os.path.join(output_dir, f"{film_name}_article.md")
    with open(article_file, "w", encoding="utf-8") as f:
        f.write(article)
            f.write(article if article else "文章生成失败")
    
    # 保存路线(JSON格式)
    route_file = os.path.join(output_dir, f"{film_name}_route.json")
    with open(route_file, "w", encoding="utf-8") as f:
            if route:
        json.dump(route, f, ensure_ascii=False, indent=2)
            else:
                json.dump({"error": "路线生成失败"}, f, ensure_ascii=False, indent=2)
    
    # 保存路线(小红书风格)
    route_article_file = os.path.join(output_dir, f"{film_name}_route_article.md")
    with open(route_article_file, "w", encoding="utf-8") as f:
        f.write(route_article)
            f.write(route_article if route_article else "路线文章生成失败")
    except Exception as e:
        print(f"保存文件失败: {str(e)}")
def main():
    # 获取所有电影作品
    try:
    film_works = get_film_works()
    except Exception as e:
        print(f"获取电影作品失败: {str(e)}")
        return
    # 为每个电影作品创建拍摄地点
    for film in film_works:
        try:
        film_name = film.get("nameCn")
        film_id = film.get("id")
        if film_name:
@@ -276,27 +313,36 @@
            # 获取所有拍摄地点信息
            location_info_list = get_location_info_from_model(film_name)
            if location_info_list:
                # 生成小红书文章
                article = generate_xiaohongshu_article(film_name, location_info_list)
                # 清理文章中的HTML标记
                if article:
                article = re.sub(r'```html|```', '', article)
                print(f"\n生成的文章:\n{article}")
                else:
                    print("文章生成失败,使用空内容")
                
                # 生成游玩路线(JSON格式)
                route = generate_travel_route(film_name, location_info_list)
                if route:
                print(f"\n生成的路线:\n{json.dumps(route, ensure_ascii=False, indent=2)}")
                else:
                    print("路线生成失败,使用空内容")
                
                # 生成小红书风格路线
                route_article = generate_xiaohongshu_route(film_name, location_info_list)
                # 清理路线文章中的HTML标记
                if route_article:
                route_article = re.sub(r'```html|```', '', route_article)
                print(f"\n生成的路线文章:\n{route_article}")
                else:
                    print("路线文章生成失败,使用空内容")
                
                # 合并文章和路线
                combined_content = f"{article}\n\n{route_article}"
                
                # 保存到新接口
                try:
                save_url = "http://192.168.1.213:8090/flower/api/filmWorks/edit"
                save_data = {
                    "id": film_id,
@@ -305,16 +351,27 @@
                }
                save_response = requests.post(save_url, json=save_data)
                print(f"保存到新接口结果: {save_response.json()}")
                except Exception as e:
                    print(f"保存到新接口失败: {str(e)}")
                
                # 为每个拍摄地点创建记录
                if location_info_list:
                for location_info in location_info_list:
                    result = create_film_location(film_id, film_name, location_info, article, route_article)
                    print(f"创建拍摄地点 {location_info.get('locationName', '未知地点')} 结果: {result}")
                else:
                    print("没有拍摄地点信息,跳过创建拍摄地点记录")
                
                # 保存文章和路线到文件
                try:
                save_article_and_route(film_id, film_name, article, route, route_article)
                except Exception as e:
                    print(f"保存文件失败: {str(e)}")
            else:
                print(f"未能获取到电影 {film_name} 的拍摄地点信息")
                print(f"电影名称为空,跳过处理")
        except Exception as e:
            print(f"处理电影 {film.get('nameCn', '未知电影')} 时发生错误: {str(e)}")
            continue
if __name__ == "__main__":