Created
May 4, 2022 18:28
-
-
Save farbod-s/fb111238e5cb917b87741ab84defbea2 to your computer and use it in GitHub Desktop.
Create application and download config file from Firebase console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding=utf8 | |
#---------- | |
# AUTHOR: Farbod Samsamipour <[email protected]> | |
# GITHUB: https://github.com/firebase/firebase-admin-python | |
#---------- | |
import os | |
import importlib.util | |
import plistlib as plist | |
import firebase_admin | |
from firebase_admin import credentials, project_management | |
#---------- | |
# https://firebase.google.com/docs/reference/admin/python/firebase_admin.project_management | |
# metadata.app_id | |
# metadata.project_id | |
# metadata.display_name | |
# metadata.package_name=The package name of the Android app. | |
# metadata.bundle_id=The bundle ID of the iOS app. | |
#---------- | |
class FirebaseAdminHelper: | |
#---------- | |
def __init__(self, certificate=None): | |
if certificate is None: | |
self.certificate = "./firebase-adminsdk-XXX.json" | |
else: | |
self.certificate = certificate | |
self._init_sdk() | |
#---------- | |
def _init_sdk(self): | |
if self._check_dependency() is False: | |
self._install_dependency() | |
cred = credentials.Certificate(self.certificate) | |
firebase_admin.initialize_app(cred) | |
#---------- | |
def _check_dependency(self): | |
spec = importlib.util.find_spec("firebase-admin") | |
if spec is None: | |
return False | |
return True | |
#---------- | |
def _install_dependency(self): | |
os.system("sudo pip3 install firebase-admin") | |
#---------- | |
def _starts_with(self, display_name, prefix): | |
return display_name and display_name.startswith(prefix) | |
#---------- | |
def _save_to_json(self, content, file_name): | |
file = open(file_name, "w+") | |
file.write(content) | |
file.close() | |
#---------- | |
def _save_to_plist(self, content, file_name): | |
with open(file_name, "wb+") as file: | |
plist.dump(content, file) | |
#---------- | |
def get_android_apps(self): | |
return project_management.list_android_apps() | |
#---------- | |
def get_android_app_by_name(self, display_name): | |
android_apps = self.get_android_apps() | |
for android_app in android_apps: | |
if self._starts_with(android_app.get_metadata().display_name, display_name): | |
return android_app | |
return None | |
#---------- | |
def get_android_app_by_id(self, package_name): | |
android_apps = self.get_android_apps() | |
for android_app in android_apps: | |
if self._starts_with(android_app.get_metadata().package_name, package_name): | |
return android_app | |
return None | |
#---------- | |
def create_android_app(self, package_name, display_name): | |
android_app = self.get_android_app_by_id(package_name) | |
if android_app != None: | |
return android_app | |
return project_management.create_android_app(package_name, display_name, app=None) | |
#---------- | |
def download_android_config(self, android_app): | |
config = android_app.get_config() | |
self.save_android_config(config) | |
#---------- | |
def download_android_config_by_name(self, display_name): | |
android_app = self.get_android_app_by_name(display_name) | |
if android_app is None: | |
# print error message | |
return | |
config = android_app.get_config() | |
self.save_android_config(config) | |
#---------- | |
def download_android_config_by_id(self, package_name): | |
android_app = self.get_android_app_by_id(package_name) | |
if android_app is None: | |
# print error message | |
return | |
config = android_app.get_config() | |
self.save_android_config(config) | |
#---------- | |
def save_android_config(self, config): | |
if config is None: | |
# print error message | |
return | |
self._save_to_json(config, "google-services.json") | |
#---------- | |
def get_ios_apps(self): | |
return project_management.list_ios_apps() | |
#---------- | |
def get_ios_app_by_name(self, display_name): | |
ios_apps = self.get_ios_apps() | |
for ios_app in ios_apps: | |
if self._starts_with(ios_app.get_metadata().display_name, display_name): | |
return ios_app | |
return None | |
#---------- | |
def get_ios_app_by_id(self, bundle_id): | |
ios_apps = self.get_ios_apps() | |
for ios_app in ios_apps: | |
if self._starts_with(ios_app.get_metadata().bundle_id, bundle_id): | |
return ios_app | |
return None | |
#---------- | |
def create_ios_app(self, bundle_id, display_name): | |
ios_app = self.get_ios_app_by_id(bundle_id) | |
if ios_app != None: | |
return ios_app | |
return project_management.create_ios_app(bundle_id, display_name, app=None) | |
#---------- | |
def download_ios_config(self, ios_app): | |
config = ios_app.get_config() | |
self.save_ios_config(config) | |
#---------- | |
def download_ios_config_by_name(self, display_name): | |
ios_app = self.get_ios_app_by_name(display_name) | |
if ios_app is None: | |
# print error message | |
return | |
config = ios_app.get_config() | |
self.save_ios_config(config) | |
#---------- | |
def download_ios_config_by_id(self, bundle_id): | |
ios_app = self.get_ios_app_by_id(bundle_id) | |
if ios_app is None: | |
# print error message | |
return | |
config = ios_app.get_config() | |
self.save_ios_config(config) | |
#---------- | |
def save_ios_config(self, config): | |
if config is None: | |
# print error message | |
return | |
self._save_to_plist(config, "GoogleService-Info.plist") | |
#---------- | |
# if __name__ == '__main__': | |
def test__create_app_and_download_config(): | |
firebase = FirebaseAdminHelper() | |
ios_app = firebase.create_ios_app("com.example.myapp", "My App") | |
firebase.download_ios_config(ios_app) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment