Skip to content

Instantly share code, notes, and snippets.

@lmccart
Last active August 29, 2015 14:15
Show Gist options
  • Save lmccart/33d461c674eca9187ff0 to your computer and use it in GitHub Desktop.
Save lmccart/33d461c674eca9187ff0 to your computer and use it in GitHub Desktop.
////// JQUERY
// openweathermap
$(document).ready(function(){
$.getJSON("http://api.openweathermap.org/data/2.5/weather?id=5128581&units=imperial", function(data) {
alert("Temperature is: " + data.main.temp );
});
});
// nytimes
var url = "http://api.nytimes.com/svc/search/v2/articlesearch.jsonp?q=obama&api-key=sample-key";
// append to body
$('body').append();
////// P5
// draw humidity
function drawWeather(weather) {
// Get the loaded JSON data
console.log(weather); // inspect the weather JSON
var humidity = weather.main.humidity; // get the main.humidity out of the loaded JSON
console.log(humidity); // inspect the humidity in the console
background(40, 90, 200);
fill(0, 255, 255, humidity); // use the humidity value to set the alpha
for (var i = 0; i < 50; i++) {
ellipse(random(width), random(height), 30, 30);
}
}
// draw clouds
function drawData(data) {
print(data.list);
for (var i=0; i<data.list.length; i++) {
var x =
ellipse(100+150*i, 100, data.list[i].clouds, data.list[i].clouds);
text("day "+i, 100+150*i, 200);
}
}
// weekly weather viz
var url = "http://api.openweathermap.org/data/2.5/forecast/daily?lat=35&lon=139&cnt=10&mode=json"
function setup() {
createCanvas(windowWidth, windowHeight);
loadJSON(url, handleData);
}
function handleData(data) {
console.log(data.list);
for (var i=0; i<data.list.length; i++) {
var numClouds = data.list[i].clouds;
var humidity = data.list[i].humidity;
fill(200, 50, 100, humidity);
ellipse(i*100+50, height/2, numClouds, numClouds);
text("day "+i, i*100+50, height/2);
}
}
// weather motion input
var x = 300;
var y = 300;
var speed = 0;
var input;
var button;
var url = "http://api.openweathermap.org/data/2.5/weather?q=";
function setup() {
createCanvas(windowWidth, windowHeight);
background(200);
input = createInput();
input.position(50, 50);
button = createButton("submit");
button.position(50, 80);
button.mousePressed(updateData);
}
function draw() {
background(200);
ellipse(x, y, 50, 50);
x += speed;
}
function handleData(weatherData) {
speed = weatherData.wind.speed;
}
function updateData() {
var city = input.value(); // text in the box
console.log(city);
//var city = "NewYork,USA";
loadJSON(url+city, handleData);
x = 300;
}
////// SERVI
// Install servi node module: npm install servi
var servi = require('servi');
// Install restclient node module: npm install node-restclient
var restclient = require('node-restclient');
var app = new servi(true);
port(8080);
route('/', requestHandler);
function requestHandler(request) {
var url = 'http://api.openweathermap.org/data/2.5/weather?q=NewYork,USA';
restclient.get(url, function(data) {
data = JSON.parse(data);
console.log(data.coord.lon)
});
request.respond("Hello World");
}
start();
// serve json
request.header("application/json");
request.respond(JSON.stringify(data.statuses));
// Twitter
var Twit = require('twit');
var twit = new Twit({
consumer_key: 'YOUR_CONSUMER_KEY',
consumer_secret: 'YOUR_CONSUMER_SECRET',
access_token: 'YOUR_ACCESS_TOKEN',
access_token_secret: 'YOUR_ACCESS_TOKEN_SECRET'
});
twit.get('search/tweets', { q: 'banana since:2011-11-11', count: 100 }, function(err, data, response) {
console.log(data)
})
// Twitter front/back end
route('/statuses', getData);
serveFiles("public");
loadJSON("/statuses", drawData);
function drawData(data) {
console.log(data);
background(0);
for (var i=0; i<data.length; i++) {
text(data[i].text, 0, i*12);
}
}
// servi routing multiple paths
route('/', run);
route('/one',handleOne);
route('/two',handleTwo);
route('/buckle/my/shoe',buckleMyShoe);
// servi routing with params
route('/profile/:username',showProfile);
var username = request.params.username;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment