Skip to content

Instantly share code, notes, and snippets.

@AgustinPelaez
Created October 3, 2014 14:04
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 AgustinPelaez/4945f16795e4792ca488 to your computer and use it in GitHub Desktop.
Save AgustinPelaez/4945f16795e4792ca488 to your computer and use it in GitHub Desktop.
A custom script to pull data from Hubspot and send it to Ubidots
#!/usr/bin/python
"""
Get number of REAL users from Hubspot API, then post this data to Ubidots
"""
__author__ = "Agustin Pelaez"
from sys import argv
import ubidots
import requests
import time
def get_var_by_name(var_name, ds):
"""Search for a variable in a datasource. If found, returns the variable.
If not found, returns None."""
for var in ds.get_variables():
if var.name == var_name:
return var
return None
def main():
"""Main routine for the script."""
if len(argv) < 2:
print "Usage: %s API_KEY" % argv[0]
sys.exit(0)
api = ubidots.ApiClient(argv[1])
# Search for a data source with name matching the desired
# name of our datasource. If it doesn"t exist, create it.
ds_name = "Ubidots Monitoring"
ds = None
var_name = "Users"
var = None
for cur_ds in api.get_datasources():
if cur_ds.name == ds_name:
ds = cur_ds
break
if ds is None:
ds = api.create_datasource({"name": ds_name})
# Create a variable
for cur_var in api.get_variables():
if cur_var.name == var_name:
var = cur_var
break
if var is None:
var = ds.create_variable({"name": var_name, "unit": "users"})
# Get users data
list_hubspot = requests.get('https://api.hubapi.com/contacts/v1/lists/28?portalId=329717&hapikey=YOUR-HUBSPOT-API-KEY')
users = list_hubspot.json().get('metaData').get('size')
print users
# Send data to Ubidots
var.save_value({"value": users})
if __name__ == "__main__":
while(1):
main()
time.sleep(3600)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment