-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
281 lines (239 loc) · 7.62 KB
/
main.js
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
const API_ROOT = localStorage.API_ROOT || 'https://api.zhongzi.io'
;(() => {
const query = queryInHash()
const now = new Date
const loading = window.results.innerHTML
const advises = `超高清 超清 1280P BD 全高清 1080P FHD 高清 720P HD ${now.getFullYear()}`
const format = [ 'mkv', 'avi', 'mp4', 'mpg', 'mpeg', 'rmvb', 'rm', 'webm' ]
const filter = [
{ terms: { extnames: format } },
{ match: { name: advises } },
]
nunjucks.configure({
web: { async: true, useCache: true },
autoescape: true
})
async function refetch() {
window.results.innerHTML = loading
let q = recognizeQ()
document.title = `${q.title} - Zhongzi.io`
ga('set', 'page', `${location.pathname}${location.search}${location.hash}`);
ga('send', 'pageview');
{
const search = {
size: 20,
sort: [
{ _score: 'desc' },
{ discovered_at: 'desc' }
],
query: {
bool: {
filter
}
}
}
if (q.value) {
//search.query.bool.must = [ { match: { name: q.value } } ]
search.query.bool.should = [
{ match : { name: q.value } },
{ match_phrase: { name: q.value } },
{ term : { _id : q.value } }
]
}
const _q = b64EncodeUnicode(JSON.stringify(search))
const response = await fetch(`${API_ROOT}/search/?${Qs.stringify({q: _q})}`)
const results = await response.json()
nunjucks.render('template.html', results, (error, html) => {
window.results.innerHTML = html
})
}
}
function b64EncodeUnicode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
refetch()
window.addEventListener("hashchange", refetch)
})()
function queryInHash() {
return Qs.parse(location.hash.slice(location.hash.indexOf('?') + 1))
}
function recognizeQ() {
const q = {}
q.value = queryInHash().q
if (!q) {
q.value = Qs.parse(location.search.slice(1)).q
}
window.search.value = q.value || ''
q.title = q.value || 'Movie, TV, Anime'
return q
}
const isiOSDevice = navigator.userAgent.match(/ipad|iphone/i);
function magnet(element, event) {
event.preventDefault()
const input = element.querySelector('input')
if (isiOSDevice) {
const editable = input.contentEditable;
const readOnly = input.readOnly;
input.contentEditable = true;
input.readOnly = false;
const range = document.createRange();
range.selectNodeContents(input);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
input.setSelectionRange(0, 1e6);
input.contentEditable = editable;
input.readOnly = readOnly;
//notie.alert({text: 'Please copy to clipboard manually!'})
} else {
input.select();
}
document.execCommand('copy')
notie.alert({text: 'Magnet has been copied to clipboard!'})
}
class Provider {
constructor() {
this.base = 'https://www.premiumize.me/api'
this.pin = '9p9pkpjf3yccyn5i'
this.transfers = new Map
this.transferWatchers = new Set
}
async fetch(endpoint, options) {
options = options || {}
options.query = options.query || {}
options.query.pin = this.pin
const querystring = Qs.stringify(options.query)
if (options.body) {
const form = new FormData
const fields = Object.keys(options.body)
for (const field of fields) {
form.append(field, options.body[field])
}
options.body = form
}
const response = await fetch(
`${this.base}${endpoint}?${querystring}`,
{
method: options.method,
mode: 'cors',
body: options.body
}
)
return await response.json()
}
async syncTransfers() {
const {transfers} = await this.fetch('/transfer/list')
for (const transfer of transfers) {
this.transfers.set(transfer.id, transfer)
}
return transfers
}
async watchTransfers(callback) {
this.transferWatchers.add(callback)
if (this.transferWatchers.size > 1) {
// delegate
return
}
while (this.transferWatchers.size) {
await this.syncTransfers()
for (const callback of this.transferWatchers) {
callback()
}
await new Promise((resolve) => setTimeout(resolve, 1000))
}
}
async watchTransfer(id, callback) {
//await this.syncTransfers()
//const transfer = this.transfers.get(id)
//if (transfer) {
//callback(transfer)
//if ('finished' === transfer.status) {
//return
//}
//}
// add watcher
const watcher = () => {
const transfer = this.transfers.get(id)
if (!transfer) {
return
}
if ('finished' === transfer.status) {
this.transferWatchers.delete(watcher)
}
callback(transfer)
}
this.watchTransfers(watcher)
}
}
const provider = new Provider
// https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
function generateUUID() { // Public Domain/MIT
var d = new Date().getTime();
if (typeof performance !== 'undefined' && typeof performance.now === 'function'){
d += performance.now(); //use high-precision timer if available
}
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
const SESSION_ID = generateUUID()
async function watch(element) {
gtag_report_conversion()
element.innerText = 'Transferring'
element.disabled = true
const progress = element.parentNode.querySelector('[data-role="progress"]')
progress.hidden = false
const progressBar = progress.firstElementChild
const {infohash, magnet, name} = element.parentNode.parentNode.dataset
//const src = magnet.replace(/btih:(.*)&.*$/, 'btih:$1&dn=$1')
const {id: folder_id} = await provider.fetch(
'/folder/create',
{method: 'POST', body: {name: SESSION_ID}}
)
let transfer = await provider.fetch(
'/transfer/create',
{method: 'POST', body: {src: magnet, folder_id}}
)
if ('error' === transfer.status && 'You have reached the maximum of 25 active download jobs. Please wait or abort an old one.' === transfer.message) {
const transfers = await provider.syncTransfers()
const transferToAbort = transfers.reverse().find((transfer) => /. ETA is unknown$/.test(transfer.message))
await provider.fetch('/transfer/delete', {method: 'POST', body: {id: transferToAbort.id}})
transfer = await provider.fetch(
'/transfer/create',
{method: 'POST', body: {src: magnet, folder_id}}
)
}
provider.watchTransfer(transfer.id, (transfer) => {
const percent = (transfer.progress * 100).toFixed(2)
progressBar.style.width = `${percent}%`
element.innerText = `Transferring (${percent}%)...`
if ('finished' !== transfer.status) {
return
}
complete(transfer)
})
async function complete(transfer) {
element.hidden = true
progress.hidden = true
const ul = element.parentNode.querySelector('ul[data-role="video-list"]')
const {content: files} = await provider.fetch(
'/folder/list',
{query: {id: transfer.folder_id}}
)
const videos = files ? files.filter(file => file.stream_link) : []
nunjucks.render('video-list.html', {videos}, (error, html) => {
ul.innerHTML = html
})
ul.hidden = false
provider.fetch('/transfer/clearfinished', {method: 'POST'})
}
}
function submit(event) {
event.preventDefault()
document.activeElement.blur()
location.hash = `#/?q=${encodeURIComponent(search.value)}`
}