How to integrate wget with PyQT?
-
Hi currently i am developing an small pyqt tool and its a front end for Wget since i am developing it on Linux i could invoke the wget by subprocess.call but i need to deploy this application in windows too how can i accomplish this ?
kindly guide me to complete this application.
@import sys
from PyQt4 import QtGui, QtCore
import subprocess
class Home(QtGui.QWidget):
def init(self):
QtGui.QMainWindow.init(self)
self.setWindowTitle('Dinesh Downloader')
self.vbox = QtGui.QVBoxLayout()
self.setLayout(self.vbox)
self.FileNameLabel = QtGui.QLabel('No file selected')
self.vbox.addWidget(self.FileNameLabel)
FileChooserButton = QtGui.QPushButton('Choose file', self)
self.vbox.addWidget(FileChooserButton)
self.connect(FileChooserButton, QtCore.SIGNAL('clicked()'), self.get_fname)
Output=QtGui.QTextEdit()
self.vbox.addWidget(Output)
def get_fname(self):
fname = QtGui.QFileDialog.getOpenFileName(self, 'Select file')
subprocess.call(["wget", "-i", fname])
if fname:
self.FileNameLabel.setText(fname)
else:
self.FileNameLabel.setText('No file selected')if name == "main":
app = QtGui.QApplication(sys.argv)
gui = Home()
gui.show()
app.exec_()@ -
Hi and welcome to devnet,
From the top of my mind, did you try with python's wget library ?
Hope it helps
-
[quote author="SGaist" date="1404204795"]Hi and welcome to devnet,
From the top of my mind, did you try with python's wget library ?
Hope it helps[/quote]
Nope i dint but just now took a look at that all i want to do is to perform
wget -i Downloads.txt where the text file will have all the download linksI think Python's wget library not providing this feature
-
SGaist is completely correct, use the Python wget module (easy_install wget). All the "-i" option does is tell wget to open the file and download each of the links it contains, we can easily do the same in python. The following is a complete working example:
pyqtwget.py
@
import sip
sip.setapi('QString',2)
sip.setapi('QVariant',2)from PyQt4.QtCore import *
from PyQt4.QtGui import *
import wgetclass Widget(QWidget):
def init(self, parent=None, **kwargs):
QWidget.init(self, parent, **kwargs)self.setWindowTitle("wget") l=QVBoxLayout(self) self._fileNameLabel=QLabel("No file selected", self) l.addWidget(self._fileNameLabel) self._chooseFile=QPushButton("Choose File...", self, clicked=self.getFileName) l.addWidget(self._chooseFile) self._progress=QProgressBar(self) l.addWidget(self._progress) self._progressLabel=QLabel(self) l.addWidget(self._progressLabel) @pyqtSlot() def getFileName(self): fileName=QFileDialog.getOpenFileName(self, "Select File") if not fileName: return self._fileNameLabel.setText(fileName) def updateProgress(current, total, width=80): self._progress.setMaximum(total) self._progress.setValue(current) QApplication.processEvents() return "" with open(fileName, 'rb') as f: for line in f: self._progressLabel.setText("Downloading '{0}'".format(line.strip())) wget.download(line.strip(), bar=updateProgress)
if name=="main":
from sys import argv, exita=QApplication(argv) w=Widget() w.show() w.raise_() exit(a.exec_())
@
downloads.txt
@
http://www.futurecrew.com/skaven/song_files/mp3/razorback.mp3
http://www.futurecrew.com/skaven/song_files/mp3/corruptor.mp3
http://www.futurecrew.com/skaven/song_files/mp3/hold_me.mp3
@Hope this helps ;o)
-
[quote author="jazzycamel" date="1404213661"]SGaist is completely correct, use the Python wget module (easy_install wget). All the "-i" option does is tell wget to open the file and download each of the links it contains, we can easily do the same in python. The following is a complete working example:
pyqtwget.py
@
import sip
sip.setapi('QString',2)
sip.setapi('QVariant',2)from PyQt4.QtCore import *
from PyQt4.QtGui import *
import wgetclass Widget(QWidget):
def init(self, parent=None, **kwargs):
QWidget.init(self, parent, **kwargs)self.setWindowTitle("wget") l=QVBoxLayout(self) self._fileNameLabel=QLabel("No file selected", self) l.addWidget(self._fileNameLabel) self._chooseFile=QPushButton("Choose File...", self, clicked=self.getFileName) l.addWidget(self._chooseFile) self._progress=QProgressBar(self) l.addWidget(self._progress) self._progressLabel=QLabel(self) l.addWidget(self._progressLabel) @pyqtSlot() def getFileName(self): fileName=QFileDialog.getOpenFileName(self, "Select File") if not fileName: return self._fileNameLabel.setText(fileName) def updateProgress(current, total, width=80): self._progress.setMaximum(total) self._progress.setValue(current) QApplication.processEvents() return "" with open(fileName, 'rb') as f: for line in f: self._progressLabel.setText("Downloading '{0}'".format(line.strip())) wget.download(line.strip(), bar=updateProgress)
if name=="main":
from sys import argv, exita=QApplication(argv) w=Widget() w.show() w.raise_() exit(a.exec_())
@
downloads.txt
@
http://www.futurecrew.com/skaven/song_files/mp3/razorback.mp3
http://www.futurecrew.com/skaven/song_files/mp3/corruptor.mp3
http://www.futurecrew.com/skaven/song_files/mp3/hold_me.mp3
@Hope this helps ;o)[/quote]
Thanks for the code friend,i got the GUI up running but i got some error while downloading here it is
@"Qt Warning - invalid keysym: dead_actute"
Traceback (most recent call last):
File "pywget.py", line 44, in getFileName
wget.download(line.strip(), bar=updateProgress)
File "/usr/lib/python3.4/site-packages/wget.py", line 293, in download
filename = filename_from_url(url) or "."
File "/usr/lib/python3.4/site-packages/wget.py", line 40, in filename_from_url
if len(fname.strip(" \n\t.")) == 0:
TypeError: Type str doesn't support the buffer API
@
But now i have got some idea i will try to use them will post it here if get struct -
Its simple enough, I wrote my example using Python 2.7 which uses strings by default, you're using 3.4 which expects unicode. It seems the wget module is another victim of the 2to3 conversion process. If you change line 44 as follows it will work fine:
@
wget.download(str(line.strip(), encoding="utf8"), bar=updateProgess)
@Hope this helps ;o)
-
[quote author="jazzycamel" date="1404225693"]Its simple enough, I wrote my example using Python 2.7 which uses strings by default, you're using 3.4 which expects unicode. It seems the wget module is another victim of the 2to3 conversion process. If you change line 44 as follows it will work fine:
@
wget.download(str(line.strip(), encoding="utf8"), bar=updateProgess)
@Hope this helps ;o)[/quote]
Again thanks for the correction buddy another small problem was the program is downloading the first URL not the rest should i readlines()??
-
No, it should work fine assuming the lines in your file are terminated correctly. The code at lines 41 and 42 as follows:
@
with open(fileName, 'rb') as f:
for line in f:
@Takes care of opening the file and the iterating over it line by line. This works fine for me with Windows and Mac and Python versions 2.7 and 3.4. You can use readlines() if you like but it shouldn't be necessary.