Skip to content

Instantly share code, notes, and snippets.

@WardCunningham

WardCunningham/roster.rb

Last active Aug 29, 2015
Embed
What would you like to do?
Recent Changes by Roster
# create page of conversatons
# cron: */5 * * * * (cd wiki/farm-8080/client; CSV='...' ruby roster.rb)
# deploy: scp roster.rb fed.wiki.org:wiki/farm-8080/client/
require 'rubygems'
require 'json'
require 'csv'
require 'pp'
@report = []
@csv = ENV['CSV']
`mkdir -p rosters`
@class = 'h2l1'
@about = ''
def process sites
return unless sites.length > 0
@report << "Roster <a href=/rosters/#{@class}.html>#{@class}</a> (#{sites.length} sites)"
stuff = sort trends sites
emit @class, stuff
end
def roster
begin
csv = `curl -s #{ENV['CSV']}`
rows = CSV.parse csv
rows.shift
result = []
rows.each do |row|
subdomain, domain, email, first, last = row
if subdomain == 'Roster'
process result
@class = domain
@about = email
result = []
else
unless domain
result << subdomain.gsub(/http:\/\//, '').gsub(/\/$/,'')
else
result << "#{subdomain}.#{domain}"
end
end
end
process result
rescue
@report << "Can't process CSV roster from '@csv'"
[]
end
result
end
# exit
def fetch endpoint
text = `curl -s http://#{endpoint}.json`
JSON.parse text
end
def folks directory
result = {}
list = fetch directory
list['story'].each do |item|
next unless item['type'] == 'reference'
result[item['site']] = true
end
result.keys
end
def row entries
url = 'http://ward.asia.wiki.org'
list = []
entries.reverse.each do |entry|
url << "/#{entry['site']}/#{entry['slug']}"
list << "<img src=http://#{entry['site']}/favicon.png width=16 height=16>"
end
"<tr><td align=right>#{list.join ' '}<td><a href=#{url}>#{entries.first['title']}</a>"
end
def emit label, trends
File.open("rosters/#{label}.html",'w') do |file|
file.puts header
file.puts "<a href=http://ward.asia.wiki.org/conversation-clubs.html>Conversation Clubs</a><br>as of #{Time.now}"
file.puts "<p>#{@about}</p>"
file.puts "<center> "
file.puts "<table>"
trends.each do |entries|
file.puts row entries
end
file.puts "</table>"
file.puts footer
end
end
def prot date
date || 1305326836819
end
def sort trends
trends.each do |entries|
entries.sort! {|a,b| prot(b['date']) <=> prot(a['date'])}
end
trends.sort! {|a,b| prot(b.first['date']) <=> prot(a.first['date'])}
end
def trends sites
result = Hash.new {|h,k| h[k] = [] }
sites.each do |site|
begin
sitemap = fetch "#{site}/system/sitemap"
sitemap.each do |entry|
entry['site'] = site
result[entry['slug']] << entry
end
rescue
@report << "can't fetch sitemap for #{site}"
end
sleep 0.5
end
result.values
end
def header
<<'EOF'
<html>
<head>
<title>Conversation Clubs</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
EOF
end
def footer
<<'EOF'
<script>
if (document.referrer) {
referrer = document.referrer.split('/')
$('a').each(function(i, each){
href = $(each).attr('href').split('/')
href[2] = referrer[2]
$(each).attr('href',href.join('/'))
})
} else {
$('a').click(function(e) {
e.preventDefault();
alert("We've lost track of your wiki site. Go back and click the link to here again.")
});
}
</script>
</body>
</html>
EOF
end
# emit sort trends folks 'journal.hapgood.net/happening-folks'
@report << "Run of #{Time.now}"
roster
@report << "Completed #{Time.now}"
File.open('report.html','w'){|file| file.puts @report.join "<br>\n"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.