이세개발
article thumbnail

파이썬으로 google의 DB플랫폼인 Firebase를 제어할 수 있다.
기본적인 데이터 삽입에 대해 알아본다.

설치

pip install firebase_admin

사용법 (Firebase)


firebase에서 프로젝트설정 - 서비스 계정 - 아래에 비공개 키 생성으로 키 파일을 다운로드 받는다

사용법 (코드)

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

# Firebase 인증 정보를 제공하는 서비스 계정 키 파일을 다운로드하고 경로를 설정합니다.
cred = credentials.Certificate('path/to/serviceAccountKey.json')
firebase_admin.initialize_app(cred)

# Firestore 데이터베이스를 가져옵니다.
db = firestore.client()

# 데이터를 추가할 컬렉션과 문서 ID를 설정합니다.
collection_name = 'users'
document_id = 'user1'

# 추가할 데이터를 딕셔너리 형태로 작성합니다.
data = {
    'name': 'John',
    'age': 30,
    'email': 'john@example.com'
}

# 데이터를 컬렉션에 추가합니다.
doc_ref = db.collection(collection_name).document(document_id)
doc_ref.set(data)

print('데이터가 성공적으로 추가되었습니다.')

cred = credentials.Certificate('path/to/serviceAccountKey.json') 부분을 아까 다운로드받은 키의 주소로 바꿔준 후 위의 코드를 실행하면 users 컬렉션에 user1 문서안에
'name': 'John',
'age': 30,
'email': 'john@example.com'
데이터가 들어간다.
참고로 document_id를 지정하지 않으면 랜덤한 id를 만들어서 넣어준다.

## 랜덤 document_id 지정
doc_ref = db.collection(collection_name).document()
doc_ref.set(data)

위의 코드를 실행하고 나면

응용 코드

config 파일을 만들어 변수들을 넣어서 재사용에 용이하도록 하였다.
랜덤 필드 id 생성과, 랜덤 문서 id 생성의 변수와 키파일,삽입할 데이터파일의 경로도 지정하도록 만들었다.

랜덤 문서id 선택 시에는 위에 설명한 document_id값을 빼주어서 자동으로 랜덤하게 넣어주고
랜덤 필드id 선택 시 UUID를 랜덤으로 만들어 넣어주는 역할을 하며
경로 설정에 따른 key값과 data를 읽어서 firebase에 삽입을 해주는 역할을 하는 코드이다.

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import random
import json
import configparser
from time import strftime

data_file_path = ""
key_file_path = ""
random_field_id = True
random_document_id = True
config = configparser.ConfigParser()
json_data = {}

def config_generator():
    global config
    global data_file_path
    global key_file_path
    global random_field_id
    global random_document_id

    # 설정파일 오브젝트 만들기
    config['system'] = {}
    config['system']['title'] = 'Input Firebase'
    config['system']['data'] = 'data.json'
    config['system']['key'] = 'firebase-adminsdk.json'
    config['system']['random_field_id'] = "True"
    config['system']['random_document_id'] = "True"
    config['system']['lastdo'] = strftime('%Y-%m-%d %H:%M:%S')

    # 설정파일 저장
    with open('config.ini', 'w', encoding='utf-8') as configfile:
        config.write(configfile)

def config_end_generator():
    global config
    global data_file_path
    global key_file_path
    global random_field_id
    global random_document_id

    # 설정파일 오브젝트 만들기
    config['system'] = {}
    config['system']['title'] = 'Input Firebase'
    config['system']['data'] = data_file_path
    config['system']['key'] = key_file_path
    config['system']['lastdo'] = strftime('%Y-%m-%d %H:%M:%S')
    config['system']['random_field_id'] = random_field_id
    config['system']['random_document_id'] = random_document_id
    # 설정파일 저장
    with open('config.ini', 'w', encoding='utf-8') as configfile:
        config.write(configfile)

def config_read():
    global config
    global data_file_path
    global key_file_path
    global random_field_id
    global random_document_id

    try:
        config.read('config.ini', encoding='utf-8')
        random_field_id = config['system']['random_field_id']
        random_document_id = config['system']['random_document_id']
        data_file_path = config['system']['data']
        key_file_path = config['system']['key']
    except:
        print("config.ini 파일이 없습니다. 기본값으로 새로 생성하겠습니다.")
        config_generator()
        return False

    if random_field_id != "True":
        if random_field_id != "False":
            print("random_field_id 설정이 잘못되었습니다. True 혹은 False를 지정해 주십시오")
            return False
    if random_document_id != "True":
        if random_document_id != "False":
            print("random_document_id 설정이 잘못되었습니다. True 혹은 False를 지정해 주십시오")
            return False
    if data_file_path[-5:] != ".json":
        print("data_file_path 설정이 잘못되었습니다. 확장자가 .json 인지 확인해 주십시오")
        return False
    if key_file_path[-5:] != ".json":
        print("key_file_path 설정이 잘못되었습니다. 확장자가 .json 인지 확인해 주십시오")
        return False

    return True

def json_generator():
    global data_file_path
    with open(data_file_path, 'r') as f:
        print(f"성공적으로 {data_file_path} 을 가져왔습니다.")
        return json.load(f)


def getUUID():
    temp = ''
    uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
    for k,v in enumerate(uuid):
        ran = int(random.random()*16)
        if v == 'y':
            ran = ran & 0x3 | 0x8
        temp +=  hex(ran)[2:]
    return temp

def makedata():
    global json_data

    cred = ""
    db = ""

    # Firebase 인증 정보를 제공하는 서비스 계정 키 파일을 다운로드하고 경로를 설정합니다.
    try:
        cred = credentials.Certificate('firebase-adminsdk.json')
        firebase_admin.initialize_app(cred)
        db = firestore.client()
        print("api 인증 성공")
    except:
        print("key파일이 존재하지 않거나 api에 오류가 있습니다.")
        return False

    # 데이터를 추가할 컬렉션과 문서 ID를 설정합니다.
    def input_data(data,collection_name):
        # 추가할 데이터를 딕셔너리 형태로 작성합니다.
        document_id = ""
        if data.get("document_id"):
            document_id = data["document_id"]
        data["document_id"] = ""
        del data["document_id"]

        if random_field_id == "True":
            data["id"] = getUUID()

        # 데이터를 컬렉션에 추가합니다.
        if random_document_id == "True":
            doc_ref = db.collection(collection_name).document()
        else:
            doc_ref = db.collection(collection_name).document(document_id)
        doc_ref.set(data)


    json_data = json_generator()

    for c in json_data:
        print(f"input collection_name : {c}")
        for i in json_data[c]:
            input_data(i,c)
    print('데이터가 성공적으로 추가되었습니다.')

def main():
    if config_read() == False:
        return
    makedata()
    config_end_generator()

if __name__ == "__main__":
    main()

'Dev > Web' 카테고리의 다른 글

Firebase 파이썬으로 DB 데이터 가져오기  (0) 2023.09.06
Django db 연결  (0) 2023.04.18
Django DRF dj_rest_auth JWT 오류  (0) 2023.04.18
profile

이세개발

@print(name)

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!