forked from jerbear2008/tttakedown-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive.ts
102 lines (97 loc) · 2.8 KB
/
archive.ts
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
import config from './config.json' with { type: 'json' }
const disableArchive = true
const disableArchive2 = true
export async function archiveURL(url: string) {
if (disableArchive2) return console.log('pretend to archive url', url)
const response = await fetch('https://web.archive.org/save', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'Authorization': `LOW ${config.internetArchiveKey}`,
},
body: new URLSearchParams({
url,
}),
})
if (!response.ok) {
throw new Error(
`Failed to archive ${url}: ${response.status} ${response.statusText} ${await response
.text()}`,
)
}
}
export async function archiveYouTubePage(id: string) {
await archiveURL(`https://www.youtube.com/watch?v=${id}&themeRefresh=1`)
}
export async function archiveYouTubeFile(id: string) {
if (disableArchive) return console.log('pretend to archive file', id)
const downloadCommand = new Deno.Command('tubeup', {
args: [
// '--metadata=collection:mirrortube',
`https://www.youtube.com/watch?v=${id}`,
],
// stdout: 'inherit',
stderr: 'inherit',
})
await downloadCommand.output()
}
export async function archiveYouTube(id: string) {
await Promise.all([
archiveYouTubePage(id),
archiveYouTubeFile(id),
])
}
export async function getYouTubeArchives(id: string) {
const metadataResponse = await fetch(
`https://archive.org/metadata/youtube-${id}`,
{
headers: {
'Accept': 'application/json',
'Authorization': `LOW ${config.internetArchiveKey}`,
},
},
)
if (metadataResponse.ok) {
const metadataJson = await metadataResponse.json()
if ('metadata' in metadataJson) {
const { identifier } = metadataJson.metadata as { identifier: string }
return `https://archive.org/details/${identifier}`
}
}
const availibilityUrl = new URL(
'https://archive.org/wayback/available',
)
availibilityUrl.searchParams.set(
'url',
`https://www.youtube.com/watch?v=${id}`,
)
const availibilityResponse = await fetch(
availibilityUrl.href,
{
headers: {
'Accept': 'application/json',
// 'Authorization': `LOW ${config.internetArchiveKey}`,
},
},
)
if (availibilityResponse.ok) {
const availibilityJson = await availibilityResponse.json() as {
url: string
archived_snapshots: Record<string, never> | {
closest: {
status: string
available: boolean
url: string
timestamp: string
}
}
}
if (availibilityJson.archived_snapshots.closest) {
const { url } = availibilityJson.archived_snapshots.closest
return url as string
}
}
// return `https://archive.org/details/youtube-${id}`
return null
}