Skip to content

Instantly share code, notes, and snippets.

validateAddress : function(addr) {
that = this;
this.geocoder.geocode({'address': addr, 'region' : 'us'}, function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
that.location_type = results[0]['geometry']['location_type'];
if (that.isValidLocationType()) {
that.followValidAddress();
that.cur_google_suggestion = results[0].formatted_address;
} else {
// if it's not a valid address or zip, we'll *assume* it's a facility search
require 'rubygems'
require 'rest_client'
require 'nokogiri'
require 'pp'
def escape_csv(string)
string.gsub(/,/,'/')
end
class Crunch
begin
ScraperWiki
rescue NameError
require 'rest_client'
ScraperWiki = Class.new do
def self.scrape(url)
RestClient.get(url)
end
def self.save(unique_keys=[],data={})
# this stuff just allows you
# to run the script outside ScraperWiki
begin
ScraperWiki
rescue NameError
require 'rest_client'
ScraperWiki = Class.new do
def self.scrape(url)
RestClient.get(url)
def import(stat, &blk)
puts "trying #{stat}"
begin
block_given? ? blk.call : Object.const_get(stat).import(@date)
rescue GatticaError => exception
puts "*** got a GA error: #{exception} ***"
sleep 10
retry
end
end
task :import_stat_range => :environment do
entity = ENV['ENTITY']
start_date = ENV['START_DATE'].to_date
end_date = ENV['END_DATE'] ? ENV['END_DATE'].to_date : start_date
Entity.import_range(entity,start_date..end_date)
end
def sanitize_callback(callback)
if callback.nil?
return
end
callback=~/^(.{5}?)(\d?)/
return nil if callback.length == 0
return nil if $1 != "jsonp"
return nil if $2 == ""
return nil if callback.length > 128
return callback
def overwrite_stat
return if %w[Page Referrer PixelPingStory TwitterStory FacebookFan TwitterFollower].include?(self.entity.type.to_s)
new_count = self.count
existing_stat = Kernel.const_get(self.entity.type.to_s).first.stats.where(:count_date => self.count_date)[0]
if existing_stat
self.id = existing_stat.id
reload
@new_record = false
self.count = new_count
makePctGraphs : function(stat) {
var el,stat_ary,max,pct_ary;
el = "tr." + stat + "_row td .county_stat_fig";
stat_ary = _(this.$(el)).map(function(q) {
return Number($(q).attr("data-stat-val").replace(/[\$,]/g,'').replace(/(\.(.+)?)?$/,''));
});
max = _(stat_ary).max();
pct_ary = _(stat_ary).map(function(q) {
return Number(((q / max) * 100).toFixed(2));
@ashaw
ashaw / mp3scrape.rb
Created January 16, 2011 21:15
For when there are a lot of mp3s on a web page
#!/usr/bin/env ruby
require 'rubygems'
require 'rest_client'
require 'nokogiri'
USER_AGENT = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.231 Safari/534.10"
def filename(url)
URI::parse(url).path.gsub(/^.+\/(.+)$/,'\1')