Last active
July 22, 2023 01:59
-
-
Save chris124567/6ac3a0e356cefeee17e411f4640d9b75 to your computer and use it in GitHub Desktop.
Instagram ID Parsing
This file contains hidden or 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
package api | |
import ( | |
"encoding/base64" | |
"fmt" | |
"strings" | |
"time" | |
) | |
// See https://gist.github.com/sclark39/9daf13eea9c0b381667b61e3d2e7bc11 | |
// and https://instagram-engineering.com/sharding-ids-at-instagram-1cf5a71e5a5c?gi=8583b512a3ca | |
const ( | |
InstagramEpoch = 1314220021721 | |
) | |
var ( | |
encoder = base64.NewEncoding(`ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_`) | |
) | |
type ID int64 | |
func NewID(unixMillis, shardID, sequenceID int64) ID { | |
result := (unixMillis - InstagramEpoch) << (64 - 41) | |
result = result | (shardID << (64 - 41 - 13)) | |
result = result | (sequenceID % 1024) | |
return ID(result) | |
} | |
func ShortcodeID(shortcode string) (ID, error) { | |
var decoded [16]byte | |
n, err := encoder.Decode(decoded[:], []byte(strings.Repeat("A", 12-len(shortcode))+shortcode)) | |
if err != nil { | |
return ID(0), err | |
} | |
var result int64 | |
for i := 0; i < n; i++ { | |
result <<= 8 | |
result |= int64(decoded[i]) | |
} | |
return ID(result), nil | |
} | |
func (id ID) Shortcode() string { | |
var idBytes [9]byte | |
for i := 8; i >= 0; i-- { | |
idBytes[i] = byte(id & 0xFF) | |
id >>= 8 | |
} | |
// Encode the ID bytes using base64 | |
shortcode := encoder.EncodeToString(idBytes[:]) | |
// Replace 'A' with a space | |
shortcode = strings.ReplaceAll(shortcode, "A", " ") | |
// Strip leading spaces | |
shortcode = strings.TrimLeft(shortcode, " ") | |
// Replace remaining spaces with 'A' | |
return strings.ReplaceAll(shortcode, " ", "A") | |
} | |
func (id ID) CreationTime() int64 { | |
return (int64(id) >> (64 - 41)) + InstagramEpoch | |
} | |
func (id ID) ShardID() int64 { | |
return (int64(id) >> (64 - 41 - 13)) & 0b1111111111111 | |
} | |
func (id ID) SequenceID() int64 { | |
return int64(id) & 0b1111111111 | |
} | |
func (id ID) String() string { | |
return fmt.Sprintf("%s - %d (Sequence ID: %d, Shard ID: %d, Created: %v)", id.Shortcode(), int64(id), id.SequenceID(), id.ShardID(), time.UnixMilli(id.CreationTime())) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment