Skip to content

Instantly share code, notes, and snippets.

@bmcbride
bmcbride / find-in-json.js
Last active November 17, 2019 00:28 — forked from iwek/find-in-json.js
//return an array of objects according to key, value, or key and value matching
function getObjects(obj, key, val) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getObjects(obj[i], key, val));
} else
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
if (i == key && obj[i] == val || i == key && val == '') { //
@bmcbride
bmcbride / fulcrum-download-videos-tracks.py
Last active August 5, 2016 02:33
Download all the videos for a specific form (with optional gpx tracks)
import sys
import urllib2
import os
from fulcrum import Fulcrum
api_key = raw_input('api_key:')
form_id = raw_input('form_id:')
if api_key == '' or api_key == 'your_api_key' or form_id == '' or form_id == 'your_form_id':
sys.exit('api_key and form_id are required!')
@bmcbride
bmcbride / fulcrum-report-rename.sh
Last active July 22, 2016 00:14
Fulcrum PDF reports are named with the record's unique fulcrum_id. This bash script loops through the parent CSV file and renames the PDF files based on the index of user-specified fields.
#!/bin/bash
INPUT=fire_hydrants.csv
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read -a csv_line
do
mv ${csv_line[0]}.pdf ${csv_line[8]}-${csv_line[0]}.pdf
done < $INPUT
@bmcbride
bmcbride / fulcrum_mysql_webhook.php
Last active August 4, 2018 12:24
Fulcrum Webhook script for syncing data shares to a MySQL database. Requires PHP with PDO & allow_url_fopen enabled.
<?php
/**
* Title: Fulcrum Webhook for syncing data shares to MySQL database
* Notes: Requires PHP with PDO & allow_url_fopen enabled
* Author: Bryan R. McBride
* Source: https://gist.github.com/bmcbride/44afdc10ee943b4e7b92
*/
# Fulcrum app information
$formID = 'your-fulcrum-form-id';
@bmcbride
bmcbride / fulcrum-update-records-from-csv.py
Last active October 13, 2017 01:22
Update Fulcrum records from a CSV file.
import csv
from fulcrum import Fulcrum
from fulcrum.exceptions import NotFoundException
api_key = 'your-api-key'
form_id = 'your-form-id'
fulcrum = Fulcrum(key=api_key)
updates = csv.reader(open('record-updates.csv'), delimiter=',')
# skip header
@bmcbride
bmcbride / fulcrum-data-share-cartodb-webhook.js
Created September 18, 2015 01:30
Fulcrum Data Share CartoDB Webhook
function doPost(e){
return handleResponse(e);
}
function handleResponse(e) {
var jsonString = e.postData.getDataAsString();
var payload = JSON.parse(jsonString);
var formId = "my-form-id";
var shareToken = "my-data-share-token";
var cartodbTable = "my-cartodb-table";
@bmcbride
bmcbride / move.bat
Created October 26, 2015 13:53
Batch organize Fulcrum photos into different folders.
move /Y ad0c334e-b57f-471f-89f1-2ca17988b74c.jpg photos-a
move /Y 88e60f5c-15e9-4d68-8e79-4b659f66157b.jpg photos-a
move /Y 4da2cd00-d3a7-4468-997e-042303dc7137.jpg photos-a
move /Y bc0edbe2-45b0-4ad5-9b35-a8c3e623634d.jpg photos-a
move /Y 9fa41b28-24c8-4f8f-abf5-08604a043d13.jpg photos-a
move /Y e28c7d9c-06af-43bc-8f8c-1862f0bc6642.jpg photos-a
move /Y 95360e82-f6a5-46ce-b33d-58a8f571fe11.jpg photos-b
move /Y cb39737a-a3ed-4240-91ff-d967c8151ab9.jpg photos-b
move /Y 2be31a26-a482-4802-81e9-478e5cc676e9.jpg photos-b
move /Y 4b4aeba8-7d78-4a41-9c61-b5bd60a484b2.jpg photos-b
@bmcbride
bmcbride / fulcrum-turf.js
Created October 29, 2015 20:41
Turf.js + javascript.util for use in a Fulcrum calculated field
/*
javascript.util is a port of selected parts of java.util to JavaScript which
main purpose is to ease porting Java code to JavaScript.
The MIT License (MIT)
Copyright (C) 2011-2014 by The Authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@bmcbride
bmcbride / fulcrum-topojson.js
Last active October 29, 2015 20:44
Topojson.js for use in a Fulcrum calculated field
/*
Copyright (c) 2012, Michael Bostock
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
@bmcbride
bmcbride / polygon_insert.sql
Created December 4, 2015 21:28 — forked from andrewxhill/polygon_insert.sql
Polygon insert Named Function
-- Create a function withn the security set to Definer so that it can insert
CREATE OR REPLACE FUNCTION AXH_NewMinneapolis(integer, geometry) RETURNS integer
AS 'INSERT INTO minneapolis (parent_id, the_geom) (SELECT id, geom FROM (SELECT $1 id,$2 geom) a WHERE ST_NPoints(geom) < 100) RETURNING cartodb_id;'
LANGUAGE SQL
SECURITY DEFINER
RETURNS NULL ON NULL INPUT;
--Grant access to the public user
GRANT EXECUTE ON FUNCTION AXH_NewMinneapolis(integer, geometry) TO publicuser;