Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 移出导出数据中的 post.user #43

Merged
merged 4 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/beta-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
- '**.md'
- '.github/**'
- '!.github/workflows/**'
pull_request:

jobs:
install-and-build:
Expand Down
34 changes: 5 additions & 29 deletions apps/monkey/src/Ctrl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,18 @@ const { config } = storeToRefs(configStore)
const percentage = computed(() => config.value.fetchedCount / postStore.total * 100)
const progressText = computed(() => () => `${config.value.fetchedCount}/${postStore.total} 条`)

/**
* 保存用户信息,以在预览页中获取这些信息
*/
async function saveUserInfo() {
const user = await userDetail(config.value.uid)
user.exportedAt = new Date().toLocaleString()

const users = GM_getValue<any[]>('users') ?? []
const index = users.findIndex((u: any) => u.uid === user.uid)
if (index !== -1)
Object.assign(users[index], user)
else
users.push(user)

GM_setValue('users', users)

console.log('已同步的用户信息', users)
message.success('用户信息同步成功')
}

/**
* 导出数据
*/
async function exportDatas() {
const posts = await postStore.getAll()
console.log('导出的数量:', posts.length)

const res = await exportData(posts)
const res = await exportData(posts, postStore.userInfo)
if (!res)
return
const scripts = 'https://github.com/Chilfish/Weibo-archiver/raw/monkey/scripts.zip'
saveAs(scripts, 'scripts.zip')

await saveUserInfo()
}

