db support
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
venv/
|
||||
__pycache__/
|
||||
.env
|
||||
*.db
|
28
db.py
Normal file
28
db.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from sqlalchemy import create_engine, Table, Column, MetaData, Integer, String, Boolean, DateTime, ForeignKey
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker, relationship, mapped_column
|
||||
import os
|
||||
|
||||
# Set up SQLite database
|
||||
if os.getenv("SQLITE_FILE") is not None:
|
||||
sqlite_file = os.getenv("SQLITE_FILE")
|
||||
else:
|
||||
raise Exception("SQLite File not defined!")
|
||||
DATABASE_URI = f'sqlite:///{sqlite_file}'
|
||||
engine = create_engine(DATABASE_URI)
|
||||
Base = declarative_base()
|
||||
|
||||
class DBGroupInstance(Base):
|
||||
__tablename__ = "GroupInstances"
|
||||
object_num = Column(Integer, primary_key=True, autoincrement=True)
|
||||
timestamp = Column(DateTime, nullable=False)
|
||||
instance_id = Column(String, nullable=False)
|
||||
world_name = Column(String, nullable=False)
|
||||
member_count = Column(Integer, nullable=False)
|
||||
|
||||
# Create tables if they don't exist
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
def get_session():
|
||||
Session = sessionmaker(bind=engine)
|
||||
return Session()
|
26
main.py
26
main.py
@@ -1,14 +1,22 @@
|
||||
from vrcapi import getGroupInstances, fakeGroupInstances
|
||||
from datetime import datetime
|
||||
from db import DBGroupInstance, get_session
|
||||
|
||||
session = get_session()
|
||||
|
||||
if __name__ == "__main__":
|
||||
instance_data = []
|
||||
group_instances = fakeGroupInstances()
|
||||
|
||||
group_instances = getGroupInstances()
|
||||
if group_instances is not []:
|
||||
for instance in group_instances:
|
||||
instance_record = {"timestamp": datetime.now(),
|
||||
"instance_id": {instance.instance_id},
|
||||
"member_count": {instance.member_count}}
|
||||
instance_data.append(instance_record)
|
||||
|
||||
print(instance_data)
|
||||
try:
|
||||
db_instance = DBGroupInstance(timestamp=datetime.now(),
|
||||
instance_id=instance.instance_id,
|
||||
world_name=instance.world.name,
|
||||
member_count=instance.member_count)
|
||||
session.add(db_instance)
|
||||
session.commit()
|
||||
print("Instance log added")
|
||||
except Exception as e:
|
||||
print(f"Exception {e}")
|
||||
else:
|
||||
print("No group instances! Nothing to do")
|
Reference in New Issue
Block a user