Skip to content

Commit

Permalink
feat: progressBar
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilfish committed Apr 4, 2024
1 parent 1c77adf commit 7be02d5
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 12 deletions.
28 changes: 20 additions & 8 deletions apps/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
import { defineCommand, runMain } from 'citty'
import { consola } from 'consola'

import type { UserBio } from '@weibo-archiver/shared'
import { fetchFollowings, fetchPosts, userDetail } from '@weibo-archiver/shared'
import type { UserBio } from '@weibo-archiver/shared'
import type { Config } from './config'
import { configFile, getConfig, saveConfig } from './config'
import { appendJson, getLastLine, renameFile, saveJson } from './utils'
import {
appendJson,
getLastLine,
renameFile,
saveJson,
updateProgress,
} from './utils'

let config: Config

Expand Down Expand Up @@ -185,15 +191,19 @@ async function getUserMeta() {
const user = await userDetail(uid)
const followings: UserBio[] = []

await fetchFollowings(uid, async (data) => {
consola.info(`TODO:进度条 抓取关注列表:${followings.length}/${user.followings}`)
await fetchFollowings(uid, async (data, total, isMe) => {
updateProgress(
followings.length,
isMe ? total : 200, // 200 是微博的限制
)

followings.push(...data)
})

const userMeta = { user, followings }
await saveJson(savePath, `meta-${uid}.json`, userMeta)

consola.success('抓取完成')
consola.success('\n抓取完成')
}

async function getWeibo() {
Expand All @@ -202,7 +212,7 @@ async function getWeibo() {

const lastId = await getLastLine(savePath, `data-${uid}.json`)
.then((data) => {
return (+data.id || 0) as number
return +data?.id || Number.POSITIVE_INFINITY
})

consola.info('开始抓取微博:', uid, '上次抓取到的 id:', lastId)
Expand All @@ -215,16 +225,18 @@ async function getWeibo() {
if (post.id >= lastId)
return

consola.info(`TODO: 进度条, ${++fetchedCount}/${total}`)
fetchedCount++
config.curPage = Math.ceil(fetchedCount / 20)
config.fetchedCount = fetchedCount

updateProgress(fetchedCount, total)

await appendJson(savePath, `data-${uid}.json`, post)
await saveConfig(config)
},
}),
onFinish: async () => {
consola.success('抓取完成')
consola.success('\n抓取完成')
},
setTotal: (_total) => {
if (!total)
Expand Down
1 change: 1 addition & 0 deletions apps/cli/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './prompt'
export * from './file'
export * from './progress'
23 changes: 23 additions & 0 deletions apps/cli/src/utils/progress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 一个简单的进度条
*/
export function updateProgress(
current: number,
total: number,
) {
const width = 50
const progress = (current / total)
const completed = Math.round(width * progress)
const remaining = width - completed

process.stdout.clearLine(0)
process.stdout.cursorTo(0)

const progressBar = `[${'='.repeat(completed)}>${' '.repeat(remaining)}]`
const percentage = Math.round(progress * 100)

process.stdout.write(`Progress: ${progressBar} ${percentage}% (${current}/${total})`)

if (current === total)
process.stdout.write('\n')
}
5 changes: 5 additions & 0 deletions packages/shared/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Weibo-archiver Shared 部分

这里不包含 vue,是浏览器和Nodejs通用的部分

建议在开发时将 package.json 的 `main` 字段改为 `src/index.ts` 来避免每次更改都要重新 build 才能更新引用
2 changes: 2 additions & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export * from './protocol'
export * from './services'
export * from './types'

export const isBrowser = typeof window !== 'undefined'

export function delay(ms = 2000) {
const randomMs = Math.random() * ms + 1000
return new Promise(resolve => setTimeout(resolve, randomMs))
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/services/postService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
import {
delay,
filterComments,
isBrowser,
parseText,
postsParser,
usePausableLoop,
Expand Down Expand Up @@ -148,7 +149,7 @@ export function fetchPosts(
const { uid, startAt, endAt, hasRepost, curPage, isFetchAll } = fetchOptions()
const page = curPage + 1

console.log(`正在获取第 ${page} 页`)
isBrowser && console.log(`正在获取第 ${page} 页`)

return isFetchAll
? await fetchAllPosts(uid, page)
Expand Down
5 changes: 2 additions & 3 deletions packages/shared/src/services/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,18 @@ export async function isMe(uid: string) {

export async function fetchFollowings(
uid: string,
saveData: (users: UserBio[], total: number) => Promise<void>,
saveData: (users: UserBio[], total: number, isMe: boolean) => Promise<void>,
) {
let page = 1
const _isMe = await isMe(uid)
console.log('isMe', _isMe)

while (true) {
await delay(3000)
const { users, total } = _isMe
? await getMyFollowings(page)
: await getFollowings(uid, page)

await saveData(users, total)
await saveData(users, total, _isMe)

page += 1
if (users.length === 0)
Expand Down

0 comments on commit 7be02d5

Please sign in to comment.