Skip to content

Instantly share code, notes, and snippets.

# -*- coding: UTF-8 -*-
"""
Combining the Repustate API with the SurveyMonkey API to understand open ended
responses.
"""
import requests
import json
SM_HOST = 'https://api.surveymonkey.net'
@repustate
repustate / scale.js
Created December 3, 2013 20:21
We need to project our map data as well as scaling and centering it so it fits in our viewport.
// Values to centre and scale the map of Toronto.
var x = -14100
var y = 7300
var scale = 66700
// Define map projection.
projection = d3.geo.albers().translate([x, y]).scale([scale])
// Define path generator.
var path = d3.geo.path().projection(projection);
@repustate
repustate / points.js
Created December 3, 2013 20:18
Insert markers for tweets based on lat/long. Colour coded based on sentiment.
d3.json('/media/js/points.json', function(err, json) {
svg.select('#points')
.selectAll('circle')
.data(json)
.enter()
.append("circle")
.attr("r",4)
.style('fill-opacity', 0.7)
.style('display', function(d) {
if (d.dd == base_date.getDate() && d.mm == base_date.getMonth()+1) {
@repustate
repustate / toronto.js
Created December 3, 2013 20:17
Loading a map of Toronto's wards from a GeoJSON file and rendered using d3.js
// Load in GeoJSON data
d3.json("/media/js/toronto.json", function(json) {
//Bind data and create one path per GeoJSON feature
svg.select('#paths').selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.attr("name", function(d) {
import requests
url = "http://social.repustate.com/YOUR_API_KEY/get-data.json?datasource_id=DATASOURCE_ID&since=2013-10-28&until=2013-11-27"
response = requests.get(url).json()
@repustate
repustate / robford.py
Created December 3, 2013 01:38
Using Repustate's API to create a data source to monitor Twitter for information about Rob Ford.
import requests
API_KEY = 'YOUR_API_KEY'
BASE_URL = 'http://social.repustate.com/%(api_key)s/%(call)s.json'
# Create a new data source.
kwargs = {'api_key':API_KEY, 'call':'add-datasource'}
response = requests.post(BASE_URL % kwargs, {'name':'Rob Ford', 'language':'en', 'niche':'general'})
datasource_id = response.json()['datasource_id']
def memo(f):
"Memoize function f."
table = {}
def fmemo(*args):
if args not in table:
table[args] = f(*args)
return table[args]
fmemo.memo = table
return fmemo
def create_rule(rule, ignore_case=False):
"""
Use Python's AST parser to generate a tree which we'll traverse to create a
regular expression.
"""
results = parser.tokens(rule.strip())
ast_string = ''
unicode_strings = []
def process(node):
if isinstance(node, ast.Num):
return str(node.n)
elif isinstance(node, ast.Name):
return node.id
elif isinstance(node, ast.Str):
return r"\b%s\b" % node.s
elif isinstance(node, ast.Expr):
return process(node.value)
@repustate
repustate / gist:5752105
Created June 10, 2013 20:43
Data mining Twitter using Python and the Repustate Social API.
import json
import requests
API_KEY = 'YOUR_API_KEY'
BASE_URL = 'http://social.repustate.com/%(api_key)s/%(call)s.json'
# Create a new data source.
kwargs = {'api_key':API_KEY, 'call':'add-datasource'}
response = requests.post(BASE_URL % kwargs, {'name':'Testing', 'language':'en', 'niche':'general'})
datasource_id = json.loads(response.content)['datasource_id']