forked from vadyur/script.media.aggregator
/
afteractions.py
299 lines (238 loc) · 8.05 KB
/
afteractions.py
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# -*- coding: utf-8 -*-
import log
from log import debug
import os
import urllib
import filesystem
class Runner(object):
def __init__(self, settings, params, playable_item, torrent_info, torrent_path, info_hash):
self.command = settings.script_params.split(u' ')
self.settings = settings
self.params = params
self.torrent_info = torrent_info
self.torrent_path = torrent_path
self.playable_item = playable_item
self.resume_file = filesystem.join(self.settings.torrents_path(), info_hash + '.resume')
debug('-' * 30 + ' Runner ' + '-' * 30)
debug('torrent: ' + self.torrent)
debug('videofile: ' + self.videofile)
debug('relativevideofile: ' + self.relativevideofile)
debug('torrent_source: ' + self.torrent_source)
debug('short_name: ' + self.short_name)
debug('downloaded: ' + str(self.downloaded))
debug('videotype: ' + self.videotype)
if settings.run_script:
self.process_params()
self.run()
if settings.remove_files:
debug('Runner: remove_files')
if filesystem.exists(self.videofile):
filesystem.remove(self.videofile)
if float(self.downloaded) > 99 and self.all_torrent_files_exists():
if settings.move_video and settings.copy_video_path and filesystem.exists(settings.copy_video_path):
self.move_video_files()
if settings.copy_torrent and settings.copy_torrent_path and filesystem.exists(settings.copy_torrent_path):
self.copy_torrent()
def copy_torrent(self):
debug('Runner: copy torrent')
dest_path = filesystem.join(self.settings.copy_torrent_path, filesystem.basename(self.torrent_path))
filesystem.copyfile(self.torrent_path, dest_path)
def move_video_files(self):
debug('Runner: move video')
for file in self.get_relative_torrent_files_list():
dest_path = filesystem.join(self.settings.copy_video_path, file)
if not filesystem.exists(filesystem.dirname(dest_path)):
filesystem.makedirs(filesystem.dirname(dest_path))
src_path = filesystem.join(self.storage_path, file)
if not filesystem.exists(src_path):
continue
if not filesystem.exists(dest_path):
# Move file if no exists
filesystem.movefile(src_path, dest_path)
else:
filesystem.remove(src_path)
self.change_resume_file(self.settings.copy_video_path)
def change_resume_file(self, dest):
if filesystem.exists(self.resume_file):
data = None
with filesystem.fopen(self.resume_file, 'rb') as resume:
from bencode import BTFailure
try:
from bencode import bdecode, bencode
decoded = bdecode(resume.read())
decoded['save_path'] = dest.encode('utf-8')
data = bencode(decoded)
except BTFailure:
pass
if data:
with filesystem.fopen(self.resume_file, 'wb') as resume:
resume.write(data)
def all_torrent_files_exists(self):
from base import TorrentPlayer
tp = TorrentPlayer()
tp.AddTorrent(self.torrent)
data = tp.GetLastTorrentData()
files = data['files']
for item in files:
path = filesystem.join(self.storage_path, data['name'], item['name'])
debug(u'all_torrent_files_exists: ' + path)
if not filesystem.exists(path):
path = filesystem.join(self.settings.copy_video_path, data['name'], item['name'])
debug(u'all_torrent_files_exists: ' + path)
if not filesystem.exists(path):
debug(u'all_torrent_files_exists: not found')
return False
debug(u'all_torrent_files_exists: Ok')
return True
def get_relative_torrent_files_list(self):
from base import TorrentPlayer
tp = TorrentPlayer()
tp.AddTorrent(self.torrent)
data = tp.GetLastTorrentData()
files = data['files']
return [filesystem.join(data['name'], item['name']) for item in files]
@staticmethod
def get_addon_path():
try:
import xbmcaddon
_ADDON_NAME = 'script.media.aggregator'
_addon = xbmcaddon.Addon(id=_ADDON_NAME)
path = _addon.getAddonInfo('path').decode('utf-8')
if path == 'Unavailable':
raise Exception('Not in Kodi')
return path
except BaseException as e:
log.print_tb(e)
return filesystem.getcwd()
@property
def torrent(self):
return self.torrent_path
@property
def storage_path(self):
result = self.settings.storage_path
if result == '':
import xbmc
result = xbmc.translatePath('special://temp').decode('utf-8')
return result
@property
def videofile(self):
return filesystem.join(self.storage_path, self.relativevideofile)
@property
def videotype(self):
base_path = self.settings.base_path().encode('utf-8')
rel_path = urllib.unquote(self.params.get('path', ''))
nfoFilename = urllib.unquote(self.params.get('nfo', ''))
from nforeader import NFOReader
nfoFullPath = NFOReader.make_path(base_path, rel_path, nfoFilename)
if filesystem.exists(nfoFullPath):
with filesystem.fopen(nfoFullPath, 'r') as nfo:
s = nfo.read()
if '<episodedetails>' in s:
return 'episode'
if '<movie>' in s:
return 'movie'
try:
import xbmc
return xbmc.getInfoLabel('ListItem.DBTYPE')
except BaseException as e:
log.print_tb(e)
return ''
@property
def relativevideofile(self):
with filesystem.fopen(self.torrent_path, 'rb') as torr:
data = torr.read()
if data is None:
return self.playable_item['name']
from bencode import BTFailure
try:
from bencode import bdecode
decoded = bdecode(data)
except BTFailure:
debug("Can't decode torrent data (invalid torrent link?)")
return self.playable_item['name']
info = decoded['info']
if 'files' in info:
from base import TorrentPlayer
return filesystem.join(TorrentPlayer.Name(info['name']), self.playable_item['name'])
return self.playable_item['name']
@property
def torrent_source(self):
import urllib
return urllib.unquote(self.params['torrent'])
@property
def short_name(self):
if 'anidub' in self.torrent_source:
return 'anidub'
if 'nnm-club' in self.torrent_source:
return 'nnmclub'
if 'hdclub' in self.torrent_source:
return 'elitehd'
if 'bluebird' in self.torrent_source:
return 'bluebird'
if 'rutor' in self.torrent_source:
return 'rutor'
if 'soap4' in self.torrent_source:
return 'soap4me'
return None
@property
def downloaded(self):
info = self.torrent_info
if info is None:
return 0
try:
return str(round(info['downloaded'] * 100 / info['size']))
except BaseException as e:
log.print_tb(e)
return 0
def process_params(self):
for i, s in enumerate(self.command):
if '%t' in s:
self.command[i] = s.replace('%t', self.torrent)
if '%f' in s:
self.command[i] = s.replace('%f', self.videofile)
if '%F' in s:
self.command[i] = s.replace('%F', self.relativevideofile)
if '%u' in s:
self.command[i] = s.replace('%u', self.torrent_source)
if '%s' in s:
self.command[i] = s.replace('%s', self.short_name)
if '%p' in s:
self.command[i] = s.replace('%p', self.downloaded)
if '%v' in s:
self.command[i] = s.replace('%v', self.videotype)
self.command[i] = self.command[i].encode('utf-8')
def run(self):
debug(self.command)
import subprocess
startupinfo = None
u8runner = None
if os.name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= 1
startupinfo.wShowWindow = 0
u8runner = filesystem.abspath(filesystem.join(Runner.get_addon_path(), 'bin/u8runner.exe')).encode('mbcs')
shell = self.command[0].startswith('@')
if shell:
self.command[0] = self.command[0][1:]
try:
subprocess.call(executable=u8runner, args=self.command, startupinfo=startupinfo, shell=shell)
except OSError, e:
debug(("Can't start %s: %r" % (str(self.command), e)))
except BaseException as e:
log.print_tb(e)
class TestRunner(Runner):
def __init__(self):
pass
def test_resume(tr):
dest = u'/mnt/videocache/фываолдж'
Runner.change_resume_file(tr, dest)
def test_get_relative_torrent_files_list(tr):
l = Runner.get_relative_torrent_files_list(tr)
for f in l:
print f
if __name__ == '__main__':
tr = TestRunner()
tr.resume_file = r'c:\Bin\626bbfbb61755200069486609d66e53146483ebe.resume'
tr.torrent_path = r'c:\Users\vd\AppData\Roaming\Kodi\userdata\addon_data\script.media.aggregator\nnmclub\507983.torrent'
test_resume(tr)
#test_get_relative_torrent_files_list(tr)