
import requests
import pandas as pd
import os

API_KEY = 'bb42fbbb'
MOVIES = ["Inception", "Titanic", "Avengers"]  # örnek

RAW_DATA_PATH = '/opt/airflow/data/raw/movies.csv'
os.makedirs(os.path.dirname(RAW_DATA_PATH), exist_ok=True)

def fetch_movie_data():
    data = []
    for movie in MOVIES:
        url = f"http://www.omdbapi.com/?t={movie}&apikey={API_KEY}"
        r = requests.get(url)
        if r.status_code == 200:
            res = r.json()
            data.append({
                'Title': res.get('Title'),
                'IMDB': res.get('imdbRating'),
                'RottenTomatoes': next((r['Value'] for r in res.get('Ratings', []) if r['Source']=='Rotten Tomatoes'), None),
                'Metacritic': res.get('Metascore')
            })
    df = pd.DataFrame(data)
    df.to_csv(RAW_DATA_PATH, index=False)
    print("Data extracted successfully!")