I
I
igreklpofrss2021-11-27 19:37:06
Character encoding
igreklpofrss, 2021-11-27 19:37:06

Why is the MediaId of an Instagram post encoded this way?

Hello everyone who came to answer the question.
Here is an example of a link to one of the Instagram posts https://www.instagram.com/p/CWvxAY7B0ci/

1. Getting the MediaId from the shortcode.
The given shortcode in the CWvxAY7B0ci link is the base64 encoded MediaId of the post. It is decoded as follows:
1. The character index corresponding to the shortcode character is taken from the base64 table. Taking indexes for CWvxAY7B0ci we get 2,22,47,49,0,24,59,1,52,28,34 respectively. All this is collected in an array.

base64 table
izobrazhenie-6.png


2. Then, through a for loop, each value is multiplied by 64 and added to the next value.
The code

shortcode = 'CWvxAY7B0ci'
alphabet ={
        "A": 0,
        "C": 2,
        "B": 1,
        #и т.д. все значения
         }
result = 0
for char in shortcode:
        result = result * 64 + alphabet[char]
#результат 2715604631366879010


Actually a question - why so? Why multiply each value by 64? Why sum them up? What are the rules for this? Is this formula some kind of coding standard or Instagram "feature"?

2. Getting shortcode from MediaId.
If in the previous question there was at least some understanding of what is happening, then here I don’t understand all the actions at all. Why look for the remainder and subtract it? There are even no more questions, because I don’t fully understand why and how encoding occurs in this part of the code and I don’t know what to ask. Help, tell me.
The code

media_id = int('2715604631366879010')
alphabet ={
        "A": 0,
        "C": 2,
        "B": 1,
        #и т.д. все значения
         }
short_code = ''
while media_id > 0:
        remainder = media_id % 64
        media_id = (media_id-remainder)/64
        short_code = alphabet[remainder] + short_code
#результат CWvxAY7B0ci



THANKS IN ADVANCE!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Puma Thailand, 2021-11-28
@opium

Why are base64 encoding and decoding functions available in any language?

H
hint000, 2021-11-28
@hint000

https://www.google.com/search?q=translate+numbers+from+o...
You are converting a number from 64 to decimal and vice versa.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question