Skip to content

Instantly share code, notes, and snippets.

@Ruxton
Last active Dec 21, 2015
Embed
What would you like to do?
A ruby script that fetches diaspora pods from activepods and geo locates them onto a map
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" content="text/html" http-equiv="content-type">
<title>Diaspora Pods by Country</title>
<script src="http://www.ammap.com/lib/ammap.js" type="text/javascript"></script>
<script src="http://www.ammap.com/lib/maps/js/worldLow.js" type="text/javascript"></script>
<script>
var map;
AmCharts.ready(function() {
map = new AmCharts.AmMap();
map.pathToImages = "http://www.ammap.com/lib/images/";
map.panEventsEnabled = true;
map.zoomControl.panControlEnabled = true;
map.zoomControl.zoomControlEnabled = true;
var icon = "m 95.999996,860.36222 c -53.017973,0 -95.999996,42.982 -95.999996,96 0,53.01798 42.982023,95.99998 95.999996,95.99998 53.017984,0 96.000004,-42.982 96.000004,-95.99998 0,-53.018 -42.98202,-96 -96.000004,-96 z m -16.276921,34.15384 32.553855,0 0,40.7077 38.70769,-12.58462 10.03077,30.92308 -38.67693,12.58462 23.9077,32.92307 -26.3077,19.13839 -23.938464,-32.95378 -23.938459,32.95378 -26.307691,-19.13839 23.907692,-32.92307 -38.676924,-12.58462 10.03077,-30.92308 38.707691,12.58462 0,-40.7077 z";
var dataProvider = {
mapVar: AmCharts.maps.worldLow,
images: [
<% @data.reject{ |d| d[:latitude].nil? }.each do |data| %>
<%= "{ latitude: #{data[:latitude]}, longitude: #{data[:longitude]}, svgPath: icon, color: \"#CC0000\", scale: 0.5, title: \"#{data[:url]}\", description: \"Users: #{data[:user_count]} \", labelShiftY:2 }," %>
<% end %>
]
};
map.dataProvider = dataProvider;
map.areaSettings = {
balloonText: ""
};
map.write("mapdiv");
});
</script>
<body style="font-family: Arial; border: none;">
<div id="mapdiv" style="width: 100%; height: 800px;"></div>
</body>
</head>
</html>
#!/usr/bin/env ruby
require 'geoip' # http://rubygems.org/gems/geoip
require 'erb'
require 'ostruct'
CHART_REJECT_KEYS = [:continent_code,:country_name,:country_code,:region,:error].freeze
def geo
# GeoLiteCity.dat comes from http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
@geo ||= GeoIP.new('/GeoLiteCity.dat')
end
## Method loops through https://diapod.net/active/dversions.txt
## runs get_pod_details for each pod
def fetch_pod_data
@data = []
@new_pod=false
File.open('dversions.txt').each_line do |line|
if line.start_with?("===")
@new_pod=true
next
end
if @new_pod
@data << get_pod_details(line.gsub("\n",""))
@new_pod=false
end
end
@data
end
## Fetches GEO information for IP Address of given URL
## is_for_chart rejects values in CHART_REJECT_KEYS
def get_pod_details(pod='')
pod_hash = {}
return_str = ""
puts "Fetching data for #{pod}..."
unless pod.empty?
begin
pod_geo = geo.city(pod)
pod_hash = {
latitude: pod_geo.latitude,
longitude: pod_geo.longitude,
user_count: get_user_count_for(pod),
url: pod,
continent_code: pod_geo.continent_code,
country_name: pod_geo.country_name,
country_code: pod_geo.country_code2,
region: pod_geo.real_region_name,
error: "none"
}
rescue SocketError => e
pod_hash = {
latitude: nil,
longitude: nil,
user_count: 0,
url: pod,
continent_code: nil,
country_name: nil,
country_code: nil,
region: nil,
error: e.to_s
}
end
end
pod_hash
end
## Method should be changed to get legit user counts some how
def get_user_count_for(pod)
0
end
## Parses diaspora.html.erb to html
def render_erb(collection_of_data)
template_str = File.read("diaspora.html.erb")
ERB.new(template_str).result(binding)
end
## Write chart map html
@data = fetch_pod_data
file = File.open("podmap.html",File::WRONLY|File::CREAT)
file << render_erb(@data)
file.close
## Write details txt
# coming soon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment