Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@foufrix
Last active October 13, 2017 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foufrix/6f460fa5ada1b27b35f7a4f842459a79 to your computer and use it in GitHub Desktop.
Save foufrix/6f460fa5ada1b27b35f7a4f842459a79 to your computer and use it in GitHub Desktop.
Algolia Firebase Python Import
#Install algoliasearch, pyrebase and use Python 3
from algoliasearch import algoliasearch
import pyrebase
import json
import time
# Algolia config
client = algoliasearch.Client("Id", "Apikey")
index = client.init_index('YourIndex')
print ("Algolia OK")
#For TESTING :
#index = client.init_index('test')
# Firebase config
config = {
"apiKey": "your-apikey",
"authDomain": "yourdomain.firebaseapp.com",
"databaseURL": "https://yoururl.firebaseio.com",
"projectId": "yourproject-ID",
"storageBucket": "yourblabla.appspot.com",
"messagingSenderId": "id",
"serviceAccount": "clef.json"
}
firebase = pyrebase.initialize_app(config)
print ("Firebase OK")
# Get a reference to the auth service
auth = firebase.auth()
db = firebase.database()
counter = 0
counterLat = 0
counterLng = 0
counterStartTime = 0
#Get the actuale date and convert to d - 2 to get all events safe
timestamp = int(time.time())
yesterdaydate = timestamp - 172800
#Get all the events from firebase
events = db.child("events").get()
print("Events from firebase downloaded !!")
for event in events.each():
#Get firebase key of the event
childKey = event.key();
#Get data of the event
childData = event.val();
#Get the starttime unix
try:
startTimestamp = childData['start_time_unix']
#Only add Event that has not passed yet
if startTimestamp > yesterdaydate:
print("StartDate : " + childData['start_time'])
#Add Unique key to the data
childData.update({'objectID' : childKey})
#Delete description or anything esle too heavy for algolia to make it smoother (part thatyou do'nt need in your bdd)
del childData['description']
#Retrieve latitude and longitude
try:
lat = float(childData['place']['location']['latitude'])
# Retrive error if no lat lng
except:
print('Oho no lat !')
counterLat = counterLat + 1
try:
lng = float(childData['place']['location']['longitude'])
except:
print("Oho pas de longitude !")
counterLng = counterLng + 1
#Add _geoloc lat lng to algolia object to make it searcheable by geoloc
childData.update({"_geoloc": {
"lat": lat,
"lng": lng
}})
# Import the event to algolia
index.save_object(childData)
#Add Counter + Data imported
counter = counter + 1
counterString = str(counter)
childKeyString = str(childKey)
counterLatString = str(counterLat)
counterLngString = str(counterLng)
print ("Total event : " + counterString + " , Event added ID : " + childKeyString)
#Some object have no start_time_unix, just ignore them
except:
counterStartTime = counterStartTime + 1
counterStartTimeString= str(counterStartTime)
print("Pas de start_time_unix, total : " + counterStartTimeString)
#Print result of imporation
print("Importation done !!! Total event imported : " + counterString)
print ("Oprhan event -> counterLat = " + counterLatString + " | counterLng = " + counterLngString)
print ("Orphan event -> No Start time unix : " + counterStartTimeString)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment