forked from tsoernes/leechflix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
222 lines (198 loc) · 5.54 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
'use strict'
var gui = require('nw.gui')
var swig = require('swig')
var async = require('async')
var scrapermain, tlscraper, iptscraper
var initcookie = require('./initcookie').init()
.then(function () {
console.log('loading modules')
scrapermain = require('./scrapers/scrapermain.js')
tlscraper = require('./scrapers/tl.js')
iptscraper = require('./scrapers/ipt.js')
})
.catch(function (e) {
console.log(e)
throw(e)
})
var streamer = require('./streamer.js')
var config = require('./config.js')
var appTemplate = swig.compileFile('./templates/app.html')
var movieLibraryTemplate = swig.compileFile('./templates/movieLibrary.html')
var movieDetails = swig.compileFile('./templates/movieDetails.html')
var downloadProgress = swig.compileFile('./templates/downloadProgress.html')
var currentScraper, currentMovies, currentUrl, currentMainUrl
function start () {
initCore(function () {
currentUrl = config.urls.ipt.moviesNew
setIptScraper()
fetch(currentUrl)
})
}
function initCore (callback) {
async.series([
function (done) {
scrapermain.login(config.urls.tl.login, config.credentials.tl, function (err) {
done(err)
})
},
function (done) {
scrapermain.login(config.urls.ipt.login, config.credentials.ipt, function (err) {
done(err)
})
}
], function (err) {
if (err) console.log(err)
callback()
})
}
function fetch () {
scrapermain.fetch(currentUrl, currentScraper, function (err, res) {
if (err) {
console.log('err' + err)
} else {
currentMovies = res
sendItemsToView()
}
})
}
function play (torrentUrl) {
scrapermain.downloadTorrent(torrentUrl, function (err, path) {
if (err) console.log(err)
else {
streamer.play(path)
//showDownloadingOverlay(movie, release)
}
})
}
function playBak (torrentUrl) {
scrapermain.downloadTorrent(torrentUrl, function (err, path) {
if (err) console.log(err)
else {
streamer.playBak(path)
//showDownloadingOverlay(movie, release)
}
})
}
function sendItemsToView () {
document.body.innerHTML = (appTemplate())
if (currentMovies !== null) {
document.getElementById('movieLibrary_content').innerHTML = (movieLibraryTemplate({movies: currentMovies}))
} else {
document.getElementById('movieLibrary_content').innerHTML = ''
}
initUI()
}
function initUI () {
$('#overlayMovie').easyModal()
}
function getReleases(movie, callback) {
if (movie.isCollection) {
callback(null, movie)
return
}
var url = getSearchUrl(currentMainUrl, movie.title + ' ' + movie.year)
console.log(url)
scrapermain.fetch(url, currentScraper, function (err, res) {
if (err) {
console.log('err' + err)
callback(err, null)
return
} else {
var movieWithReleases = res
if (movieWithReleases.length == 0) {
var err = 'Could not find any releases for ' + movie.title + ' ' + movie.year
callback(err, null)
} else if (movieWithReleases.length > 1) {
console.log('More than 1 movie when searching for releases:')
console.log(movieWithReleases)
function sortByNumReleases (a, b) {
return parseFloat(b.release.length) - parseFloat(a.release.length)
}
movieWithReleases = movieWithReleases.sort(sortByNumReleases)
}
callback(null, movieWithReleases[0])
}
})
}
function showMovieOverlay (position) {
var movie = currentMovies[position]
getReleases(movie, function(err, res) {
if (err) {
console.log(err)
res = movie
}
document.getElementById('overlayMovie').innerHTML = (movieDetails({movie: res}))
$('#overlayMovie').trigger('openModal')
})
}
function showDownloadingOverlay (movie, release) {
document.getElementById('overlayDownloadProgress').innerHTML = (downloadProgress({movie: movie, release: release}))
$('#overlayDownloadProgress').trigger('openModal')
$(function() {
$('#overlayDownloadProgress').easyModal({
autoOpen: true,
overlayOpacity: 0.8,
overlayColor: "#3333FF",
overlayClose: false,
closeOnEscape: true,
closeButtonClass: '.close'
});
});
}
function openLink (link) {
gui.Shell.openExternal(link)
}
/*
Page navigation
*/
function goTlNextPage () {
var index = parseInt(currentUrl.substring(currentUrl.length - 1, currentUrl.length), 10) + 1
currentUrl = currentUrl.substring(0, currentUrl.length - 1) + index.toString()
fetch()
}
function goTlPrevPage () {
var index = parseInt(currentUrl.substring(currentUrl.length - 1, currentUrl.length), 10)
if (index > 1) {
index = index - 1
currentUrl = currentUrl.substring(0, currentUrl.length - 1) + index.toString()
fetch()
}
}
/*
Browse most popular movies added in the given timeframe
*/
function goTlPopPage (term) {
/*
Example valid terms:
24HOURS, 7DAYS, 2MONTHS, 1YEAR (weeks not working..?)
*/
currentUrl = config.urls.tl.moviesPopBeg + term + config.urls.tl.moviesPopEnd
setTlScraper()
fetch()
}
function goTlNewPage () {
currentUrl = config.urls.tl.moviesNew
setTlScraper()
fetch()
}
function getSearchUrl (site, term) {
return site.searchBeg + term.replace(/[^a-zA-Z0-9]/g,'+') + site.searchEnd
}
function goIptNewPage () {
currentUrl = config.urls.ipt.moviesNew
setIptScraper()
fetch()
}
function goIptSeeders() {
currentUrl = config.urls.ipt.moviesNew + config.urls.ipt.sortSeeders
setIptScraper()
fetch()
}
function setTlScraper () {
currentScraper = tlscraper
currentMainUrl = config.urls.tl
}
function setIptScraper () {
currentScraper = iptscraper
currentMainUrl = config.urls.ipt
}