Spaces:
Sleeping
Sleeping
from peewee import * | |
from datetime import datetime | |
from playhouse.sqlite_ext import * | |
# Initialize database | |
json_structure = "JSON column with hash with keys 'title': str, official title of the project, 'deposit': int, initial deposit (in percentage), 'min_price': int, lowest available property price with VAT. It should not be booked or sold., 'city': str, city of the project, 'lat': float, gps coordinates of the project, 'lng': float, gps coordinates of the project, 'start_year': int, year of construction start, 'end_year': int, estimated year of construction end. Some values can be null/unknown" | |
db = SqliteExtDatabase('estate.db', pragmas=( | |
('cache_size', -1024 * 64), # 64MB page-cache. | |
('journal_mode', 'wal'), # Use WAL-mode (you should always use this!). | |
('foreign_keys', 1))) # Enforce foreign-key constraints. | |
# Define the Project model | |
db_description = f"""Table "project" - list of real estate projects (novostavby) in Czech Republic | |
url: url of the project. | |
structure: {json_structure}. | |
content: contents of the website. | |
created_at: date and time of creation. | |
""" | |
class Project(Model): | |
url = CharField(unique=True) | |
structure = JSONField(null=True) | |
content = TextField(null=True) | |
created_at = DateTimeField(default=datetime.now) | |
class Meta: | |
database = db | |
def init_database(): | |
"""Initialize the database and create tables""" | |
print("Initializing database...") | |
db.connect() | |
db.drop_tables([Project]) | |
db.create_tables([Project]) | |
print("Created tables successfully!") | |
# Add some test data if needed | |
test_urls = [ | |
'https://brnojedna.cz/' | |
] | |
for url in test_urls: | |
Project.get_or_create(url=url) | |
print(f"Number of projects in database: {Project.select().count()}") | |
db.close() | |
if __name__ == "__main__": | |
init_database() |