-
Notifications
You must be signed in to change notification settings - Fork 1
/
dsd.py
executable file
·95 lines (73 loc) · 2.82 KB
/
dsd.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
#!/usr/bin/python
import os
import json
from subtitles import Subtitle
class DSD(object):
def __init__(self, config):
self.config = config
def run(self):
self.download_subtitle_queue(self.search_missing_subtitles(self.config["shows_folder"]))
"""
Search in the directory for missing subtitles
Returns a dictionary with video files missing subtitles
"""
def search_missing_subtitles(self, directory):
directory_dict = self.directory_to_dict(directory)
video_dict = self.separate_video_files(directory_dict)
subtitle_dict = self.separate_srt_files(directory_dict)
# Remove from dict files already with subtitles
to_remove = []
for subtitle in subtitle_dict:
for video in video_dict:
if (subtitle_dict[subtitle]['file_name'][:-4] == video_dict[video]['file_name'][:-4]):
to_remove.append(video)
for remove in to_remove:
del video_dict[remove]
return video_dict
"""
Creates a dictionary with all files from the directory
"""
def directory_to_dict(self, directory):
directory_files = {}
cnt = 0
for path, subdirs, files in os.walk(directory):
for name in files:
loop_dict = {}
file_extension = os.path.splitext(name)[1]
if (file_extension == ".mp4" or file_extension == ".avi" or file_extension == ".mkv" or file_extension == ".srt"):
loop_dict = {cnt: {"file_name" : name, "file_location" : path}}
directory_files.update(loop_dict)
cnt += 1
return directory_files
"""
Remove all video files from dictionary
"""
def separate_video_files(self, directory_dict):
subtitle_dict = {}
cnt = 0
for i in directory_dict:
if (directory_dict[i]['file_name'][-3:]) != 'srt':
subtitle_dict[cnt] = directory_dict[i]
cnt += 1
return subtitle_dict
"""
Remove all video files from dictionary
"""
def separate_srt_files(self, directory_dict):
video_dict = {}
cnt = 0
for i in directory_dict:
if (directory_dict[i]['file_name'][-3:]) == 'srt':
video_dict[cnt] = directory_dict[i]
cnt += 1
return video_dict
"""
Loop on the missing subtitles list and start the downloads
"""
def download_subtitle_queue(self, missing_subtitles):
for subtitle in missing_subtitles:
print 'Downloading', missing_subtitles[subtitle]['file_name']
new_subtitle = Subtitle(missing_subtitles[subtitle], self.config)
new_subtitle.start_download()
missing_subtitles.clear()
print 'DONE!\n'