Created
August 14, 2012 04:57
-
-
Save mazgi/3346389 to your computer and use it in GitHub Desktop.
https://gist.github.com/2787140 とほぼ一緒。あとで整理する
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
#!/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="Zusaar Participant list" | |
opts.on("-e", "--event=event_id", Integer, "Zusaar 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('www.zusaar.com', 80) | |
response=http.get("/api/event/user/?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["event"] | |
events.each{|event| | |
users=event["users"] | |
users.each{|user| | |
u=User.new | |
u.atnd_name=user['nickname'] | |
u.is_regular=user['status'].to_i==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