-
Notifications
You must be signed in to change notification settings - Fork 4
/
directory.py
291 lines (226 loc) · 7.93 KB
/
directory.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
# -*- coding: utf-8 -*-
import imghdr
import urlquick
from config import Config
RIGHT_ARROW = u'\U000027A1'
LEFT_ARROW = u'\U00002B05'
FOLDER = u'\U0001F4C1'
PLAY = u'\U000025B6'
SQUARE = u'\U000025FD'
WEB = u'\U0001F30D'
COMPUTER = u'\U0001F5A5'
CHECK = u'\U00002705'
NO_CHECK = u'\U0000274C'
RED_RING = u'\U00002B55'
TV = u'\U0001F4FA'
WARNING = u'\U000026A0'
BACK_ARROW = u'\U0001F519'
LEFT_ARROW_CURVING_RIGHT = u'\U000021AA'
QUESTION_MARK = u'\U00002754'
def check_image(path):
# First we need to check if image is local or from internet
image_pp = ''
if 'http' in path:
image_pp += WEB + ' '
if not Config.get('disable_image_check'):
# We need to check the status code without download the image
r = urlquick.head(path)
if 'image' in r.headers['content-type']:
image_pp += CHECK
else:
image_pp += NO_CHECK
else:
image_pp += QUESTION_MARK
else:
image_pp += COMPUTER + ' '
try:
result = imghdr.what(path)
if result is None:
image_pp += NO_CHECK
else:
if path == Config.get('addon_icon_filepath'):
image_pp += TV
elif path == Config.get('addon_fanart_filepath'):
image_pp += TV
else:
image_pp += CHECK
except Exception:
image_pp += NO_CHECK
return image_pp
def compute_column_size(column):
if column == 'label':
return int(Config.get('console_size') / 5)
if column == 'plot':
return int(Config.get('console_size') / 4)
raise Exception(
'Unknown column name: {}'.format(column))
def truncate_string(column, string):
max_size = compute_column_size(column) - 1
string_len = len(string)
result = string
if string_len > max_size:
last_chars = string[-4:]
first_chars = string[:max_size - 8]
result = first_chars + '...' + last_chars
return result
class Item():
def __init__(self, url='', listitem=None, is_folder=False, key=0):
self.url = url
self.listitem = listitem
self.is_folder = is_folder
self.key = key
def get_label(self):
return self.listitem.getLabel()
def get_query_string(self):
splitted_url = self.url.split('?')
return splitted_url[1]
def get_base_url(self):
splitted_url = self.url.split('?')
return splitted_url[0]
def format_item(self, cnt):
listitem = self.listitem
is_folder = self.is_folder
url = self.url
# Type
type_pp = SQUARE
if is_folder:
type_pp = FOLDER
elif 'isplayable' in listitem._property and listitem._property['isplayable'] == 'true':
type_pp = PLAY
# Label
label_pp = truncate_string('label', listitem.getLabel())
# Plot
plot_pp = ''
if 'video' in listitem._info and 'plot' in listitem._info['video']:
plot_pp = truncate_string('plot', (listitem._info['video']['plot']))
plot_pp = plot_pp.replace('\n', ' ').replace('\r', '')
# Thumb
thumb_pp = ''
if 'thumb' in listitem._art:
thumb_pp = check_image(listitem._art['thumb'])
else:
thumb_pp = RED_RING
# Fanart
fanart_pp = ''
if 'fanart' in listitem._art:
fanart_pp = check_image(listitem._art['fanart'])
else:
fanart_pp = RED_RING
# Duration
duration_pp = ''
if 'video' in listitem._info and 'duration' in listitem._info['video']:
duration_sec = listitem._info['video']['duration']
if duration_sec < 60:
duration_pp = str(duration_sec) + ' s'
else:
hours = duration_sec // 3600
duration_sec = duration_sec - (hours * 3600)
minutes = duration_sec // 60
seconds = duration_sec - (minutes * 60)
if hours == 0:
duration_pp = str(minutes) + ':' + str(seconds)
else:
duration_pp = str(hours) + ':' + str(minutes) + ':' + str(seconds)
# Date
date_pp = ''
if 'video' in listitem._info and 'date' in listitem._info['video']:
date_pp = listitem._info['video']['date']
'''
if listitem._label2:
print(' - [label2] = ' + listitem._label2)
print('Art')
if listitem._art:
for art_item_k, art_item_v in listitem._art.items():
print(' - [' + art_item_k + '] = ' + art_item_v)
print('Info')
if listitem._info:
for info_item_k, info_item_v in listitem._info.items():
print(' - [' + info_item_k + '] = ' + repr(info_item_v))
print('Stream')
if listitem._stream:
for item_k, item_v in listitem._stream.items():
print(' - [' + item_k + '] = ' + repr(item_v))
print('Property')
if listitem._property:
for property_item_k, property_item_v in listitem._property.items():
print(' - [' + property_item_k + '] = ' + repr(property_item_v))
'''
return {
'key': str(cnt),
'type': type_pp,
'label': label_pp,
'plot': plot_pp,
'thumb': thumb_pp,
'fanart': fanart_pp,
'duration': duration_pp,
'date': date_pp
}
class Directory():
current_directory = None
@classmethod
def is_current_directory_playable(cls):
if len(cls.current_directory.items) == 1 and \
cls.current_directory.items[1].is_folder is False:
return True
return False
def __init__(self, succeeded=False, update_listing=False, path=[]):
self.items = {}
self.succeeded = succeeded
self.update_listing = update_listing
self.path = path
def __str__(self):
dash = '-' * Config.get('console_size')
listing_array = []
first_line = {
'key': '',
'type': '',
'label': 'Label',
'plot': 'Plot',
'thumb': 'Thumb',
'fanart': 'Fanart',
'duration': 'Time',
'date': 'Date'
}
listing_array.append(first_line)
if len(Directory.current_directory.path) > 1:
previous_line = {
'key': str(0),
'type': LEFT_ARROW,
'label': 'Previous menu',
'plot': '',
'thumb': '',
'fanart': '',
'duration': '',
'date': ''
}
listing_array.append(previous_line)
cnt = 0
for key, item in Directory.current_directory.items.items():
cnt += 1
listing_array.append(item.format_item(cnt))
s = '\n'
for i in range(len(listing_array)):
if i == 0:
s += dash + '\n'
s += '{:{key_size}}{:{type_size}}{:{label_size}} {:{plot_size}} {:{duration_size}}{:{date_size}}{:{thumb_size}}{:{fanart_size}}\n'.format(
listing_array[i]['key'],
listing_array[i]['type'],
listing_array[i]['label'],
listing_array[i]['plot'],
listing_array[i]['duration'],
listing_array[i]['date'],
listing_array[i]['thumb'],
listing_array[i]['fanart'],
key_size=3,
type_size=4,
label_size=compute_column_size('label'),
plot_size=compute_column_size('plot'),
thumb_size=6,
fanart_size=6,
duration_size=8,
date_size=12
)
if i == 0:
s += dash + '\n'
s += '\n'
return s