-
Notifications
You must be signed in to change notification settings - Fork 0
/
audiobrowse.py
executable file
·181 lines (135 loc) · 4.67 KB
/
audiobrowse.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
#!/usr/bin/python
"""
AudioBrowse
Qt application for browsing audiobooks
author: Maxime Cogney
"""
import sys
import json
import time
import requests
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from ADBLParser import *
class ABFeed(QThread):
def __init__(self):
QThread.__init__(self)
self.urls=[]
self.quitting = False
self.parser = ADBLParser()
def __del__(self):
self.quitting = True
self.wait()
def addUrl(self,url):
self.urls.append(url)
def run(self):
for url in self.urls:
if(self.quitting):
break
self.feedUrl(url)
def feedUrl(self,url):
data = requests.get(url).text
self.parser.feed(data)
for item in self.parser.res:
item=self.cacheImage(item)
self.emit(SIGNAL('feed(PyQt_PyObject)'), item)
self.parser.reset()
def cacheImage(self,item):
if 'img' not in item:
return item
url=item['img']
filename=url.split('/')[-1]
filelocation='img_cache/'+filename
req=requests.get(url, stream=True)
if req.status_code == 200:
with open(filelocation,'wb') as f:
for ch in req.iter_content(1024):
f.write(ch)
else:
print 'Bad requests ({})'.format(req.status_code)
return item
item['img']=filelocation
return item
class ABMain(QMainWindow):
def __init__(self, *args):
QMainWindow.__init__(self, *args)
self.initUI()
# Setting up our worker to scrape the first 10 pages of new Audible releases
self.feed = ABFeed()
for x in range(10):
self.feed.addUrl(page_url.format(x+1))
self.connect(self.feed, SIGNAL('feed(PyQt_PyObject)'), self.table.addEntry)
self.feed.start()
def initUI(self):
self.setWindowTitle("AudioBrowse")
self.resize(800,600)
# File menu
exit_action = QAction('&Exit', self)
exit_action.setShortcut('Ctrl+Q')
exit_action.triggered.connect(qApp.quit)
file_menu = self.menuBar().addMenu('&File')
file_menu.addAction(exit_action)
# Option menu
switch_url_action = QAction('Hide &links', self)
switch_url_action.setShortcut('Ctrl+L')
switch_url_action.setCheckable(True)
switch_url_action.triggered.connect(self.switchUrl)
option_menu = self.menuBar().addMenu('&Option')
option_menu.addAction(switch_url_action)
# Main table
self.table = ABTable(self)
self.setCentralWidget(self.table)
self.table.show()
def switchUrl(self):
if self.sender().isChecked():
self.table.hideColumn(1)
else:
self.table.showColumn(1)
self.table.setColumnWidth(0,self.table.columnWidth(0)-200)
class ABTable(QTableWidget):
# Horizontal header labels
labels=('Title','Link')
def __init__(self, *args):
QTableWidget.__init__(self, *args)
self.initUI()
# UI Setup
def initUI(self):
# Setting up headers
self.setColumnCount(len(self.labels))
self.setHorizontalHeaderLabels(self.labels)
self.horizontalHeader().setStretchLastSection(True)
self.setColumnWidth(0,600)
self.verticalHeader().setVisible(False)
# Setting up selection/edition behavior
self.setSelectionBehavior(QAbstractItemView.SelectRows)
self.setSelectionMode(QAbstractItemView.NoSelection)
self.setEditTriggers(QAbstractItemView.NoEditTriggers)
# Allow sorting
self.setSortingEnabled(True)
# Add entry (row) to table from a dictionnary
def addEntry(self, data):
# Verify item validity
if any(x not in data for x in ('title','img','href')):
print 'Invalid item'
return
# Setting up items
title = QTableWidgetItem(data['title'])
title.setData(Qt.DecorationRole,QPixmap('img_cache/'+data['img'].split('/')[-1]))
href = QTableWidgetItem(data['href'])
# Qt sorts at item insertion, which means we gotta hold auto sort calls until the
# whole row is filled up if we don't want a mess or overwritten cells.
self.setSortingEnabled(False)
# Creating new row
new_size = self.rowCount()
self.insertRow(new_size)
self.setItem(new_size,0,title)
self.setItem(new_size,1,href)
# Reenabling sorting
self.setSortingEnabled(True)
def main():
app = QApplication(sys.argv)
main = ABMain()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()