@hkai-ai/weibo-api
Version:
香港人工智能协会封装的新浪微博API
32 lines (30 loc) • 979 B
JavaScript
import { ofetch } from 'ofetch';
import { Effect } from 'effect';
/**
* 根据用户名搜索用户的 uid
* @param username
* @returns
*/
const searchForUserUIDEffect = username => Effect.tryPromise({
try: async () => {
const encodedUsername = encodeURIComponent(username);
const url = `https://m.weibo.cn/n/${encodedUsername}`;
const response = await ofetch(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
});
const match = response.match(/"value":"(\d+)"/);
if (match && match[1]) {
return match[1];
}
return null;
},
catch: error => new Error(`Failed to fetch UID: ${error.message}`)
});
/**
* 根据用户名搜索用户的 uid,如果是 null 表示搜索不到
* @param username
* @returns
*/
export const searchForUserUID = username => Effect.runPromise(searchForUserUIDEffect(username));