Created
May 25, 2012 10:16
-
-
Save mazgi/2787140 to your computer and use it in GitHub Desktop.
ATNDの参加者一覧を出力
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'pp' | |
require 'optparse' | |
require 'net/http' | |
require 'json' | |
Version=0.5 | |
class User | |
attr_accessor :atnd_name, :is_regular | |
def initialize | |
@atnd_name='' | |
@is_regular=true | |
end | |
def <=>(other) | |
if is_regular | |
if other.is_regular | |
@atnd_name.upcase<=>other.atnd_name.upcase | |
else | |
-1 | |
end | |
else | |
if other.is_regular | |
1 | |
else | |
@atnd_name.upcase<=>other.atnd_name.upcase | |
end | |
end | |
end | |
end | |
event_id=nil | |
output_file=nil | |
show_index=false | |
line_count=0 | |
opts=OptionParser.new | |
opts.banner="ATND Participant list" | |
opts.on("-e", "--event=event_id", Integer, "ATND event id"){|v| | |
event_id=v | |
} | |
opts.on("-f", "--file=output", String, "output file path"){|v| | |
output_file=File::expand_path(v) | |
} | |
opts.on("-i", "--[no-]index", TrueClass, "show/hide index column"){|v| | |
show_index=v | |
} | |
opts.on("-n", "--lines=num", Integer, "line"){|v| | |
line_count=v if v>0 | |
} | |
opts.parse!(ARGV) | |
if !event_id | |
puts opts.help | |
exit 1 | |
end | |
http=Net::HTTP.new('api.atnd.org', 80) | |
response=http.get("/events/users/?event_id=#{event_id}&format=json") | |
if !response||!response.code||response.code.to_i!=200 | |
puts "HTTP Error" | |
exit 1 | |
end | |
body=response.body | |
result=JSON.parse(body) | |
personz=Array.new | |
events=result["events"] | |
events.each{|event| | |
users=event["users"] | |
users.each{|user| | |
u=User.new | |
u.atnd_name=user['nickname'] | |
u.is_regular=user['status']==1 | |
personz.push(u) | |
} | |
} | |
personz.sort! | |
if line_count>personz.size | |
torch=User.new | |
personz.fill(torch, personz.size, line_count-personz.size) | |
end | |
file=STDOUT | |
file=open(output_file, 'w', 0755) if output_file | |
index=nil | |
personz.each{|user| | |
char='' | |
if index.nil?||(!user.atnd_name.empty?&&index!=user.atnd_name[0].upcase) | |
index=user.atnd_name[0].upcase | |
if index=~/^[0-9A-Za-z]+$/ | |
char=index | |
end | |
end | |
str='' | |
str="#{char}," if show_index | |
str+='(waiting)' if !user.is_regular | |
str+=user.atnd_name | |
file.puts str | |
} | |
file.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment