Skip to content

Instantly share code, notes, and snippets.

@dylandy
Last active August 29, 2015 14:11
Show Gist options
  • Save dylandy/50bc8ce9a28c0ec082a4 to your computer and use it in GitHub Desktop.
Save dylandy/50bc8ce9a28c0ec082a4 to your computer and use it in GitHub Desktop.
Dcard 發文男女比
require 'rubygems'
require 'json'
require 'open-uri'
module Counter
def self.go!
count()
end
def self.count
gender = []
# Dcard 97 以後的 api 不回應
(1..97).each do |i|
begin
tmp = JSON.load(open("http://www.dcard.tw/api/forum/all/#{i}/"))
rescue
# 中間出現 error 時先讓 server 休息一下
puts "error in #{i} sleeping..."
puts $!
puts $@
sleep(30)
tmp = JSON.load(open("http://www.dcard.tw/api/forum/all/#{i}/"))
end
tmp.each do |j|
gender << j["member"]["gender"]
end
puts "fetched #{i}"
end
#約等於 1.5
puts gender.count("F")/ gender.count("M").to_f
end
end
Counter.go!
@dylandy
Copy link
Author

dylandy commented Dec 8, 2014

JS 無 ajax 版本:

var i = 0;
var timer;
//若是 server 正常回應的話,大約放個 3 個小時,就能將所有的發文紀錄都抓出來了
var count_boy_girl = function( callback ){
  $(window).scrollTop(i);
  i++;
  callback();
  timer = setTimeout(count_boy_girl , 10);   // 0.01 秒更新一次
}

var counting = function(){
  var tmp = [];
  // 得到所有的發文者的性別
  $('.item .item-author .head.ng-hide').each(function(){
    tmp.push($(this).hasClass("ng-show")[19])
  });
  var count_girl = 0;
  //得到女生發文者數量
  for( var i = 0 ; i < tmp.length ; i++ ){
    if( tmp[i] == "=" ){
      count_girl++;
    }
  }

  //得到所有發文者的性別比
  console.log(count_girl / (tmp.length -count_girl))
}

  localStorage.setItem("start_time" , new Date());
  var current = new Date();
  count_boy_girl()

  //下面很久以後再執行
  var starting_time  = new Date( localStorage.start_time );
  if( (current.getHours() - starting_time.getHours()) > 3 ){
    clearTimeout( timer );
  }
  counting()

@dylandy
Copy link
Author

dylandy commented Dec 10, 2014

JS ajax 版本:

  var  male = 0;
  var female = 0;
  for( var i = 1 ; i < 98 ; i++ ){
  $.ajax({
     type: "GET",
     url: "http://www.dcard.tw/api/forum/all/" + i + "/",
     dataType: "json",
     success: function(json){
        $(json).each(function(){
         if(this.member.gender=="M"){male+=1}else{female+=1}
        })
      },
      error: function(){
        console.log("error");
      }
   });
  }
//印出發文者男女比
console.log((female / male)*100)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment