film_recom_sys/actor.py
2025-01-31 21:36:31 +08:00

79 lines
2.1 KiB
Python

import pymysql
import requests
import re
from itertools import cycle
# 数据库连接配置
db_config = {
'host': 'localhost',
'user': 'root',
'password': 'root',
'database': 'filmsystem',
'port': 3316, # 设置端口为 3316
'charset': 'utf8mb4'
}
# OMDb API 配置
api_keys = cycle(["f236d30", "39c50d95"]) # 轮流使用多个 API key
api_url = "http://www.omdbapi.com/"
# 提取电影名的函数
def extract_title(title_with_year):
# 匹配"电影名 (年份)",并提取电影名部分
match = re.match(r"^(.*?)(?: \(\d{4}\))$", title_with_year)
return match.group(1) if match else title_with_year
# 请求演员信息的函数
def get_actors(title):
api_key = next(api_keys) # 轮流使用 API Key
params = {
't': title,
'apikey': api_key
}
response = requests.get(api_url, params=params)
data = response.json()
if "Actors" in data:
return data["Actors"]
else:
return None
# 主程序
def main():
# 连接数据库
connection = pymysql.connect(**db_config)
try:
with connection.cursor(pymysql.cursors.DictCursor) as cursor: # 使用 DictCursor
# 查询所有电影标题
cursor.execute("SELECT id, title FROM movie WHERE actor IS NULL")
movies = cursor.fetchall()
for movie in movies:
movie_id = movie['id']
full_title = movie['title']
# 提取电影名
title = extract_title(full_title)
# 使用 OMDb API 获取演员信息
actors = get_actors(title)
if actors:
# 更新 actor 列
update_query = "UPDATE movie SET actor = %s WHERE id = %s"
cursor.execute(update_query, (actors, movie_id))
connection.commit()
print(f"Updated actors for '{full_title}': {actors}")
else:
print(f"No actors found for '{full_title}'")
finally:
# 关闭数据库连接
connection.close()
if __name__ == "__main__":
main()