-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic_normal.py
More file actions
634 lines (603 loc) · 33.5 KB
/
Copy pathlogic_normal.py
File metadata and controls
634 lines (603 loc) · 33.5 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# -*- coding: utf-8 -*-
#########################################################
# python
import os
import sys
import datetime
import traceback
import threading
import re
import subprocess
import shutil
import json
import ast
import time
from framework import py_urllib
import rclone
#import platform
#import daum_tv
# third-party
from sqlalchemy import desc
from sqlalchemy import or_, and_, func, not_
from guessit import guessit
# sjva 공용
from framework import app, db, scheduler, path_app_root, celery, py_unicode
from framework.job import Job
from framework.util import Util
from system.model import ModelSetting as SystemModelSetting
from framework.logger import get_logger
# 패키지
from .plugin import logger, package_name
from .model import ModelSetting, ModelItem
import subprocess
from metadata.logic_ftv import LogicFtv
from lib_metadata.site_tmdb import SiteTmdbFtv
from lib_metadata.site_watcha import SiteWatchaTv
#########################################################
class LogicNormal(object):
@staticmethod
@celery.task
def scheduler_function():
try:
logger.debug("해외TV 파일 처리 시작!")
source_base_path = ModelSetting.get('source_base_path')
sub_o_path = ModelSetting.get('sub_o_path')
uhd_base_path = ModelSetting.get('uhd_base_path')
error_path = ModelSetting.get('error_path')
source_base_path = [ x.strip() for x in source_base_path.split(',') ]
if not source_base_path:
return None
logger.debug("경로를 확인하세요")
if None == '':
return None
try:
for source_path in source_base_path:
if ModelSetting.get_bool('use_smi_to_srt'):
try:
import smi2srt
# if app.config['config']['use_celery']:
# result = smi2srt.Logic.start_by_path.apply_async((source_path,))
# result.get()
# else:
logger.debug("smi2srt 진행중")
smi2srt.Logic.start_by_path(work_path=source_path)
logger.debug("파일처리 시작")
LogicNormal.check_move_list(source_path, sub_o_path, error_path)
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
else:
LogicNormal.check_move_list(source_path, sub_o_path, error_path)
if ModelSetting.get_bool('match_sub_flag'):
LogicNormal.match_sub(source_path)
if ModelSetting.get_bool('emptyFolderDelete'):
LogicNormal.empty_folder_remove(source_path)
if ModelSetting.get_bool('sub_x_Delete'):
LogicNormal.empty_folder_remove(os.path.join(error_path,'sub_x'))
else:
logger.debug('해외 TV 파일처리 종료')
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
@staticmethod
def load_videos(source_path):
extradelete = ModelSetting.get('extradelete')
extdelete = ModelSetting.get('extdelete')
file_list = []
delete_lists = [ x.strip().lower() for x in extradelete.split(',') ]
delete_ext = [x.strip().lower() for x in extdelete.split(',')]
for (path, dir, files) in os.walk(source_path.strip(), topdown=False):
for filename in files:
video_exts = ['.mp4', '.mkv', '.avi', '.wmv', '.flv']
if os.path.isdir(source_path): pass
if os.path.splitext(filename.lower())[1] in video_exts:
file_list.append(os.path.join(path, filename))
elif filename.lower() in delete_lists:
try:
os.remove(os.path.join(path,filename))
except:
logger.debug('efr - FAILED:%s', os.path.join(path,filename))
pass
elif os.path.splitext(filename)[1].lower() in delete_ext:
try:
os.remove(os.path.join(path,filename))
except:
logger.debug('efr - FAILED:%s', os.path.join(path,deletefile))
pass
pass
return file_list
@staticmethod
def item_list(path, f):
try:
item = {}
item['path'] = path
item['name'] = os.path.basename(f)
item['fullPath'] = f
try:
item['year'] = guessit(item['fullPath'])['year']
if item['year'] < 1950 or item['year'] > datetime.date.today().year + 1:
item['year'] = None
except:
item['year'] = None
item['folder_title'] = guessit(item['fullPath'])['title']
temp = re.sub('시즌', 's', item['name'])
item['guessit'] = guessit(temp)
try:
match = re.match('\d{4}',item['name'])
if match:
item['guessit']['title'] = match.group()
except:
try:
item['guessit']['title'] = temp[0:temp.find(re.search('[sS]\d\d[eE]',temp).group())].replace('.', ' ').strip()
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
item['ext'] = os.path.splitext(f)[1].lower()
item['search_name'] = None
item['uhd'] = 0
return item
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
@staticmethod
def search(item):
try:
logger.debug('파일이름: %s', item['name'])
en_title = item['guessit']['title']
year = item['year']
logger.debug('검색어: %s', en_title)
if LogicFtv.search((), en_title, year) == []:
if LogicNormal.isHangul(en_title) > 0 :
logger.debug('검색 실패, 추가 검색 중')
kor_title = re.sub('[^가-힣]', ' ', en_title).strip()
if LogicFtv.search((), kor_title, year) != []:
logger.debug('검색성공. 검색어: %s', kor_title)
en_title = kor_title
elif LogicFtv.search((), kor_title, year) == []:
ori_title = re.sub('[ㄱ-ㅎ가-힣]', '', en_title).strip()
if LogicFtv.search((), ori_title, year) != []:
logger.debug('검색성공. 검색어: %s', ori_title)
en_title = ori_title
if LogicFtv.search((), en_title, year) == []:
logger.debug('검색 실패. 폴더까지 확대 탐색')
en_title = item['folder_title']
if LogicFtv.search((), en_title, year) != []:
logger.debug('검색성공. 검색어: %s', en_title)
if LogicFtv.search((), en_title, year) == []:
if LogicNormal.isHangul(en_title) > 0 :
kor_title = re.sub('[^가-힣]', ' ', en_title).strip()
if LogicFtv.search((), kor_title, year) != []:
logger.debug('검색성공. 검색어: %s', kor_title)
en_title = kor_title
elif LogicFtv.search((), kor_title, year) == []:
ori_title = re.sub('[ㄱ-ㅎ가-힣]', '', en_title).strip()
if LogicFtv.search((), ori_title, year) != []:
logger.debug('검색성공. 검색어: %s', ori_title)
en_title = ori_title
else:
tmdb = None
logger.debug('검색 실패')
else:
tmdb = None
logger.debug('검색 실패')
if LogicFtv.search((), en_title, year) != []:
if year is not None:
logger.debug('연도: %s', year)
tmdb_code = LogicFtv.search((), en_title)[0]['code'].strip()
i = 0
while 0<= i < len(LogicFtv.search((), en_title, year)):
if abs(int(LogicFtv.search((), en_title, year)[i]['year']) - year) < 2:
logger.debug('TMDB year = %s', LogicFtv.search((), en_title, year)[i]['year'])
tmdb_code = LogicFtv.search((), en_title, year)[i]['code'].strip()
break
else:
i = i + 1
tmdb = SiteTmdbFtv.info(tmdb_code)
else:
tmdb_code = LogicFtv.search((), en_title)[0]['code'].strip()
tmdb = SiteTmdbFtv.info(tmdb_code)
watcha = SiteWatchaTv.search(tmdb['data']['title'], tmdb['data']['year'])
if not LogicNormal.isHangul(tmdb['data']['title']) > 0 :
logger.debug('TMDB 한글제목 아님. 추가 검색')
try:
if watcha['ret'] == 'empty':
logger.debug('검색 실패')
pass
else:
i = 0
while 0<= i <= len(watcha['data']) - 1:
try:
t_watcha = watcha['data'][i]['title_en'].lower().strip()
except:
t_watcha = 'no_title_en'
t_tmdb = tmdb['data']['title'].lower().strip()
if t_watcha == t_tmdb and abs(int(watcha['data'][i]['year']) - int(tmdb['data']['year'])) < 2 and LogicNormal.isHangul(watcha['data'][i]['title']) > 0 :
logger.debug('한글 제목 매칭 성공')
tmdb['data']['title'] = watcha['data'][i]['title']
break
else:
i = i + 1
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
logger.debug('폴더명: %s', tmdb["data"]["title"])
if tmdb['data']['country'] == []:
logger.debug('TMDB 국가 정보 없음')
if watcha['ret'] == 'empty':
tmdb['data']['country'] == None
if watcha['ret'] == 'success':
i = 0
while 0<= i < len(watcha['data']):
if watcha['data'][i]['title'] == tmdb['data']['title'] and abs(int(watcha['data'][i]['year']) - int(tmdb['data']['year'])) < 2:
if not watcha['data'][i]['country'] == []:
logger.debug('왓챠 국가정보 있음')
tmdb['data']['country'] == watcha['data'][i]['country']
else:
try:
logger.debug('왓챠 메인 국가 정보 없음, 시리즈에서 국가 정보 검색')
tmdb['data']['country'].insert(0, watcha['data'][i]['seasons'][0]['info']['country'][0])
logger.debug('국가정보: %s', tmdb['data']['country'][0])
except:
pass
break
else:
i = i + 1
return tmdb
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
@staticmethod
def set_ftv(data, ftv):
interval = ModelSetting.get('interval')
data['ftv'] = ftv
tmp = ""
try:
if ftv is not None:
data['dest_folder_name'] = '%s' % (re.sub('[\\/:*?"<>|]', '', ftv['data']['title']).replace(' ', ' '))
folder_rule = ModelSetting.get_setting_value('folder_rule')
tmp = folder_rule.replace('%TITLE%', ftv['data']['title']).replace('%YEAR%', str(ftv['data']['year'])).replace('%ORIGINALTITLE%', ftv['data']['originaltitle'])
tmp = re.sub('[\\/:*?\"<>|]', '', tmp).replace(' ', ' ').replace('[]', '')
data['dest_folder_name'] = tmp
time.sleep(int(interval))
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
def check_kor_sub(data, tmdb, sub_o_path):
logger.debug('자막 체크 시작!')
try:
if re.search('korsub',data['name'].lower()):
logger.debug('VOD 파일')
return True
try:
if data['guessit']['release_group'] in ['SW', 'ST', 'SAP', 'SWWV', 'SWWC', 'SWSZ']:
logger.debug('VOD 파일')
return True
except:
pass
if os.path.isfile(os.path.splitext(data['fullPath'])[0]+'.ko.srt'):
logger.debug('외부 자막 있음')
LogicNormal.file_move(os.path.splitext(data['fullPath'])[0]+'.ko.srt',data['dest'],os.path.splitext(data['name'])[0]+'.ko.srt')
return True
if os.path.isfile(os.path.splitext(data['fullPath'])[0]+'.smi'):
logger.debug('외부 자막 있음')
LogicNormal.file_move(os.path.splitext(data['fullPath'])[0]+'.smi',data['dest'],os.path.splitext(data['name'])[0]+'.smi')
return True
if os.path.isfile(os.path.splitext(data['fullPath'])[0]+'.srt'):
logger.debug('외부 자막 있음')
LogicNormal.file_move(os.path.splitext(data['fullPath'])[0]+'.srt',data['dest'],os.path.splitext(data['name'])[0]+'.srt')
return True
else:
logger.debug('외부 자막 없음')
try:
command = ['ffprobe', '-loglevel', 'error', '-select_streams', 's', '-show_entries', 'stream_tags=language', '-of', 'csv=p=0', data['fullPath']]
output = subprocess.Popen(command, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, encoding='utf-8')
ffprobe_json = json.dumps(output.communicate())
if 'kor' in ffprobe_json:
logger.debug('내장 한글 자막 있음')
return True
else:
logger.debug('한글 자막 없음')
return False
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
@staticmethod
def file_move(fullPath, dest, f):
try:
if os.path.exists(dest):
if os.path.exists(os.path.join(dest, f)):
logger.debug('타겟 파일: %s', os.path.exists(os.path.join(dest, f)))
logger.debug('같은 파일 있음')
if os.path.getsize(fullPath) == os.path.getsize(os.path.join(dest,f)):
logger.debug('대상 %s 사이즈가 같아 그냥 삭제', f)
os.remove(fullPath)
else:
os.path.getsize(fullPath) != os.path.getsize(os.path.join(dest,f))
logger.debug('대상 %s 사이즈가 달라 기존 파일 삭제', f)
os.remove(os.path.join(dest,f))
shutil.move(fullPath,os.path.join(dest,f))
if not os.path.isfile(os.path.join(dest,f)):
shutil.move(fullPath, os.path.join(dest, f))
if not os.path.exists(dest):
os.makedirs(dest)
if not os.path.isfile(os.path.join(dest,f)):
shutil.move(fullPath, os.path.join(dest, f))
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
@staticmethod
def genre_move(data, tmdb, sub_o_path):
kids_base_path = ModelSetting.get('kids_base_path')
ani_base_path = ModelSetting.get('ani_base_path')
real_base_path = ModelSetting.get('real_base_path')
doc_base_path = ModelSetting.get('doc_base_path')
error_path = ModelSetting.get('error_path')
try:
if str(data['guessit']['season']) == str(0):
season = 'Specials'
else:
season = 'Season '+str(data['guessit']['season'])
except:
season = 'Season '+str(1)
try:
release_group = data['guessit']['release_group']
except:
data['guessit']['release_group'] = 'no_release'
release_group = 'no_release'
if tmdb is not None:
try:
if ModelSetting.get_bool('kids_flag') and 'Kids' in tmdb['data']['genre'] :
logger.debug('장르: Kids')
try:
data['dest']=os.path.join(kids_base_path,data['dest_folder_name'],season)
if LogicNormal.check_kor_sub(data, tmdb, sub_o_path):
logger.debug('이동경로:%s', data['dest'])
LogicNormal.file_move(data['fullPath'],data['dest'],data['name'])
LogicNormal.db_save(data, data['dest'], 'sub_o', True)
else:
data['dest']=os.path.join(error_path,'sub_x',data['dest_folder_name'],season)
LogicNormal.file_move(data['fullPath'],data['dest'],data['name'])
LogicNormal.db_save(data, data['dest'], 'sub_x', True)
except Exception as e:
logger.error('Exxception:%s', e)
logger.error(traceback.format_exc())
elif ModelSetting.get_bool('ani_flag') and '애니메이션' in tmdb['data']['genre'] :
logger.debug('애니메이션 파일')
try:
data['dest']=os.path.join(ani_base_path,data['dest_folder_name'],season)
if LogicNormal.check_kor_sub(data, tmdb, sub_o_path):
logger.debug('이동경로:%s', data['dest'])
LogicNormal.file_move(data['fullPath'],data['dest'],data['name'])
LogicNormal.db_save(data, data['dest'], 'sub_o', True)
else:
data['dest']=os.path.join(error_path,'sub_x',data['dest_folder_name'],season)
LogicNormal.file_move(data['fullPath'],data['dest'],data['name'])
LogicNormal.db_save(data, data['dest'], 'sub_x', True)
except Exception as e:
logger.error('Exxception:%s', e)
logger.error(traceback.format_exc())
elif ModelSetting.get_bool('real_flag') and u'리얼리티' in tmdb['data']['genre'] :
logger.debug('장르: 리얼리티')
try:
data['dest']=os.path.join(real_base_path,data['dest_folder_name'],season)
if LogicNormal.check_kor_sub(data, tmdb, sub_o_path):
logger.debug('이동경로:%s', data['dest'])
LogicNormal.file_move(data['fullPath'],data['dest'],data['name'])
LogicNormal.db_save(data, data['dest'], 'sub_o', True)
else:
data['dest']=os.path.join(error_path,'sub_x',data['dest_folder_name'],season)
LogicNormal.file_move(data['fullPath'],data['dest'],data['name'])
LogicNormal.db_save(data, data['dest'], 'sub_x', True)
except Exception as e:
logger.error('Exxception:%s', e)
logger.error(traceback.format_exc())
elif ModelSetting.get_bool('doc_flag') and u'다큐멘터리' in tmdb['data']['genre']:
logger.debug('장르: 다큐멘터리')
try:
data['dest']=os.path.join(doc_base_path,data['dest_folder_name'],season)
if LogicNormal.check_kor_sub(data, tmdb, sub_o_path):
logger.debug('이동경로:%s', data['dest'])
LogicNormal.file_move(data['fullPath'],data['dest'],data['name'])
LogicNormal.db_save(data, data['dest'], 'sub_o', True)
else:
data['dest']=os.path.join(error_path,'sub_x',data['dest_folder_name'],season)
LogicNormal.file_move(data['fullPath'],data['dest'],data['name'])
LogicNormal.db_save(data, data['dest'], 'sub_x', True)
except Exception as e:
logger.error('Exxception:%s', e)
logger.error(traceback.format_exc())
else:
LogicNormal.move_ftv(data, tmdb, sub_o_path)
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
if tmdb is None:
data['dest']=os.path.join(error_path,'no_meta',data['guessit']['title'],season)
LogicNormal.file_move(data['fullPath'],data['dest'],data['name'])
LogicNormal.db_save(data, data['dest'], '불일치', True)
@staticmethod
def move_ftv(data, tmdb, sub_o_path):
try:
sub_o_path = ModelSetting.get('sub_o_path')
uhd_base_path = ModelSetting.get('uhd_base_path')
error_path = ModelSetting.get('error_path')
try:
if str(data['guessit']['season']) == str(0):
season = 'Specials'
else:
season = 'Season '+str(data['guessit']['season'])
except:
season = 'Season '+str(1)
try:
release_group = data['guessit']['release_group']
except:
release_group = 'no_release'
command = ['ffprobe', '-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=width', '-of', 'csv=s=x:p=0', data['fullPath']]
output = subprocess.Popen(command, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, encoding='utf-8')
ffprobe_json = json.dumps(output.communicate())
try:
resol = int(ffprobe_json[2:ffprobe_json.find('\\n')])
except:
resol = 1920
if (ModelSetting.get_bool('uhd_flag')) and (os.path.splitext(data['name'])[1] in ['.mkv', '.mp4']) and (resol > 3000) :
logger.debug('UHD 파일')
try:
cu_user_list = ast.literal_eval(ModelSetting.get_setting_value('uhd_country_option').strip())
c = tmdb['data']['country'][0]
if c in cu_user_list.keys():
cu = cu_user_list[c]
else:
cu = ModelSetting.get_setting_value('etc_uhd_country').strip()
except:
cu = ModelSetting.get_setting_value('etc_uhd_country').strip()
data['dest']=os.path.join(uhd_base_path,cu,data['dest_folder_name'],season)
if LogicNormal.check_kor_sub(data, tmdb, sub_o_path):
logger.debug('이동경로:%s', data['dest'])
LogicNormal.file_move(data['fullPath'],data['dest'],data['name'])
LogicNormal.db_save(data, data['dest'], 'sub_o', True)
else:
data['dest']=os.path.join(error_path,'sub_x','UHD',data['dest_folder_name'],season)
LogicNormal.file_move(data['fullPath'],data['dest'],data['name'])
LogicNormal.db_save(data, data['dest'], 'sub_x', True)
else:
try:
cg_user_list = ast.literal_eval(ModelSetting.get_setting_value('ftv_country_option').strip())
c = tmdb['data']['country'][0]
if c in cg_user_list.keys():
cg = cg_user_list[c]
else:
cg = ModelSetting.get_setting_value('etc_ftv_country').strip()
except:
cg = ModelSetting.get_setting_value('etc_ftv_country').strip()
data['dest']=os.path.join(sub_o_path,cg,data['dest_folder_name'],season)
if LogicNormal.check_kor_sub(data, tmdb, sub_o_path):
logger.debug('이동경로:%s', data['dest'])
LogicNormal.file_move(data['fullPath'],data['dest'],data['name'])
LogicNormal.db_save(data, data['dest'], 'sub_o', True)
else:
data['dest']=os.path.join(error_path,'sub_x',data['dest_folder_name'],season)
LogicNormal.file_move(data['fullPath'],data['dest'],data['name'])
LogicNormal.db_save(data, data['dest'], 'sub_x', True)
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
@staticmethod
def load_subs(source_path):
sub_list = []
for (path, dir, files) in os.walk(source_path.strip(), topdown=False):
for subfile in files:
sub_exts = ['.srt']
if os.path.isdir(source_path): pass
if os.path.splitext(subfile.lower())[1] in sub_exts:
sub_list.append(os.path.join(path, subfile))
return sub_list
@staticmethod
def match_sub(source_path):
source_path = LogicNormal.load_subs(source_path)
error_path = ModelSetting.get('error_path')
if source_path != []:
logger.debug("자막에 맞는 동영상 파일 불러오는 중")
for s in source_path:
spath = os.path.dirname(s)
item = LogicNormal.item_list(spath, s)
tmdb = LogicNormal.search(item)
LogicNormal.set_ftv(item, tmdb)
try:
if str(item['guessit']['season']) == str(0):
season = 'Specials'
else:
season = 'Season '+str(item['guessit']['season'])
except:
season = 'Season '+str(1)
video_path = os.path.join(error_path,'sub_x',item['dest_folder_name'],season)
logger.debug('탐색경로: %s', video_path)
try:
if os.path.exists(video_path):
for (path, dir, files) in os.walk(video_path, topdown=False):
for v in files:
try:
if item['guessit']['episode'] == guessit(v)['episode'] and item['guessit']['release_group'].lower() == guessit(v)['release_group'].lower() and item['guessit']['streaming_service'].lower() == guessit(v)['streaming_service'].lower() :
shutil.move(os.path.join(path,v),os.path.join(spath,v))
os.rename(s,os.path.join(spath,os.path.splitext(v)[0]+'.ko.srt'))
logger.debug("불러오기 성공. 다음 탐색시 이동")
except:
if item['guessit']['episode'] == guessit(v)['episode'] and item['guessit']['release_group'].lower() == guessit(v)['release_group'].lower() :
shutil.move(os.path.join(path,v),os.path.join(spath,v))
os.rename(s,os.path.join(spath,os.path.splitext(v)[0]+'.ko.srt'))
logger.debug("불러오기 성공. 다음 탐색시 이동")
else:
logger.debug("자막에 맞는 sub_x 폴더 없음")
except:
pass
@staticmethod
def empty_folder_remove(base_path):
try:
logger.debug('efr - base_path:%s', base_path)
for root, dirs, files in os.walk(base_path, topdown=False):
for name in dirs:
try:
if len(os.listdir(os.path.join(root, name))) == 0:
logger.debug('efr - Deleting:%s', os.path.join(root, name))
try:
os.rmdir(os.path.join(root, name))
except:
logger.debug('efr - FAILED:%s', os.path.join(root, name))
pass
except:
pass
logger.debug('해외 TV 파일처리 종료')
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
@staticmethod
def check_move_list(source_path, sub_o_path, error_path):
source_path = LogicNormal.load_videos(source_path)
for f in source_path:
path = os.path.dirname(f)
item = LogicNormal.item_list(path, f)
tmdb = LogicNormal.search(item)
LogicNormal.set_ftv(item, tmdb)
LogicNormal.genre_move(item, tmdb, sub_o_path)
@staticmethod
def db_save(data, dest, match_type, is_moved):
# telegram_flag = ModelSetting.get_bool('telegram')
try:
entity = {}
entity['name'] = data['guessit']['title']
entity['fileName'] = data['name']
entity['dirName'] = os.path.basename(data['fullPath'])
entity['targetPath'] = dest
entity['match_type'] = match_type
if is_moved:
entity['is_moved'] = 1
else:
entity['is_moved'] = 0
ModelItem.save_as_dict(entity)
# if telegram_flag == 1:
# text = u'파일정리\n [%s] %s -> %s\n' % (match_type, data['fullPath'], dest)
# #import framework.common.notify as Notify
# #Notify.send_message(text, message_id = 'files_move_result')
# from tool_base import ToolBaseNotify
# ToolBaseNotify.send_message(text, message_id = 'files_move_result')
except Exception as e:
logger.error('Exxception:%s', e)
logger.error(traceback.format_exc())
@staticmethod
def isHangul(text):
#Check the Python Version
pyVer3 = sys.version_info >= (3, 0)
if pyVer3 : # for Ver 3 or later
encText = text
else: # for Ver 2.x
if type(text) is not unicode:
encText = text.decode('utf-8')
else:
encText = text
hanCount = len(re.findall(u'[\u3130-\u318F\uAC00-\uD7A3]+', encText))
return hanCount > 0