Skip to content

Instantly share code, notes, and snippets.

@giastfader
Created February 17, 2014 16:29
Show Gist options
  • Select an option

  • Save giastfader/9053882 to your computer and use it in GitHub Desktop.

Select an option

Save giastfader/9053882 to your computer and use it in GitHub Desktop.
Import StackMob data into BaasBox
import codecs #to manage unicode files
import json #to manipulate JSON objects
import datetime #to manipulate and format timestamps
import requests #to call BaasBox APIs
import base64 #to encode the Basic Auth header
baasbox_address = "http://localhost:9000/" #the BaasBox server address. NOTE: be sure to add the / at the end of the URL
baasbox_admin_user = "admin" #the BaasBox user to use when a BaasBox is called
baasbox_admin_password = "admin" #the password to use
baasbox_app_code = "1234567890" #the BaasBox instance application code
baasbox_collection = "product" #the BaasBox collection name
date_format = '%Y-%m-%dT%H:%M:%S.000+000' #BaasBox formats dates in this way
date_fields = ["createddate", "lastmoddate"] #by default all StackMob objects have at least these date fields
#these are the headers used to call BaasBox APIs
basicauth = base64.b64encode(baasbox_admin_user + ":" + baasbox_admin_password) #The Basic Authentication will be used
headers = {u'Authorization':u"Basic " + basicauth,u'X-BAASBOX-APPCODE':baasbox_app_code,u'Content-Type':'application/json'}
#converts unix-timestamps in strings
def formatdate( timestamp ):
#since StackMob uses milliseconds, we have to calculate seconds before apply the formatting rule
return datetime.datetime.fromtimestamp(timestamp/1000).strftime(date_format)
#grants read permission on the BaasBox object to all registered users
def grantread ( id ):
res = requests.put(baasbox_address+"document/" + baasbox_collection + "/" + id + "/read/role/registered","{}",headers=headers)
#inserts an object into BaasBox
#uses the collection name and BaasBox credentials defined earlier
def insertobject (json_object):
res = requests.post(baasbox_address+"document/" + baasbox_collection ,data=json.dumps(json_object),headers=headers)
if(res.status_code == 200):
#extracts the BaasBox id from the response
baasbox_obj=json.loads(res.text)["data"]
bb_id = baasbox_obj["id"]
#grants permission on the object
grantread (bb_id)
message = json_object["_id"] + " --> " + bb_id
print message
else:
error = "** Error inserting object " + json_object["_id"] + " BaasBox replied: " + res.text
print error
#create the collection on BaasBox, if it does not exist
def createbaasboxcollection ():
res = requests.post(baasbox_address+"admin/collection/" + baasbox_collection ,"{}",headers=headers)
# --------MAIN --------
#creates the collection on BaasBox
createbaasboxcollection()
#opens the file to import
with codecs.open("product.json",encoding='UTF-8') as f:
#reads the exported objects (one per line)
for line in f:
json_line = json.loads(line)
#format the date fields
for df in date_fields:
json_line[df] = formatdate( json_line[df] )
#insert the object
insertobject (json_line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment