db support
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
venv/
|
venv/
|
||||||
__pycache__/
|
__pycache__/
|
||||||
.env
|
.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()
|
28
main.py
28
main.py
@@ -1,14 +1,22 @@
|
|||||||
from vrcapi import getGroupInstances, fakeGroupInstances
|
from vrcapi import getGroupInstances, fakeGroupInstances
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from db import DBGroupInstance, get_session
|
||||||
|
|
||||||
|
session = get_session()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
instance_data = []
|
group_instances = getGroupInstances()
|
||||||
group_instances = fakeGroupInstances()
|
if group_instances is not []:
|
||||||
|
for instance in group_instances:
|
||||||
for instance in group_instances:
|
try:
|
||||||
instance_record = {"timestamp": datetime.now(),
|
db_instance = DBGroupInstance(timestamp=datetime.now(),
|
||||||
"instance_id": {instance.instance_id},
|
instance_id=instance.instance_id,
|
||||||
"member_count": {instance.member_count}}
|
world_name=instance.world.name,
|
||||||
instance_data.append(instance_record)
|
member_count=instance.member_count)
|
||||||
|
session.add(db_instance)
|
||||||
print(instance_data)
|
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