Created
December 23, 2014 16:59
-
-
Save WardCunningham/fcdf01be9c23fdf652b1 to your computer and use it in GitHub Desktop.
Script for Conversation Clubs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create page of conversatons | |
# cron: */5 * * * * (cd wiki/farm-8080/client; ruby trending.rb) | |
# deploy: scp trending.rb fed.wiki.org:wiki/farm-8080/client/ | |
require 'rubygems' | |
require 'json' | |
require 'pp' | |
def fetch endpoint | |
text = `curl -s http://#{endpoint}.json` | |
JSON.parse text | |
end | |
def neighbourhood | |
result = {} | |
list = fetch 'journal.hapgood.net/happening-folks' | |
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.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><a href=#{url}>#{entries.first['title']}</a><td>#{list.join ' '}" | |
end | |
# The section method inserts section markers. I missed something in the translation from coffeescript | |
# from https://github.com/fedwiki/wiki-plugin-activity/blob/master/client/activity.coffee | |
@bigger = Time.now.to_i*1000 | |
@sections = [ | |
{'date'=>@bigger-1000*60*60*24*365, 'period'=>'Years'}, | |
{'date'=>@bigger-1000*60*60*24*91, 'period'=>'a Year'}, | |
{'date'=>@bigger-1000*60*60*24*31, 'period'=>'a Season'}, | |
{'date'=>@bigger-1000*60*60*24*7, 'period'=>'a Month'}, | |
{'date'=>@bigger-1000*60*60*24, 'period'=>'a Week'}, | |
{'date'=>@bigger-1000*60*60, 'period'=>'a Day'}, | |
{'date'=>@bigger-1000*60*5, 'period'=>'an Hour'}, | |
{'date'=>@bigger-1000, 'period'=>'a few Minutes'}, | |
{'date'=>@bigger, 'period'=>'Seconds'} | |
] | |
def section smaller | |
result = '' | |
@sections.each do |section| | |
if section['date'] > smaller && section['date'] <= @bigger | |
result = "<tr><td><td> Within #{section['period']}" | |
break | |
end | |
@bigger = smaller | |
end | |
result | |
end | |
def emit trends | |
File.open('trending.html','w') do |file| | |
file.puts header | |
file.puts "Conversation Clubs<br>as of #{Time.now}<br>" | |
file.puts "See <a href=http://ward.asia.wiki.org/conversation-clubs.html>Conversation Clubs</a>" | |
file.puts "<table>" | |
trends.each do |entries| | |
# file.puts section prot(entries.first['date']) | |
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 | |
result = Hash.new {|h,k| h[k] = [] } | |
neighbourhood.each do |site| | |
sitemap = fetch "#{site}/system/sitemap" | |
sitemap.each do |entry| | |
entry['site'] = site | |
result[entry['slug']] << entry | |
end | |
sleep 0.5 | |
end | |
result.values | |
end | |
def header | |
<<'EOF' | |
<html> | |
<head> | |
<title>Conversation Clubs</title> | |
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script> | |
</head> | |
<body> | |
EOF | |
end | |
def footer | |
<<'EOF' | |
<script> | |
referrer = document.referrer.split('/') | |
$('a').each(function(i, each){ | |
href = $(each).attr('href').split('/') | |
href[2] = referrer[2] | |
$(each).attr('href',href.join('/')) | |
}) | |
</script> | |
</body> | |
</html> | |
EOF | |
end | |
emit sort trends | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment