Use Python for the mid and URL conversion instances of Sina Weibo (10-in and 62-binary) _python

Source: Internet
Author: User
Tags strlen

However, the status contains a mid field that, through mid, we can actually compute to get the URL.

It is necessary to explain what the BASE62 encoding is before starting the calculation. It is actually a decimal and 62-bit binary interchange. For 62 binary, from 0 to 9, 10 is represented in lowercase a, followed by 26 letters, to Z 35, then 36 to uppercase A, until 61 is capital Letter Z. So, we can implement decimal digit base62 encoding encode and decode. The following code actually comes from StackOverflow:

Copy Code code as follows:

Alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

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 is the conversion of the URL to mid. For a Sina Weibo URL, it is shaped like: http://weibo.com/2991905905/z579Hz9Wr, the middle number is the user's UID, it is important to follow the string "Z579HZ9WR". It is also very simple to calculate, from the back four characters in a group, you get:

Copy Code code as follows:

Z
579H
Z9wr

by BASE62 encoding each string to decode, you can get their decimal digits, respectively:

Copy Code code as follows:

35
1219149
8379699

Put them together to get mid: "3512191498379699". The emphasis here is that for a string other than the beginning, if the resulting decimal number is less than 7 digits, you need to complement 0 in the front. For example, the resulting decimal number is: 35,33040,8906190, you need to add two 0 in front of 33040.
The code is as follows:

Copy Code code as follows:

def url_to_mid (URL):
'''
>>> url_to_mid (' Z0jh2lomb ')
3501756485200075L
>>> url_to_mid (' Z0ijpwgk7 ')
3501703397689247L
>>> url_to_mid (' Z0igabdsn ')
3501701648871479L
>>> url_to_mid (' Z08aubmue ')
3500330408906190L
>>> url_to_mid (' z06ql6b28 ')
3500247231472384L
>>> url_to_mid (' YCTXN8IXR ')
3491700092079471L
>>> url_to_mid (' Yat1n2xra ')
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))

Mid to URL is also very simple, for a mid, we from the forward each 7-digit group, with base62 code to encode, spell it. Also note that every 7 sets of numbers, in addition to the first group, if the resulting 62-digit number is less than 4, you need to complement 0.

Copy Code code 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)
' Z08aubmue '
>>> Mid_to_url (3500247231472384)
' Z06ql6b28 '
>>> Mid_to_url (3491700092079471)
' Yctxn8ixr '
>>> Mid_to_url (3486913690606804)
' Yat1n2xra '
'''
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 can see all the test cases passed.

Finally I do not quite understand why Sina Weibo does not directly include the URL in the field, and Sina Weibo open platform also has a lot of substandard places, in fact, the content of this article does not have any technical content, but is to let developers toss about. There are also issues like the refresh token, and so on, here are no one by one enumerations.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.