微博API汇总及 mid和url的转换算法

微博API 汇总

1. 搜索

2.评论

附:mid-url转换函数

微博采用的是base62编码来进行mid映射

base62即 26个小写字母+26个大写字母+10个数字=62

可用python进行实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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

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'

midint = str(midint)[::-1]
size = len(midint) / 7 if len(midint) % 7 == 0 else len(midint) / 7 + 1
result = []

for i in range(int(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)

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 = str(url)[::-1]
size = len(url) / 4 if len(url) % 4 == 0 else len(url) / 4 + 1
result = []
for i in range(int(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))

微博API汇总及 mid和url的转换算法

https://blog.luzy.top/posts/794074733/

作者

江风引雨

发布于

2020-02-08

更新于

2024-07-10

许可协议

CC BY 4.0

评论