Skip to content

Instantly share code, notes, and snippets.

@ntanya
ntanya / gist:816cba067f0e0dccc524
Last active April 27, 2021 05:35
Python script to check HTTP status and redirect chains
import requests
def get_status_code(url):
try:
r = requests.get(url)
print "Processing " + url
if len(r.history) > 0:
chain = ""
code = r.history[0].status_code
@ntanya
ntanya / gist:3717461
Created September 13, 2012 20:38
Python csv with twitter api lookups
import csv
import urllib2
import json
import re
def findID(str):
# extracts the status ID from teh following string example:
# "http://twitter.com/IBPTravels/statuses/75907873892339712"
matchObj = re.match('(.)+statuses\/(\d+)', str, re.I)
return matchObj.group(2)
@ntanya
ntanya / node1
Created June 26, 2012 20:18
Node.js data sharing between server and client
//Client side
app.get('/trends', function(req, res){
twitterProcessor.getTrends( function(error, trendData){
res.render('trends.jade',{ locals: {
trends:trendData // here's the data you pass to client
}
});
});
});
@ntanya
ntanya / mr2
Created June 26, 2012 20:07
Map Reduce
var map = function(){
emit(this.tag, {tag_date: this.tag_date, count:this.count});
}
var reduce = function(key, values){
var obj = {value: []};
for(var i in values){
obj.value.push({tag_date:values[i].tag_date, count:values[i].count});
}
return obj;
@ntanya
ntanya / mr1
Created June 26, 2012 20:05
Map Reduce
coll.mapReduce(
//map
function(){
emit(this.tag, {tag_date: this.tag_date, count:this.count});
},
// reduce
function(key, values){
var obj = {value: []};
for(var i in values){
obj.value.push({tag_date:values[i].tag_date, count:values[i].count});
@ntanya
ntanya / get3
Created June 8, 2012 22:18
GET with node
response.on('end', function(){
var dataObj = JSON.parse(completeResponse);
var saveObj = {
id: dataObj["id"],
created_at: dataObj["created_at"],
description: dataObj["description"],
followers_count: dataObj["followers_count"],
@ntanya
ntanya / get2
Created June 8, 2012 22:14
GET with node
var completeResponse = "";
request.on('response', function (response) {
response.on('data', function (chunk) {
//res.send("data: " + chunk);
completeResponse += chunk;
});
@ntanya
ntanya / get1
Created June 8, 2012 22:09
GET with node
app.get('/', function(req, res){
var user = "twitterapi;
var completeResponse = "";
var options = {
host: 'api.twitter.com',
port: 80,
path: '/1/users/show.json?screen_name=' + user
};
@ntanya
ntanya / upload3
Created June 7, 2012 17:52
upload
app.post('/', function(req, res){
var temp_path = req.files.uploadfile.path;
var save_path = './public/images/' + req.files.uploadfile.name;
fs.rename(temp_path, save_path, function(error){
if(error) throw error;
fs.unlink(temp_path, function(){
if(error) throw error;
@ntanya
ntanya / upload2
Created June 7, 2012 17:51
file upload
var express = require('express');
var fs = require('fs'); // we will need it for file uploads
var app = module.exports = express.createServer();
app.configure(function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));