Skip to content

Instantly share code, notes, and snippets.

@pxsta
Created February 2, 2015 08:22
Show Gist options
  • Save pxsta/6b48ff35f6f6e1e6e48c to your computer and use it in GitHub Desktop.
Save pxsta/6b48ff35f6f6e1e6e48c to your computer and use it in GitHub Desktop.
BF4の各月ごとのプレイ時間を取得する
#BF4の各月ごとのプレイ時間を取得する
require 'bundler'
Bundler.require
EMAIL=''
PASSWORD=''
USER_ID='' #http://battlelog.battlefield.com/bf4/soldier/#{USER_NAME}/stats/#{USER_ID}/pc/の#{USER_ID}部分の数値
#ログインしてCookieを取得する
res=RestClient.post('https://battlelog.battlefield.com:443/bf4/gate/login/',{:email=>EMAIL,:password=>PASSWORD,:submit=>'Log+in',:redirect=>''}) do |response, request, result, &block|
break response
end
#プレイデータのJSONを取得する
res2=RestClient.get("http://battlelog.battlefield.com/bf4/warsawoverviewhistory/#{USER_ID}/1/",{:cookies => res.cookies})
#各月のプレイ時間を計算する
my_data=JSON.parse(res2)
time_array=[]
prev_month=0
prev_year=0
prev_total_time=0
prev_month_total_time=0
current_total_time=0
my_data['data']['historicalOverviewStats'].sort_by{|key,value|key}.to_h.each_pair do |key,value|
current_year= Time.at(key.to_i).year
current_month= Time.at(key.to_i).month
prev_year=current_year if prev_year==0
prev_month=current_month if prev_month==0
#積み上げの合計プレイ時間が取れるので「月末までのプレイ時間-前の月末までのプレイ時間」で求める
prev_total_time=current_total_time
current_total_time=value['timePlayed'].to_i
if current_year != prev_year or current_month != prev_month then
#年,月,プレイ時間[秒],プレイ時間[時間]
time_array.push({'year'=>prev_year,'month'=>prev_month,'time_s'=>prev_total_time-prev_month_total_time,'time_h'=>(prev_total_time-prev_month_total_time)/60.0/60})
prev_year=current_year
prev_month=current_month
prev_month_total_time=prev_total_time
end
end
time_array.push({'year'=>prev_year,'month'=>prev_month,'time_s'=>current_total_time-prev_month_total_time,'time_h'=>(current_total_time-prev_month_total_time)/60.0/60})
#CSV形式で標準出力に書き出す
puts time_array.first.keys.join(',')
time_array.each do |elm|
puts elm.values.to_a.join(',')
end
source "https://rubygems.org"
gem 'json'
gem 'rest-client'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment