How to detect changes in firebase child with python?

前端 未结 3 848
醉梦人生
醉梦人生 2021-01-03 15:31

I have some troubles with this application. What I need is that If I detect a change in the database (FIREBASE) particularly in \'sala\' and \'ventilacion\' nodes the functi

3条回答
  •  感动是毒
    2021-01-03 15:50

    I know this post is 2 years old but hope this helps. Try using firebase_admin module.

    Use this command - pip install firebase-admin

    I too had a requirement where I needed to check for changes made to the Firebase database. I referred here

    Following is a sample code based on your question which you can refer from and try it out.

    import firebase_admin
    from firebase_admin import credentials
    from firebase_admin import db
    
    
    cred = credentials.Certificate("path/to/serviceAccountKey.json")
    firebase_admin.initialize_app(cred, {
        'databaseURL': 'https://example.firebaseio.com',
        'databaseAuthVariableOverride': None
    })
    
    
    def ignore_first_call(fn):
        called = False
    
        def wrapper(*args, **kwargs):
            nonlocal called
            if called:
                return fn(*args, **kwargs)
            else:
                called = True
                return None
    
        return wrapper
    
    
    @ignore_first_call
    def listener(event):
        print(event.event_type)  # can be 'put' or 'patch'
        print(event.path)  # relative to the reference, it seems
        print(event.data)  # new data at /reference/event.path. None if deleted
    
        node = str(event.path).split('/')[-2] #you can slice the path according to your requirement
        property = str(event.path).split('/')[-1] 
        value = event.data
        if (node=='sala'):
            #do something
        elif (node=='ventilacion'):
            #do something
        else:
            #do something else
    
    
    db.reference('/').listen(listener)
    

提交回复
热议问题