-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBScene.py
173 lines (132 loc) · 4.05 KB
/
DBScene.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
from PySide.QtGui import *
from PySide.QtCore import *
from Widgets import *
import sys
import os
import threading
import time
import unicodedata
UPDATE_THREADS = 5
class ViewScene(QFrame):
newTreedata = Signal(tuple)
def __init__(self, win):
super(ViewScene, self).__init__(win)
self.win = win
self.initUI()
def initUI(self):
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.createWidgets()
self.done = False
self.isSearching = False
def setBackend(self, bg):
self.backend = bg
def startThread(self, func, *args):
t = threading.Thread(target=func,args=args)
t.daemon = True
t.start()
def createWidgets(self):
self.tree = URLViewer(0,2) # id, title, video_id
self.tree.setHorizontalHeaderLabels(['Title','Video ID'])
self.tree.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tree.setColumnWidth(0,460)
self.tree.setColumnWidth(1,90)
self.tree.setMinimumHeight(400)
self.refresh_btn = QPushButton("Refresh list")
self.refresh_btn.clicked.connect(self.initFetcher)
btn_layout = QHBoxLayout()
btn_layout.addWidget(self.refresh_btn)
btn_layout.addStretch(1)
self.layout.addWidget(self.tree)
self.layout.addLayout(btn_layout)
self.final_info = []
self.fetched = 0
self.running = True
self.newTreedata.connect(self.addInfo)
def _disable(self):
self.refresh_btn.setEnabled(False)
def _enable(self):
self.refresh_btn.setEnabled(True)
def chunks(self, l, n):
for i in range(0, len(l), n):
yield l[i:i+n]
def initFetcher(self):
if self.isSearching:
return False
self.startThread(self.startFetcher)
def startFetcher(self):
global UPDATE_THREADS
self.isSearching = True
self.done = False
self._disable()
self.win.status.emit("Fetching URL info...")
self.fetched = 0
self.tree.clear()
self.tree.setHorizontalHeaderLabels(['Title','Video ID'])
# Get Url list
file_name = self.backend.info.data['Player']['backup']
file_path = os.path.join('resources', file_name)
# Read Url list
with open(file_path, 'r') as f:
self.urls = f.read().splitlines()
for x in self.urls:
place = self.urls.index(x)
# remove non-youtube links
if 'youtu' not in x:
self.urls.pop( place )
# remove empty lines
if x == '':
self.urls.pop( place )
# put list in backend
self.final_info = self.urls
self.backend.urls = self.final_info
# Get URLS to fetch
self.fetchlist = []
for x in range(len(self.urls)-1):
url = self.urls[x]
self.fetchlist.append( [ x , url ] )
# Populate Table
self.tree.setRowCount(len(self.fetchlist))
if len(self.fetchlist) > 200:
UPDATE_THREADS = 300
# Split data to multiple threads for faster loading
thread_list = list(self.chunks(self.fetchlist, UPDATE_THREADS))
for thread in thread_list:
self.startThread(self.threadFetcher, thread)
# Wait for threads to finish
while self.fetched != len(thread_list):
time.sleep(0.1)
self.win.status.emit("Finished Fetching")
self._enable()
self.done = True
self.isSearching = False
self.final_info = self.urls
self.backend.urls = self.final_info
def threadFetcher(self, fetch_list):
# Get info
for x in fetch_list:
try:
_id = x[1]
# Get Video ID only
if 'watch?v' in _id:
_id = _id.split('watch?v=')[-1]
if '&' in _id:
_id = _id.split('&')[0]
# make sure program is still on
if not self.running:
break
# Get Title
_title = self.backend.yi.getTitle(_id,_filter=True)
# Add to tree
#self.tree.setItem( x[0], 0, QTableWidgetItem( str(_title) ))
#self.tree.setItem( x[0], 1, QTableWidgetItem( str(_id) ))
self.newTreedata.emit((x[0], _title, _id))
except Exception as e:
print str(e)
if not self.running:
break
self.fetched += 1
def addInfo(self, info):
# (x[0], _title, _id)
self.tree.setItem( info[0], 0, QTableWidgetItem( str(info[1]) ))
self.tree.setItem( info[0], 1, QTableWidgetItem( str(info[2]) ))