-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfolder.py
163 lines (133 loc) · 4.84 KB
/
folder.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
import os
import urllib
import requests
import json
import conf
from multiprocessing import Pool
from decorators import click_protection
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
def cache_image(img_url):
img_dir = os.path.join('cache', '1tv')
img_file = os.path.join(img_dir, os.path.basename(img_url))
if not os.path.exists(img_dir):
os.makedirs(img_dir)
if not os.path.exists(img_file):
try:
urllib.request.urlretrieve(img_url, img_file)
except Exception:
return 'img/1tv_default.png'
return img_file
class FolderView(QWidget):
rendered = False
playlist = []
path = []
_resp = {}
MIMETYPES = (
('video', ('ogv', 'mpg', 'mpeg', 'avi', 'mkv', 'm4v', 'flv',
'webm', 'wmv', 'ts', 'vob', 'mov', 'mp4')),
('audio', ('ogg', 'oga', 'flac', 'mp3', 'mp2', 'mpc', 'wma',
'aac', 'ac3', 'aiff', 'ape', 'm4a', 'wav', 'wv',)),
('img', ('png', 'jpeg', 'jpg', 'gif', 'tif', 'tiff', 'bmp'))
)
def __init__(self, parent=None):
super().__init__(parent)
self.setLayout(QVBoxLayout())
self.files = QListWidget()
self.files.setIconSize(QSize(64, 64))
self.setStyleSheet('''
QListWidget, QLabel {
font-size: 50px;
margin: 30px 50px;
}
QLabel {
margin-bottom: 5px;
}
QListWidget:item:focus:selected {
background-color: rgb(100,100,100);
}
QListWidget:focus {
background-color: rgb(80,80,80);
}
''')
self.files.itemActivated.connect(self.activate_item)
self.files.keyPressEvent = click_protection(self.files.keyPressEvent, s=self.files)
self.path_info = QLabel('/{}'.format('/'.join(self.path)))
self.layout().addWidget(self.path_info)
self.layout().addWidget(self.files)
def render(self):
print('RENDER')
self.show_files_list()
self.rendered = True
self.files.setFocus()
def get_url(self, filename=None):
_path = self.path + [filename or '']
quoted_path = list(map(urllib.parse.quote, _path))
return 'http://{server}/{path}'.format(
server=conf.MEDIA_SERVER,
path='/'.join(quoted_path)
)
def show_files_list(self):
while self.files.count() > 0:
c = self.files.takeItem(0)
self.files.removeItemWidget(c)
self.playlist = []
r = requests.get(self.get_url())
self._resp = r.json()
#pool = Pool(processes=16)
#images = pool.map(cache_image, [i.get('image') for i in r.get('projects', [])])
#pool.terminate()
if self.path:
self.add_item({'name': 'Назад', 'type': 'up'})
for _file in self._resp:
self.add_item(_file)
self.files.show()
self.files.setCurrentRow(self.path and 1 or 0)
def get_mime(self, _file):
if _file['type'] == 'up':
return 'up'
elif _file['type'] == 'directory':
return 'directory'
mt = filter(lambda m: _file['name'].split('.')[-1].lower() in m[1], self.MIMETYPES)
return (list(mt) or [['other']])[0][0]
def get_icon(self, mime):
return 'img/nav/{}.png'.format(mime)
def add_item(self, _file):
self.path_info.setText('/{}'.format('/'.join(self.path)))
mime_type = self.get_mime(_file)
icon = self.get_icon(mime_type)
url = self.get_url(_file['name'])
item = QListWidgetItem(QIcon(icon), _file['name'])
item.file_type = _file['type']
item.name = _file['name']
item.url = url
if mime_type in map(lambda m: m[0], self.MIMETYPES):
self.playlist.append((url, _file['name'], icon))
item.index = len(self.playlist) - 1
self.files.addItem(item)
def activate_item(self, item):
if item.file_type == 'up':
self.path.pop(-1)
elif item.file_type == 'directory':
self.path.append(item.name)
elif item.file_type == 'file' and hasattr(item, 'index'):
return self.play(item)
self.show_files_list()
def play(self, item):
print('PLAY')
self.window().player.playlist = self.playlist
self.window().player.current_index = item.index
self.window().player.play()
self.window().setFocus()
@click_protection
def keyPressEvent(self, event):
print('folder', event.key())
if event.key() == Qt.Key_Escape:
self.window().categories.show()
self.window().categories.widget().setFocus()
else:
super().keyPressEvent(event)
def setFocus(self):
super().setFocus()
self.files.setFocus()