Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Py Script to populate FireBase Database #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ This repo allows you to populate your database with quiz questions for [Flutter

Requires Node.js >= 8.

1. Clone this repo, cd into it, and run `npm install`.
1. Clone this repo, cd into it.
2. Download your service account (generate private key) from the [Firebase Console](https://console.firebase.google.com) under settings, then save it in the root of this project as `credentials.json`.
3. Run `node quizzes.js` and `node topics.js`
2. Run `python3 quizzes.py` and `python3 topics.py`
48 changes: 48 additions & 0 deletions quizzes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import firebase_admin
from firebase_admin import credentials, firestore
import yaml
import asyncio

# Initialize Firebase Admin SDK
cred = credentials.Certificate('./credentials.json')
firebase_admin.initialize_app(cred)
db = firestore.client()

quizzes = [
'angular-basics',
'flutter-scroll',
'cf-basics',
'cf-triggers',
'firebase-perf',
'firestore-basics',
'flutter-basics',
'flutter-containers',
'flutter-flex',
'flutter-gestures',
'flutter-material',
'flutter-scroll',
'js-basics',
'js-variables',
'rxjs-basics',
'ts-basics'
]

async def update(quiz_id):
with open(f'quizzes/{quiz_id}.yaml', 'r') as file:
yaml_data = yaml.safe_load(file)

print(yaml_data)

ref = db.collection('quizzes').document(quiz_id)

await ref.set(yaml_data, merge=True)

print('DONE')

# Loop through quizzes and update Firestore
async def main():
tasks = [update(quiz) for quiz in quizzes]
await asyncio.gather(*tasks)

if __name__ == "__main__":
asyncio.run(main())
41 changes: 41 additions & 0 deletions topics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import firebase_admin
from firebase_admin import credentials, firestore
import yaml
import asyncio

# Initialize Firebase Admin SDK
cred = credentials.Certificate('./credentials.json')
firebase_admin.initialize_app(cred)
db = firestore.client()

topics = [
'angular',
'flutter',
'cf',
'firebase',
'firestore',
'flutter',
'rxjs',
'js',
'ts'
]

async def update(topic_id):
with open(f'topics/{topic_id}.yaml', 'r') as file:
yaml_data = yaml.safe_load(file)

print(yaml_data)

ref = db.collection('topics').document(topic_id)

await ref.set(yaml_data, merge=True)

print('DONE')

# Loop through topics and update Firestore
async def main():
tasks = [update(topic) for topic in topics]
await asyncio.gather(*tasks)

if __name__ == "__main__":
asyncio.run(main())