Last active
April 19, 2018 06:41
-
-
Save komasaru/c1326aa96c1d3ff918532e585ad09be2 to your computer and use it in GitHub Desktop.
Ruby script to get a timestamp from a tweet-id.
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/local/bin/ruby | |
# coding: utf-8 | |
#=ツイートID解析 | |
# : コマンドライン引数にツイートIDを渡して実行するとツイート日時等を算出する。 | |
# | |
# date name version | |
# 2017.01.07 Masaru Koizumi 1.00 新規作成 | |
# | |
# Copyright(C) 2017 mk-mode.com All Rights Reserved. | |
#--------------------------------------------------------------------------------- | |
# 引数 : ツイートID(半角数字) | |
#--------------------------------------------------------------------------------- | |
#++ | |
class AnalyzeTweetid | |
TW_EPOCH = 1288834974657 # 単位:ミリ秒 | |
MASK_T = "111111111111111111111111111111111111111110000000000000000000000" | |
MASK_M = "000000000000000000000000000000000000000001111111111000000000000" | |
MASK_S = "000000000000000000000000000000000000000000000000000111111111111" | |
def initialize(tweet_id) | |
@tweet_id = tweet_id.to_i | |
end | |
def exec | |
begin | |
# 各値のバイナリ文字列 | |
timestamp_b = (@tweet_id & MASK_T.to_i(2)).to_s(2) | |
machine_id_b = (@tweet_id & MASK_M.to_i(2)).to_s(2) | |
sequence_b = (@tweet_id & MASK_S.to_i(2)).to_s(2) | |
# 各値の抽出&数値化 | |
timestamp = ("%063d" % timestamp_b.to_i )[ 0,41].to_i(2) | |
machine_id = ("%063d" % machine_id_b.to_i)[41,10].to_i(2) | |
sequence = ("%063d" % sequence_b.to_i )[51,12].to_i(2) | |
# timestamp の算出 | |
timestamp = Time.at((timestamp + TW_EPOCH) / 1000.0) | |
# 結果出力 | |
puts " TWEET-ID: #{@tweet_id}" | |
puts "TIMESTAMP: #{timestamp.strftime("%Y-%m-%d %H:%M:%S.%L %Z")}" | |
puts " (MACHINE-ID: #{machine_id}, SEQUENCE: #{sequence})" | |
# timestamp のみなら、マスク処理をしなくとも、以下で充分 | |
# timestamp = Time.at(((@tweet_id >> 22) + TW_EPOCH) / 1000.0) | |
# puts " TWEET-ID: #{@tweet_id}" | |
# puts "TIMESTAMP: #{timestamp.strftime("%Y-%m-%d %H:%M:%S.%L %Z")}" | |
rescue => e | |
$stderr.puts "[ERROR][#{self.class.name}.#{__method__}] #{e}" | |
e.backtrace.each { |tr| $stderr.puts "\t#{tr}"} | |
exit 1 | |
end | |
end | |
end | |
if __FILE__ == $0 | |
exit 0 unless tweet_id = ARGV.shift | |
AnalyzeTweetid.new(tweet_id).exec | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment