commit 35c7bac5569c47d7e9b8397bedeafa9550bbd66f Author: mstfyldz Date: Sun Feb 22 23:23:57 2026 +0300 main diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c3b279c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3.11-slim + +WORKDIR /app + +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +COPY . . + +CMD ["python", "import.py"] \ No newline at end of file diff --git a/import.py b/import.py new file mode 100644 index 0000000..f7d0fb5 --- /dev/null +++ b/import.py @@ -0,0 +1,92 @@ +import os +import requests +import pandas as pd +import psycopg2 +from io import StringIO +from datetime import datetime + +DATABASE_URL = os.getenv("DATABASE_URL") + +# EPA resmi dataset URL +CSV_URL = "https://www.fueleconomy.gov/feg/epadata/vehicles.csv" + +print("CSV indiriliyor...") +response = requests.get(CSV_URL) +response.raise_for_status() + +print("CSV memory'e alınıyor...") +df = pd.read_csv(StringIO(response.text), low_memory=False) + +print("Filtreleme başlıyor...") + +# 2000 sonrası +df = df[df["year"] >= 2000] + +df = df[[ + "make", + "model", + "year", + "fuelType1", + "trany", + "cylinders", + "comb08" +]] + +df = df.dropna(subset=["make", "model", "year"]) +df = df.drop_duplicates(subset=["make", "model", "year", "fuelType1", "trany"]) + +# MPG -> L/100km +df["avgConsumption"] = df["comb08"].apply( + lambda mpg: 235.214 / mpg if mpg and mpg > 0 else None +) + +df["createdAt"] = datetime.utcnow() + +df = df.rename(columns={ + "make": "brand", + "fuelType1": "fuelType", + "trany": "transmission", + "comb08": "combinationMpg" +}) + +df = df[[ + "brand", + "model", + "year", + "fuelType", + "transmission", + "cylinders", + "combinationMpg", + "avgConsumption", + "createdAt" +]] + +print("DB bağlantısı kuruluyor...") +conn = psycopg2.connect(DATABASE_URL) +cur = conn.cursor() + +print("Eski veriler temizleniyor...") +cur.execute('TRUNCATE TABLE "CarLibrary";') +conn.commit() + +print("COPY başlıyor...") + +buffer = StringIO() +df.to_csv(buffer, index=False, header=False) +buffer.seek(0) + +cur.copy_expert( + """ + COPY "CarLibrary" + (brand, model, year, fuelType, transmission, cylinders, + combinationMpg, avgConsumption, createdAt) + FROM STDIN WITH CSV + """, + buffer +) + +conn.commit() +cur.close() +conn.close() + +print("🚀 IMPORT TAMAMLANDI.") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..93637d0 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +pandas +psycopg2-binary +requests \ No newline at end of file