Spaces:
Paused
Paused
File size: 2,339 Bytes
5b01a63 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import psycopg2
import json
from faker import Faker
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Set up Faker for generating fake user data
fake = Faker()
# Get database connection
def get_database_connection():
database_url = os.getenv("DATABASE_URL")
if not database_url:
raise ValueError("DATABASE_URL is not set in environment variables.")
return psycopg2.connect(database_url)
# Function to insert a single user into the database
def insert_user(name, profile_name, about, skills, rating, trust_score, ninja_level, task_experience, online_status, profile_pic, is_pro):
skills_json = json.dumps(skills)
conn = get_database_connection()
try:
with conn.cursor() as curs:
curs.execute("""
INSERT INTO users (name, profile_name, about, skills, rating, trust_score, ninja_level, task_experience, online_status, profile_pic, is_pro)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
RETURNING user_id
""", (name, profile_name, about, skills_json, rating, trust_score, ninja_level, task_experience, online_status, profile_pic, is_pro))
user_id = curs.fetchone()[0]
conn.commit()
print(f"User added with ID: {user_id}")
except Exception as e:
conn.rollback()
print(f"Error adding user: {e}")
finally:
conn.close()
# Function to generate and insert 5 unique users
def insert_unique_users():
for _ in range(5):
name = fake.name()
profile_name = fake.user_name()
about = fake.text()
skills = [fake.job() for _ in range(5)]
rating = round(min(fake.random.uniform(0, 5.00), 5.00), 2) # Ensure rating is within 0.00 to 5.00
trust_score = round(min(fake.random.uniform(0, 5.00), 5.00), 2) # Ensure trust_score is within 0.00 to 5.00
ninja_level = fake.random.randint(1, 10)
task_experience = fake.random.randint(0, 1000)
online_status = fake.boolean()
profile_pic = fake.image_url()
is_pro = fake.boolean()
insert_user(name, profile_name, about, skills, rating, trust_score, ninja_level, task_experience, online_status, profile_pic, is_pro)
if __name__ == "__main__":
insert_unique_users()
|