Skip to content

Instantly share code, notes, and snippets.

@giastfader
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

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

Select an option

Save giastfader/9024821 to your computer and use it in GitHub Desktop.
Import StackMob users into BaasBox
#the BaasBox server address. NOTE: be sure to add the / at the end of the URL
baasbox_address = "http://localhost:9000/"
#the BaasBox instance application code
baasbox_app_code = "1234567890"
#the HTTP header to send
headers = {u'X-BAASBOX-APPCODE':baasbox_app_code,u'Content-Type':'application/json'}
#inserts a new user into BaasBox
def insertuser (profile_object):
#format the BaasBox user object
res = requests.post(baasbox_address+"user" ,data=json.dumps(profile_object),headers=headers)
if(res.status_code != 201):
error = profile_object["username"] + " *** ERROR ***: BaasBox replied: " + res.text
print error
else:
print profile_object["username"] + " created"
#BaasBox formats dates in this way
date_format = '%Y-%m-%dT%H:%M:%S.%f'
#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(float(timestamp)/1000).strftime(date_format)[:-3] + "+0000"
#sets the username and password
json_user["username"]=json_line["_id"]
json_user["password"]=time.time()
#sets the email field, if it exists
if "email" in json_line:
json_user["visibleByTheUser"]["email"]=json_line["email"]
#sets other fields
if "city" in json_line:
json_user["visibleByRegisteredUsers"]["city"]=json_line["city"]
json_user["visibleByRegisteredUsers"]["sm_createddate"]=formatdate(json_line["createddate"])
json_user["visibleByRegisteredUsers"]["sm_lastmoddate"]=formatdate(json_line["lastmoddate"])
with codecs.open("user.json",encoding='UTF-8') as f:
#reads the exported users (one per line)
for line in f:
json_line = json.loads(line)
import codecs #to manage unicode files
import json #to manipulate JSON objects
import datetime #to manipulate and format timestamps
import time #to generate "random" passwords
import requests #to call BaasBox APIs
baasbox_address = "http://localhost:9000/" #the BaasBox server address. NOTE: be sure to add the / at the end of the URL
baasbox_app_code = "1234567890" #the BaasBox instance application code
date_format = '%Y-%m-%dT%H:%M:%S.%f' #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
headers = {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(float(timestamp)/1000).strftime(date_format)[:-3] + "+0000"
#inserts a new user into BaasBox
def insertuser (profile_object):
#format the BaasBox user object
res = requests.post(baasbox_address+"user" ,data=json.dumps(profile_object),headers=headers)
if(res.status_code != 201):
error = profile_object["username"] + " *** ERROR ***: BaasBox replied: " + res.text
print error
else:
print profile_object["username"] + " created"
# --------MAIN --------
bb_profile_template="""{
"username":"",
"password":"",
"visibleByRegisteredUsers":{
"city":null,
"sm_createddate":"",
"sm_lastmoddate":""
},
"visibleByTheUser":{
"email":null
}
}"""
#opens the file to import
with codecs.open("user.json",encoding='UTF-8') as f:
#reads the exported objects (one per line)
for line in f:
json_line = json.loads(line)
json_user=json.loads(bb_profile_template)
#sets the username and password
json_user["username"]=json_line["_id"]
json_user["password"]=time.time()
#sets the email field, if it exists
if "email" in json_line:
json_user["visibleByTheUser"]["email"]=json_line["email"]
#sets other fields
if "city" in json_line:
json_user["visibleByRegisteredUsers"]["city"]=json_line["city"]
json_user["visibleByRegisteredUsers"]["sm_createddate"]=formatdate(json_line["createddate"])
json_user["visibleByRegisteredUsers"]["sm_lastmoddate"]=formatdate(json_line["lastmoddate"])
#creates the user
insertuser (json_user)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment