-
Notifications
You must be signed in to change notification settings - Fork 1
/
playback.go
171 lines (141 loc) · 3.65 KB
/
playback.go
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
// Copyright 2021 Wayback Archiver. All rights reserved.
// Use of this source code is governed by the GNU GPL v3
// license that can be found in the LICENSE file.
/*
Playback is a toolkit for search webpages archived
to Internet Archive, archive.today, IPFS and beyond.
*/
package playback // import "github.com/wabarc/playback"
import (
"context"
"fmt"
"net/url"
"os"
"github.com/wabarc/archive.is"
"github.com/wabarc/archive.org"
"github.com/wabarc/ghostarchive"
"github.com/wabarc/logger"
"github.com/wabarc/memento"
"github.com/wabarc/proxier"
)
var errNotFound = fmt.Errorf("Not found")
func init() {
debug := os.Getenv("DEBUG")
if debug == "true" || debug == "1" || debug == "on" {
logger.EnableDebug()
}
}
// Collect represents a collections from the time capsules.
type Collect struct {
// Arc string // Archive slot name, see config/config.go
Dst string // Archived destination URL
Src string // Source URL
// Ext string // Extra identifier
}
type IA struct {
URL *url.URL
}
type IS struct {
URL *url.URL
}
// IPFS
type IP struct {
URL *url.URL
}
type PH struct {
URL *url.URL
}
type GA struct {
URL *url.URL
}
// Time Travel, http://timetravel.mementoweb.org
type TT struct {
URL *url.URL
}
// Google Cache
type GC struct {
URL *url.URL
}
// Playbacker is the interface that wraps the basic Playback method.
//
// Playback playback *url.URL from implementations from the Wayback Machine.
// It returns the result of string from the upstream service.
type Playbacker interface {
Playback(ctx context.Context) string
}
func (i IA) Playback(ctx context.Context) string {
arc := &ia.Archiver{}
dst, err := arc.Playback(ctx, i.URL)
if err != nil {
logger.Error("[playback] %s from Internet Archive failed: %v", i.URL.String(), err)
return fmt.Sprint(err)
}
return dst
}
func (i IS) Playback(ctx context.Context) string {
client := proxier.NewClient(nil).Client
arc := is.NewArchiver(client)
dst, err := arc.Playback(ctx, i.URL)
if err != nil {
logger.Error("[playback] %s from archive.today failed: %v", i.URL.String(), err)
return fmt.Sprint(err)
}
return dst
}
func (g GA) Playback(ctx context.Context) string {
arc := &ga.Archiver{}
dst, err := arc.Playback(ctx, g.URL)
if err != nil {
logger.Error("[playback] %s from Ghostarchive failed: %v", g.URL.String(), err)
return fmt.Sprint(err)
}
return dst
}
func (i IP) Playback(ctx context.Context) string {
dst, err := newGitHub().extract(ctx, i.URL, "ipfs")
if err != nil && err != errNotFound {
logger.Error("[playback] %s from IPFS failed: %v", i.URL.String(), err)
return fmt.Sprint(err)
}
if dst == "" {
dst, err = newMeili().extract(ctx, i.URL, "ipfs")
}
if err != nil {
return fmt.Sprint(err)
}
return dst
}
func (i PH) Playback(ctx context.Context) string {
dst, err := newGitHub().extract(ctx, i.URL, "telegraph")
if err != nil && err != errNotFound {
logger.Error("[playback] %s from Telegra.ph failed: %v", i.URL.String(), err)
return fmt.Sprint(err)
}
if dst == "" {
dst, err = newMeili().extract(ctx, i.URL, "telegraph")
}
if err != nil {
return fmt.Sprint(err)
}
return dst
}
func (i TT) Playback(ctx context.Context) string {
arc := &memento.Memento{}
dst, err := arc.Mementos(ctx, i.URL)
if err != nil {
logger.Error("[playback] %s from Time Travel failed: %v", i.URL.String(), err)
return fmt.Sprint(err)
}
return dst
}
func (i GC) Playback(ctx context.Context) string {
dst, err := newGoogle().cache(ctx, i.URL)
if err != nil {
logger.Error("[playback] %s from Google Cache failed: %v", i.URL.String(), err)
return fmt.Sprint(err)
}
return dst
}
func Playback(ctx context.Context, p Playbacker) string {
return p.Playback(ctx)
}