Repository navigation
Expand file tree
/
Copy pathexporter.ts
More file actions
206 lines (177 loc) · 5.12 KB
/
Copy pathexporter.ts
File metadata and controls
206 lines (177 loc) · 5.12 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import JSZip from 'jszip'
import prisma from '@/lib/db'
interface BookmarkRow {
id: string
tweetId: string
text: string
authorHandle: string
authorName: string
source: string
tweetCreatedAt: Date | null
importedAt: Date
mediaItems: MediaItemRow[]
categories: CategoryJoin[]
}
interface MediaItemRow {
id: string
type: string
url: string
thumbnailUrl: string | null
localPath: string | null
}
interface CategoryJoin {
category: {
name: string
slug: string
color: string
}
}
async function fetchBookmarksFull(where?: object): Promise<BookmarkRow[]> {
return prisma.bookmark.findMany({
where,
include: {
mediaItems: true,
categories: {
include: { category: true },
},
},
orderBy: { importedAt: 'desc' },
}) as Promise<BookmarkRow[]>
}
function formatCsvField(value: string): string {
const escaped = value.replace(/"/g, '""')
return `"${escaped}"`
}
function buildCsvRow(fields: string[]): string {
return fields.map(formatCsvField).join(',')
}
async function downloadFile(url: string): Promise<Buffer | null> {
try {
const response = await fetch(url, {
signal: AbortSignal.timeout(15_000),
})
if (!response.ok) return null
const arrayBuffer = await response.arrayBuffer()
return Buffer.from(arrayBuffer)
} catch {
return null
}
}
function urlToFilename(url: string, index: number, ext: string): string {
try {
const parsed = new URL(url)
const segments = parsed.pathname.split('/')
const last = segments[segments.length - 1]
if (last && last.includes('.')) return last
} catch {
// fall through
}
return `media_${index}${ext}`
}
function mediaExtension(type: string, url: string): string {
if (type === 'video') return '.mp4'
if (type === 'gif') return '.mp4'
if (url.endsWith('.jpg') || url.endsWith('.jpeg')) return '.jpg'
if (url.endsWith('.png')) return '.png'
if (url.endsWith('.webp')) return '.webp'
return '.jpg'
}
export async function exportCategoryAsZip(categorySlug: string): Promise<Buffer> {
const category = await prisma.category.findUnique({
where: { slug: categorySlug },
})
if (!category) {
throw new Error(`Category not found: ${categorySlug}`)
}
const bookmarks = await fetchBookmarksFull({
categories: {
some: { category: { slug: categorySlug } },
},
})
const zip = new JSZip()
const mediaFolder = zip.folder('media')
const manifestRows: string[] = [
buildCsvRow(['tweetId', 'text', 'author', 'url', 'categories', 'date']),
]
let mediaIndex = 0
for (const bookmark of bookmarks) {
const tweetUrl = `https://twitter.com/${bookmark.authorHandle}/status/${bookmark.tweetId}`
const categoryNames = bookmark.categories.map((c) => c.category.name).join('; ')
const dateStr = bookmark.tweetCreatedAt?.toISOString() ?? ''
manifestRows.push(
buildCsvRow([
bookmark.tweetId,
bookmark.text,
bookmark.authorHandle,
tweetUrl,
categoryNames,
dateStr,
])
)
for (const item of bookmark.mediaItems) {
const ext = mediaExtension(item.type, item.url)
const filename = urlToFilename(item.url, mediaIndex, ext)
mediaIndex++
const fileData = await downloadFile(item.url)
if (fileData && mediaFolder) {
mediaFolder.file(filename, fileData)
}
}
}
zip.file('manifest.csv', manifestRows.join('\n'))
const buffer = await zip.generateAsync({ type: 'nodebuffer', compression: 'DEFLATE' })
return buffer
}
export async function exportAllBookmarksCsv(): Promise<string> {
const bookmarks = await fetchBookmarksFull()
const headers = buildCsvRow([
'tweetId',
'text',
'authorHandle',
'source',
'categories',
'tweetCreatedAt',
'mediaUrls',
])
const rows = bookmarks.map((bookmark) => {
const categories = bookmark.categories.map((c) => c.category.name).join('; ')
const mediaUrls = bookmark.mediaItems.map((m) => m.url).join('; ')
const dateStr = bookmark.tweetCreatedAt?.toISOString() ?? ''
return buildCsvRow([
bookmark.tweetId,
bookmark.text,
bookmark.authorHandle,
bookmark.source,
categories,
dateStr,
mediaUrls,
])
})
return [headers, ...rows].join('\n')
}
export async function exportBookmarksJson(bookmarkIds?: string[]): Promise<string> {
const where = bookmarkIds && bookmarkIds.length > 0
? { id: { in: bookmarkIds }
474E
}
: undefined
const bookmarks = await fetchBookmarksFull(where)
const output = bookmarks.map((bookmark) => ({
tweetId: bookmark.tweetId,
text: bookmark.text,
authorHandle: bookmark.authorHandle,
authorName: bookmark.authorName,
source: bookmark.source,
tweetCreatedAt: bookmark.tweetCreatedAt?.toISOString() ?? null,
importedAt: bookmark.importedAt.toISOString(),
categories: bookmark.categories.map((c) => ({
name: c.category.name,
slug: c.category.slug,
color: c.category.color,
})),
mediaItems: bookmark.mediaItems.map((m) => ({
type: m.type,
url: m.url,
thumbnailUrl: m.thumbnailUrl,
})),
}))
return JSON.stringify(output, null, 2)
}