Created
July 15, 2013 14:34
-
-
Save mrluanma/6000424 to your computer and use it in GitHub Desktop.
新浪微博mid转id Python代码。
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
import sys | |
ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
def rsplit(s, count): | |
f = lambda x: x > 0 and x or 0 | |
return [s[f(i - count):i] for i in range(len(s), 0, -count)] | |
def mid2str(mid): | |
result = '' | |
for i in rsplit(mid, 7): | |
str62 = base62_encode(int(i)) | |
result = str62.zfill(4) + result | |
return result.lstrip('0') | |
def str2mid(input): | |
result = '' | |
for i in rsplit(input, 4): | |
str10 = str(base62_decode(i)).zfill(7) | |
result = str10 + result | |
return result.lstrip('0') | |
def base62_encode(num, alphabet=ALPHABET): | |
"""Encode a number in Base X | |
`num`: The number to encode | |
`alphabet`: The alphabet to use for encoding | |
""" | |
if (num == 0): | |
return alphabet[0] | |
arr = [] | |
base = len(alphabet) | |
while num: | |
rem = num % base | |
num = num // base | |
arr.append(alphabet[rem]) | |
arr.reverse() | |
return ''.join(arr) | |
def base62_decode(string, alphabet=ALPHABET): | |
"""Decode a Base X encoded string into the number | |
Arguments: | |
- `string`: The encoded string | |
- `alphabet`: The alphabet to use for encoding | |
""" | |
base = len(alphabet) | |
strlen = len(string) | |
num = 0 | |
idx = 0 | |
for char in string: | |
power = (strlen - (idx + 1)) | |
num += alphabet.index(char) * (base ** power) | |
idx += 1 | |
return num | |
def test(): | |
assert mid2str('231101124784859058') == 'Bh0tgako3U' | |
assert str2mid('Bh0tgako3U') == '231101124784859058' | |
assert mid2str('3491273850170657') == 'yCirT0Iox' | |
assert str2mid('yCirT0Iox') == '3491273850170657' | |
if __name__ == '__main__': | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
感激不尽!