tweet_idから得られる情報(Python)
2本目の記事です。こんな感じでツイッターについていろいろ書いていきます。
完成コード
tweetid.py
from datetime import datetime
tweet_id = 279622981959970816
print("tweet_id =",tweet_id)
tweet_raw = format(tweet_id,'016b')
print ("tweet_raw =",tweet_raw)
tweet_raw=format(int(tweet_raw), '064')[1:]
sequence = tweet_raw[51:]
worker_id = tweet_raw[46:-12]
datacenter_id = tweet_raw[41:-17]
machine_id = datacenter_id + worker_id
timestamp_id = tweet_raw[:-22]
seq=int(sequence,2)
work=int(worker_id,2)
data=int(datacenter_id,2)
machine=int(machine_id,2)
timestamp=int(timestamp_id,2)
unixtime = (timestamp + 1288834974657)/1000
time = datetime.fromtimestamp(unixtime)
print("sequence =",seq)
print("worker_id =",work)
print("datacenter_id =",data)
print("machine_id =",machine)
print("timestamp_id =",timestamp)
print("unixtime=",unixtime)
print(time)
本当に合ってるのかよ!ってことについては、
https://www.slideshare.net/pfi/id-15755280
ここと一致させているのでしっかり取得できているはずです。
説明
ツイートidを2進数に分解し、最終的に左に63桁になるように0埋めして、
tttttttttttttttttttttttttttttttttttttttttdddddwwwwwssssssssssss
000001111100001011010111001111011011110110000100111000000000000
t=timestamp(UNIX時間を修正したもの)
d=datacenter_id(文字通り)
w=worker_id(文字通り)
dw=machine_id(文字通り)
s=seqence(連番)
というようになっています。
時間だけ取得する関数が欲しい!という場合はこちらをどうぞ
gettime.py
def get_time(id):
two_raw=format(id,'016b').zfill(64)
unixtime = int(two_raw[:-22],2) + 1288834974657
ime = datetime.datetime.fromtimestamp(unixtime/1000)
return ime
定型文
ツイッター(@kenkensz9)にいつもいるので何かあればどうぞ
よろしければいいねお願いします!
Author And Source
この問題について(tweet_idから得られる情報(Python)), 我々は、より多くの情報をここで見つけました https://qiita.com/kenkensz9/items/4592b3a5e52d0e58f980著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .