Last active
August 9, 2017 16:51
-
-
Save bkj/30a8b93225ae3c3ca88fec7010632953 to your computer and use it in GitHub Desktop.
id2url.py
This file contains 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
#!/usr/bin/env python | |
""" | |
id2url.py | |
Convert instagram post id to the url | |
""" | |
import os | |
from string import letters | |
lookup = letters[-26:] + letters[:26] + ''.join(map(str, range(10))) + '-' + '_' | |
lookup = dict(zip(map(str, range(64)), lookup)) | |
def baseb(n, b): | |
e = n // b | |
q = n % b | |
if n == 0: | |
return '0' | |
elif e == 0: | |
return str(q) | |
else: | |
return baseb(e, b) + ':' + str(q) | |
def id2url(mid): | |
pmid = mid.split('_')[0] | |
pmid64 = baseb(long(pmid), 64).split(':') | |
pmid_enc = ''.join([lookup[m] for m in pmid64]) | |
return os.path.join('https://www.instagram.com/p/', pmid_enc) | |
id2url('1052907828199364530_242280836') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment