Created
May 7, 2018 13:49
-
-
Save peicheng/9a3b76a83cb5f2ad1ddc8ba9a5e19acb to your computer and use it in GitHub Desktop.
Instagram shortcode to media id media id to shortcode
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
# coding=utf-8 | |
''' | |
Instagram shortcode to media id | |
media id to shortcode | |
''' | |
sid = 'BFBQEQDxVqV' | |
# 1243345632996186773 | |
nid = '' | |
alph = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_' | |
''' | |
i = 0 | |
result = 0 | |
for c in reversed(sid): | |
# print c | |
idx = alph.find(c) | |
r = int(idx) * pow(64, i) | |
# print r,bin(r) | |
result = result + r | |
i += 1 | |
print result, bin(result) | |
''' | |
def url2id(url): | |
alph = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_' | |
mid = 0 | |
for l in url: | |
# mid = mid * 64 + alph.find(l) | |
pos = alph.find(l) | |
mid = mid * 64 + pos | |
print l, mid, pos | |
return mid | |
print url2id(sid) | |
def id2url(mid): | |
alph = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_' | |
url = '' | |
while mid > 0: | |
res = mid % 64 | |
mid = (mid - res) / 64 | |
url = alph[res] + url | |
return url | |
print id2url(1243345632996186773) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment