When using Sina Weibo APIs, we sometimes need to get a Weibo url, but the Weibo status field obtained from interfaces such as statusespublic_timeline does not include any, status contains a mid field. we can calculate the url through mid.
Before starting the calculation, it is necessary to describe what is base62 encoding. It is actually an interchange between decimal and 62-digit. For the 62-digit system, after the value ranges from 0 to 9, 10 is represented by the lowercase letter a, followed by 26 letters, and z is 35. then 36 is the capital letter, until 61 is the capital letter Z. Therefore, we can implement the encode and decode encoded in base62 decimal digits. The following code is actually from stackoverflow:
The code is as follows:
ALPHABET = "0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
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
The following describes the conversion from url to mid. For a Sina Weibo url, it is like: Weibo ". It is actually very easy to calculate. from the group of four characters in front of the back, we will get:
The code is as follows:
Z
579 H
Z9Wr
Decode each string in base62 encoding, and the following decimal numbers are obtained:
The code is as follows:
35
1219149
8379699
After they are combined, the mid is "3512191498379699 ". It should be emphasized that for strings except the start, if the given decimal number is less than 7 digits, the first digit must be supplemented with 0. For example, if the obtained decimal numbers are: 35, 33040, and 8906190, you must add two zeros before 33040.
The code is as follows:
The code is as follows:
Def url_to_mid (url ):
'''
>>> Url_to_mid ('z0jh2lomb ')
3501756485200075L
>>> Url_to_mid ('z0ijpwgk7 ')
3501703425689247l
>>> Url_to_mid ('z0igabdsn ')
3501701648871479L
>>> Url_to_mid ('z08aubmu ')
3500330408906116l
>>> Url_to_mid ('z06ql6b28 ')
3500247231472384L
>>> Url_to_mid ('yctxn8ixr ')
3491700092079471L
>>> Url_to_mid ('yat1n2xa ')
3486913690606804L
'''
Url = str (url) [:-1]
Size = len (url)/4 if len (url) % 4 = 0 else len (url)/4 + 1
Result = []
For I in range (size ):
S = url [I * 4: (I + 1) * 4] [:-1]
S = str (base62_decode (str (s )))
S_len = len (s)
If I <size-1 and s_len <7:
S = (7-s_len) * '0' + s
Result. append (s)
Result. reverse ()
Return int (''. join (result ))
It is very easy to convert the mid into a url. for a mid, we use base62 encoding to encode each 7-bit forward. It is also worth noting that for each 7 digits, except for the first group, if the number in the 62 hexadecimal format is less than four digits, it must be supplemented with 0.
The code is as follows:
Def mid_to_url (midint ):
'''
>>> Mid_to_url (3501756485200075)
'Z0jh2lomb'
>>> Mid_to_url (3501703397689247)
'Z0ijpwgk7'
>>> Mid_to_url (3501701648871479)
'Z0igabdsn'
>>> Mid_to_url (3500330408906190)
'Z08aubmu'
>>> Mid_to_url (3500247231472384)
'Z06ql6b28'
>>> Mid_to_url (3491700092079471)
'Yctxn8ixr'
>>> Mid_to_url (3486913690606804)
'Yat1n2xa'
'''
Midint = str (midint) [:-1]
Size = len (midint)/7 if len (midint) % 7 = 0 else len (midint)/7 + 1
Result = []
For I in range (size ):
S = midint [I * 7: (I + 1) * 7] [:-1]
S = base62_encode (int (s ))
S_len = len (s)
If I <size-1 and len (s) <4:
S = '0' * (4-s_len) + s
Result. append (s)
Result. reverse ()
Return ''. join (result)
Run doctest and you can see that all the test cases have passed.
In the end, I don't quite understand why Sina Weibo does not directly include the url in the field, while Sina Weibo's open platform also has many non-conforming places, in fact, the content of this article does not have any technical content, but it is just for developers to make a try. For example, the refresh token issue is not listed here.