Skip to content

Instantly share code, notes, and snippets.

import os
import sys
import re
import websocket
import http.client as httplib
import base64
import json
import urllib
import requests
from requests.auth import HTTPBasicAuth
exports.handler = async function(event, context, callback) {
// https://api.calltrackingmetrics.com/api/v1/accounts/{{account_id}}/calls/{{id}}/tag?tag={{tag}}
const source_tag = event.activity.source_tag;
console.log("check for tag: ", source_tag);
if (source_tag && source_tag.length) {
console.log("apply source tag: ", source_tag)
await context.ctm.api_patch(`calls/${event.activity.id}/tag`, {"tag":source_tag});
context.done();
} else {
console.log("no source tag")
@taf2
taf2 / action.js
Last active April 15, 2022 18:13
Lead Form Process data from Google Ads Lead Form Extensions
/*
{"lead_id":"TeSter-123-ABCDEFGHIJKLMNOPQRSTUVWXYZ-abcdefghijklmnopqrstuvwxyz-0123456789-AaBbCcDdEeFfGgHhIiJjKkLl",
"user_column_data":[{"column_name":"Full Name","string_value":"FirstName LastName","column_id":"FULL_NAME"},
{"column_name":"User Phone","string_value":"+16505550123","column_id":"PHONE_NUMBER"},
{"column_name":"User Email","string_value":"test@example.com","column_id":"EMAIL"},
{"column_name":"Company Name","string_value":"CompanyName","column_id":"COMPANY_NAME"}],
"api_version":"1.0","form_id":40000000000,"campaign_id":281489216452814,
"google_key":"----","is_test":true,
"gcl_id":"TeSter-123-ABCDEFGHIJKLMNOPQRSTUVWXYZ-abcdefghijklmnopqrstuvwxyz-0123456789-AaBbCcDdEeFfGgHhIiJjKkLl",
"adgroup_id":20000000000,"creative_id":30000000000}
from hashlib import sha1;
import hmac;
import base64;
timestamp = '1350310273' # X-CTM-Time header
signature = 'A sample signature from CTM=' # X-CTM-Signature header
api_secret_key = 'your-api-key' # your secret key can be found in the account settings
msg = '{...}' # request body
mac = hmac.new(api_secret_key, timestamp, sha1)
mac.update(msg)
// wrapper around the customers ctm_action.js
const HTTPS = require('https');
const URL = require('url');
function http_request(opts, requestBodyCallback) {
let promise = new Promise(function(resolve, reject) {
let request = HTTPS.request(opts, function(response) {
let data = "";
response.on('data', function (chunk) { data += chunk; });
response.on('end', function () { resolve({request: request, response: response, responseBody: data}); });
$(function() {
var poll_interval = 2
var relatizer = function(){
var dt = $(this).text(), relatized = $.relatizeDate(this)
if ($(this).parents("a").length > 0 || $(this).is("a")) {
$(this).relatizeDate()
if (!$(this).attr('title')) {
$(this).attr('title', dt)
}
@taf2
taf2 / converter.js
Last active January 12, 2021 22:52
A version of https://github.com/javl/image2cpp for the command line
/*
originally from https://raw.githubusercontent.com/javl/image2cpp/master/index.html
converted to command line interface
node converter.js -f arduino -m horizontal1bit -b transparent -n VariableName -i input.bmp -o output.c
head output.c
// 'input', 100x100px
const unsigned char VariableName [] PROGMEM = {
0x00, 0x00, 0x00
options:

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

/***************************************************
This is a library for the Adafruit TTL JPEG Camera (VC0706 chipset)
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/products/397
These displays use Serial to communicate, 2 pins are required to interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing