forked from jaruba/PowderWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
executable file
·169 lines (127 loc) · 3.57 KB
/
Copy pathapi.js
File metadata and controls
executable file
·169 lines (127 loc) · 3.57 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
import 'isomorphic-fetch'
import Frisbee from 'frisbee'
import _ from 'lodash'
import modals from 'utils/modals'
import events from 'utils/events'
import { getParameterByName } from 'utils/misc'
const apiGet = new Frisbee({
baseURI: window.location.origin,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
})
const api = {
frisbee: new Frisbee({
baseURI: window.location.origin,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
}),
get: async (opts, errCb) => {
const isJson = !!opts.json
delete opts.json
const resp = await api.frisbee.get(api.parseUrl(opts, true))
if (resp && resp.err && errCb) {
// fetch errors
if (resp.originalResponse && resp.status == 500 && resp.originalResponse._bodyBlob) {
if (FileReader) {
const reader = new FileReader()
reader.addEventListener('loadend', (e) => {
const text = e.srcElement.result
if (text)
errCb(text)
})
reader.readAsText(resp.originalResponse._bodyBlob)
return false
}
}
}
if (!resp || !resp.body)
return false
if (isJson) {
let parsed = false
try {
parsed = JSON.parse(resp.body)
} catch(e) {}
return parsed
} else
return resp.body
},
parseUrl: (opts, noToken) => {
let params = []
if (!noToken && !opts.token && (localStorage.getItem('token') || getParameterByName('token')))
opts.token = localStorage.getItem('token') || getParameterByName('token')
_.each(opts, (el, ij) => {
if (ij == 'type') return
params.push(ij+'='+encodeURIComponent(el))
})
return '/' + (opts.type || 'actions') + (params.length ? ('?' + params.join('&')) : '')
},
secureToken: () => {
const token = localStorage.getItem('token')
const expiresOn = localStorage.getItem('expiresOn')
if (token && expiresOn > Date.now())
api.frisbee.headers = { ...api.frisbee.headers, authorization: token }
},
addMagnet: async (torrentUrl) => {
const waitForListening = async (utime, hash, cb) => {
let checkReadyInterval = setInterval(async () => {
if (canceledLoading) {
if (checkReadyInterval)
clearInterval(checkReadyInterval)
checkReadyInterval = false
return
}
let pattern = { method: 'isListening', json: true }
if (utime)
pattern.utime = utime
if (hash)
pattern.hash = hash
const parsed = await api.get(pattern)
if (parsed && parsed.value) {
if (checkReadyInterval)
clearInterval(checkReadyInterval)
checkReadyInterval = false
if (canceledLoading)
return
cb(true)
}
}, 500)
}
// cancelAddMagnet()
if (window.torrentDataPage) {
await api.get({ method: 'new', torrent: torrentUrl })
window.location = '/'
return
}
let canceledLoading = false
const didCancel = () => {
events.off('canceledLoading', didCancel)
canceledLoading = true
}
events.on('canceledLoading', didCancel)
modals.open('loading')
const parsed = await api.get({ method: 'new', torrent: torrentUrl, json: true })
if (canceledLoading)
return
if (parsed && parsed.infoHash) {
const selectedId = parsed.infoHash
waitForListening(parsed.utime, false, () => {
modals.open('stream', { infoHash: selectedId })
})
}
},
findSubs: async (torrent, file, cb) => {
const parsed = await api.get({ method: 'getSubs', infohash: torrent.infoHash, id: file.id, json: true })
if (parsed) {
console.log(parsed)
cb && cb(parsed)
} else {
console.log('no subs found')
cb && cb(false)
}
}
}
export default api