const { pause, start } = fetchPosts({
Expand Down Expand Up @@ -83,6 +61,8 @@ async function startFetch() {
if (!config.value.restore || !config.value.isFetchAll)
await postStore.reset()

await postStore.setUser()

isStart.value = true
isFinish.value = false
isStop.value = false
Expand All @@ -98,6 +78,8 @@ async function init() {
const id = document.URL.match(/\/(\d+)/)?.[1] ?? ''
const username = document.URL.match(/\/n\/(.+)/)?.[1] ?? ''
const { uid, name } = await userInfo({ id, name: decodeURIComponent(username) })

postStore.userInfo = await userDetail(uid)
configStore.setConfig({ uid, name })
}

Expand Down Expand Up @@ -182,12 +164,6 @@ function toggleStop() {
>
导出
</button>

<button
@click="saveUserInfo"
>
同步信息
</button>
</div>
</div>

Expand Down
8 changes: 4 additions & 4 deletions apps/monkey/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ if (document.location.hostname === 'weibo.com') {
initApp()
console.log('weibo-archiver 加载成功')
}
else {
const users = GM_getValue('users') || []
// else {
// const users = GM_getValue('users') || []

localStorage.setItem('users', JSON.stringify(users))
}
// localStorage.setItem('users', JSON.stringify(users))
// }
17 changes: 15 additions & 2 deletions apps/monkey/src/stores/postStore.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { defineStore, storeToRefs } from 'pinia'
import type { Post, UID } from '@types'
import type { Post, UID, UserInfo } from '@types'
import { EmptyIDB, IDB } from '@core/utils/storage'
import { useConfigStore } from './configStore'

export const usePostStore = defineStore('post', () => {
/* 获取到的所有帖子,但会卡内存 */
// const posts = shallowRef([] as Post[])

const userInfo = ref<UserInfo | null>(null)

const configStore = useConfigStore()
const { config } = storeToRefs(configStore)

Expand Down Expand Up @@ -58,7 +60,7 @@ export const usePostStore = defineStore('post', () => {
await idb.value.addDBPost(newPost)
config.value.fetchedCount += 1

config.value.curPage = Math.floor(config.value.fetchedCount / pageSize.value)
config.value.curPage = Math.ceil(config.value.fetchedCount / 20)
}

async function getAll() {
Expand All @@ -73,14 +75,25 @@ export const usePostStore = defineStore('post', () => {
configStore.setConfig({ fetchedCount: count })
}

async function setUser() {
if (!userInfo.value)
return
await waitIDB()

const user = toRaw(userInfo.value)
await idb.value.setUserInfo(user)
}

return {
total,
pageSize,
userInfo,

setDB,
add,
reset,
getAll,
setCount,
setUser,
}
})
2 changes: 1 addition & 1 deletion apps/monkey/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default defineConfig({
match: [
'https://weibo.com/u/*',
'https://weibo.com/n/*',
'https://weibo.chilfish.top/*',
// 'https://weibo.chilfish.top/*',
],
grant: [
'GM_setValue',
Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ useHead({
})

const loaded = ref(false)
onNuxtReady(() => {
onNuxtReady(async () => {
const publicStore = usePublicStore()
const users = localStorage.getItem('users')
const curUid = localStorage.getItem('curUid')

publicStore.users = JSON.parse(users || '[]')
publicStore.curUid = curUid || ''

await publicStore.migrateUser()

loaded.value = true
})
</script>
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/stores/post.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineStore } from 'pinia'
import { useRoute, useRouter } from 'vue-router'
import type { Post, UID } from '@types'
import type { Post, UID, UserInfo } from '@types'
import { EmptyIDB, IDB } from '../utils/storage'

export const usePostStore = defineStore('post', () => {
Expand Down Expand Up @@ -71,17 +71,17 @@ export const usePostStore = defineStore('post', () => {
*/
async function set(
data: Post[],
user: UserInfo,
isReplace = false,
) {
if (!data[0]?.user)
throw new Error('数据格式错误,可能要重新导入')

await waitIDB()

const { count, search } = await idb.value.addDBPosts(data, isReplace)
totalDB.value = count
total.value = count
seachFn.value = search

await idb.value.setUserInfo(user)
}

async function _searchPost(
Expand Down
25 changes: 24 additions & 1 deletion packages/core/src/stores/public.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineStore } from 'pinia'
import type { UserInfo } from '@types'
import type { UID, UserInfo } from '@types'
import { IDB } from '../utils/storage'

export const usePublicStore = defineStore('public', () => {
const globalImg = ref('')
Expand Down Expand Up @@ -29,6 +30,26 @@ export const usePublicStore = defineStore('public', () => {
curUid.value = users.value[0]?.uid || ''
}

function importUser(user: UserInfo) {
curUid.value = user.uid
addUser(user)
}

/**
* 从旧版中迁移 user 数据到 idb 中
*/
async function migrateUser() {
// if (DB_VERSION > 2)
// return

users.value.forEach(async (user) => {
const dbName = `uid-${user.uid}` as UID
const idb = new IDB(dbName)
await idb.setUserInfo(toRaw(user))
return await idb.close()
})
}

return {
globalImg,
users,
Expand All @@ -37,5 +58,7 @@ export const usePublicStore = defineStore('public', () => {
otherUsers,
addUser,
rmUser,
migrateUser,
importUser,
}
})
20 changes: 14 additions & 6 deletions packages/core/src/utils/export.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import fileSaver from 'file-saver'
import type { Post } from '@types'
import type { Post, UserInfo } from '@types'
import { imgsParser } from './parse'

export async function exportData(posts: Post[]) {
if (!posts[0]) {
export async function exportData(
posts: Post[],
userInfo?: UserInfo | null,
) {
if (!posts[0] || !userInfo?.name) {
window.$message.warning('没有数据可以导出')
return false
}

const name = posts[0].user.screen_name || ''
const { name } = userInfo

try {
const data = JSON.stringify(posts)
const data = {
user: userInfo,
weibo: posts,
}

const dataStr = JSON.stringify(data)
const imgsData = Array
.from(imgsParser(posts))
.join(',\n') // csv 格式

const dataBlob = new Blob([data], { type: 'application/json' })
const dataBlob = new Blob([dataStr], { type: 'application/json' })
const imgsDataBlob = new Blob([imgsData], { type: 'text/csv' })

window.$message.success('导出成功,正在下载数据...请允许浏览器批量下载文件', {
Expand Down
59 changes: 35 additions & 24 deletions packages/core/src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type {
CardInfo,
Comment,
FetchOptions,
ParseResult,
PicInfo,
Post,
} from '@types'
Expand Down Expand Up @@ -122,9 +121,9 @@ export function filterComments(
img,
created_at: comment.created_at,
user: {
id: comment.user?.idstr,
screen_name: comment.user?.screen_name,
profile_image_url: comment.user?.profile_image_url,
uid: comment.user?.idstr || comment.user?.id,
name: comment.user?.screen_name,
avatar: comment.user?.profile_image_url,
},
region_name: comment.source,
like_count: comment.like_counts,
Expand All @@ -145,6 +144,30 @@ export function filterComments(
})
}

/**
* 将数据转换为 Post 类型
*/
export function parseOldPost(
post: any,
): Post {
const repost = post.retweeted_status

if (repost) {
repost.user = {
uid: repost.user.id,
name: repost.user.screen_name,
avatar: repost.user.profile_image_url,
}
}

return {
...post,
user: undefined,
retweeted_status: repost,
comments: filterComments(post.comments),
}
}

/**
* 从返回的 api 中提取信息
* @param isRepost 是否是转发,用在递归判断中
Expand Down Expand Up @@ -180,11 +203,13 @@ export async function postFilter(
comments_count: post.comments_count,
like_count: post.attitudes_count,
created_at: post.created_at,
user: {
id: uid,
screen_name: post.user?.screen_name,
profile_image_url: post.user?.profile_image_url,
},
user: isRepost
? {
uid,
name: post.user?.screen_name,
avatar: post.user?.profile_image_url,
}
: undefined,
source: post.source,
region_name: post.region_name,
mblogid: post.mblogid,
Expand Down Expand Up @@ -222,7 +247,7 @@ export async function postsParser(
posts.map(post => queue.add(async () => {
await delay(3000)
const res = await postFilter(post, options)
if (res && options.uid === res.user?.id && options.savePost)
if (res && options.savePost)
await options.savePost(res)
return res
})),
Expand All @@ -240,7 +265,6 @@ export function imgsParser(posts: Post[]): Set<string> {
post.imgs,
post.retweeted_status?.imgs,
post.comments.map(e => e.img),
post.user?.profile_image_url,
post.card?.img,
textImg,
]
Expand All @@ -252,16 +276,3 @@ export function imgsParser(posts: Post[]): Set<string> {

return new Set(imgs)
}

export async function parsedData(
posts: Post[],
options: FetchOptions,
): Promise<ParseResult> {
const parsedPosts = await postsParser(posts, options, false)
const imgs = imgsParser(parsedPosts)

return {
posts: parsedPosts,
imgs,
}
}
Loading