LoginSignup
5
1

More than 3 years have passed since last update.

tweet_idから得られる情報(Python)

Last updated at Posted at 2020-02-07

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)

こうprintされます。
bandicam 2020-02-08 01-14-41-754.jpg

本当に合ってるのかよ!ってことについては、
bandicam 2020-02-08 00-50-10-563.jpg
https://www.slideshare.net/pfi/id-15755280
ここと一致させているのでしっかり取得できているはずです。

説明

ツイートidを2進数に分解し、最終的に左に63桁になるように0埋めして、
tttttttttttttttttttttttttttttttttttttttttdddddwwwwwssssssssssss
000001111100001011010111001111011011110110000100111000000000000bandicam 2020-02-08 01-09-57-277.jpg

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)にいつもいるので何かあればどうぞ
よろしければいいねお願いします!

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1