Exemplo n.º 1
0
class LoginThread(QtCore.QThread):
    # login_signal = pyqtSignal(str)

    def __init__(self, settingController, username, password, mainwindow):
        super(LoginThread, self).__init__()
        self.mainwindows = mainwindow
        self.storagonSDK = StoragonSDK(serverURL)
        # self.login_signal.connect(self.mainwindows.startListen)
        self.username = username
        self.password = password
        self.settingController = settingController


    def run(self):
        try:
            login = self.storagonSDK.login(self.username, self.password)
        except:
            self.settingController.trigger.emit(-1, '0', '0')
            return
        if login:
            self.settingController.trigger.emit(1, self.username, self.password)
            self.mainwindows.logined = True
        else:
            self.settingController.trigger.emit(0, '0', '0')
Exemplo n.º 2
0
class UploadThread(QtCore.QThread):
    def __init__(self, uploadController, threadid, file_path, username, password, folder_id=None):
        super(UploadThread, self).__init__()

        #   ::type: TabDownloadController
        #   threadID = filepath
        #   phai truyen mainwindows.
        self.username = username
        self.password = password
        self.storagonSDK = StoragonSDK(serverURL)
        self.threadID = threadid
        self.file_path = file_path
        self.upload_folder_id = folder_id
        self.uploadController = uploadController
        self.pausenow = False

    def run(self):
        login = self.storagonSDK.login(self.username, self.password)
        if login:
            self.threadID = self.threadID.decode("utf-8")
            print("ThreadID XXX : %s " % self.threadID)
            self.file_path = self.file_path.decode("utf-8")
            self.storagonSDK.uploadFile(filePath=self.file_path, folder_id=self.upload_folder_id,
                                        progressFunction=self.progressFunction)
            time.sleep(0.1)

    def checkAndWait(self):
        while True:
            if self.pausenow:
                time.sleep(0.5)
            else:
                break

    def progressFunction(self, file_id, download_d, file_size, upload_d, speed=None, links=None):
        """

        :param file_id: uploaded file_id
        :param download_d: 0
        :param upload_t: file_size
        :param upload_d: uploaded size
        :return:
        """
        #   self.threadID, self.site_upload, self.username
        self.checkAndWait()

        if file_id == 0 and download_d == 0 and file_size > 0 and upload_d == 0:
            self.uploadController.updateStatus(self.threadID, status='Uploading', file_size=file_size)
            self.file_size = float(file_size)
        elif file_id == 0 and download_d == 0 and file_size > 0 and upload_d > 0:
            percent = upload_d / self.file_size
            kbspeed = ''
            if speed:
                kbspeed = str(speed / 1024) + 'Kb/s'
            self.uploadController.updateStatus(self.threadID, status='Uploading %.2f%%' % (percent * 100),
                                               speed=kbspeed)
        elif links and download_d == 0 and file_size == self.file_size and upload_d == self.file_size:
            self.uploadController.updateStatus(self.threadID, status='Completed', links=links)
        else:
            self.uploadController.updateStatus(self.threadID, status='Unknow state')

    def stop_uploadthread(self):
        self.storagonSDK.stop_thread()
Exemplo n.º 3
0
class UploadThread(QtCore.QThread):
    def __init__(self, uploadQueue, uploadController, appFolderPath):
        super(UploadThread, self).__init__()
        # ::type: TabDownloadController
        # threadID = filepath

        # phai truyen mainwindows.
        self.appFolderPath = appFolderPath
        self.uploadController = uploadController
        self.uploadQueue = uploadQueue
        self.pausenow = False
        # self.stop = False
        self.statusqueueworker = Queue.Queue()
        self.statusqueueworker.put(True)

    def run(self):
        # self.checkAndWait()
        # print(u"Upload thread start to upload "+self.file_path)
        # self.uploadController.setStatusBar('Uploading...')

        while 1:
            # file, site_upload, username, password
            self.statusqueueworker.get()
            upload_info = self.uploadQueue.get()
            time.sleep(0.5)
            if len(upload_info) == 8:
                self.threadID, self.site_upload, self.username, self.password, self.browser, self.ftp_host, self.ftp_login, self.ftp_password = upload_info
                self.threadID = self.threadID.decode("utf-8")
                download_link = self.upload_file(self.threadID, self.site_upload, self.username, self.password,
                                                 self.ftp_host, self.ftp_login, self.ftp_password)

            else:

                self.total_fileupload, self.movies_info, self.remote_setting = upload_info
                list_downloadlink = []
                list_downloadlink_torrent = []
                for line_upload in self.total_fileupload:
                    # self.threadID = line_upload
                    list_upload = self.uploadController.mainWindow.get_list_site_upload(line_upload)
                    for line_f_upload in list_upload:
                        self.site_upload = line_f_upload.keys()[0]
                        info_upload = line_f_upload.values()[0]
                        self.threadID = info_upload['file']
                        self.username = info_upload['username']
                        self.password = info_upload['password']
                        self.ftp_host = info_upload['ftp_host']
                        self.ftp_login = info_upload['ftp_login']
                        self.ftp_password = info_upload['ftp_password']
                        self.uploadController.updateStatus(self.threadID, host=self.site_upload,
                                                           username=self.username, status='Ready for upload',
                                                           type_status='Upload')
                        download_link = self.upload_file(self.threadID, self.site_upload, self.username, self.password,
                                                         self.ftp_host, self.ftp_login, self.ftp_password)
                        print download_link
                        if self.site_upload == 'Storagon' and download_link:
                            list_downloadlink.append(download_link)
                            list_downloadlink_torrent.append(download_link + '?auto=torrent')
                    if self.remote_setting['leech_remove_file']:
                        i = 0
                        while i < 5:
                            try:
                                os.remove(u'' + line_upload)
                            except:
                                time.sleep(1)
                            else:
                                break
                            i += 1

                if list_downloadlink:
                    list_downloadlink.sort()
                    list_downloadlink_torrent.sort()
                    self.movies_info.link_download = {'Storagon': list_downloadlink,
                                                      'Storagon torrent': list_downloadlink_torrent}
                # movieinfo.name
                file_save = self.movies_info.name.decode('utf-8', 'ignore').\
                    replace('/', '-').replace("\\", '-').replace(':', '-').replace('*', '').replace('?', '')
                path_filesave = os.path.join(self.remote_setting['save_leechpath'].strip().decode('utf-8'),
                                             file_save + '.txt')

                try:
                    f = open(u'' + path_filesave, 'w')
                    f.write(self.movies_info.toBBcode() + '\n===\n')
                    f.write(self.movies_info.toHTML())
                    f.close()
                    lf = open(os.path.join(self.appFolderPath, 'leeched_database.txt'), 'a')
                    name_file = re.sub(r' +', ' ', re.sub(r'<.+?>|&.+?;', '',
                                                          re.sub(r'</p>|<br\s*/>', '\n', self.movies_info.name,
                                                                 re.DOTALL)))
                    lf.write(name_file + '\n')
                    lf.close()
                except Exception as e:
                    print e
                self.uploadController.updateStatus(file_save, status=path_filesave, type_status='Leech_completed')
                if self.movies_info.poster and self.remote_setting['leech_auto_post_blog'] and self.remote_setting[
                    'leech_blog_link'] and self.remote_setting['leech_blog_username'] and self.remote_setting[
                    'leech_blog_password']:
                    listcategory = []
                    listtag = []
                    if self.remote_setting['leech_blog_listcategory']:
                        listcategorys = self.remote_setting['leech_blog_listcategory']
                        if listcategorys.find(',') != -1:
                            for line_calog in listcategorys.split(','):
                                listcategory.append(line_calog.strip())
                        else:
                            listcategory.append(listcategorys.strip())
                    if self.remote_setting['leech_blog_listtag']:
                        listtags = self.remote_setting['leech_blog_listtag']
                        if listtags.find(',') != -1:
                            for line_tag in listtags.split(','):
                                listtag.append(line_tag.strip())
                        else:
                            listtag.append(listtags.strip())
                    print '===Post to your blog==='
                    poster = wppost.Wppost()
                    poster.post(self.remote_setting['leech_blog_link'].strip(),
                                self.remote_setting['leech_blog_username'].strip(),
                                self.remote_setting['leech_blog_password'].strip(), self.movies_info.name,
                                self.movies_info.toHTML(), listcategory, listtag)

            self.statusqueueworker.put(True)
            time.sleep(0.1)

    def upload_file(self, file, site_upload, username, password, ftp_host='', ftp_login='', ftp_password=''):
        # print file
        if site_upload == 'Storagon':
            self.uploadSKD = StoragonSDK()
        elif site_upload.find('rapidgator') != -1:
            self.uploadSKD = rapidgatorSDK.Rapidgator(browser=self.browser)
        else:
            self.uploadSKD = ftpSDK.FtpSDK(browser=self.browser)
        i = 0
        while i < 5:
            try:
                if site_upload == 'Storagon':
                    login = self.uploadSKD.login(username, password)
                else:
                    login = self.uploadSKD.login(email=username, password=password,
                                                 ftp_host=ftp_host, ftp_login=ftp_login,
                                                 ftp_password=ftp_password)
                if not login:
                    i += 1
                    continue
                if site_upload == 'Storagon':
                    link_down = self.uploadSKD.uploadFile(file, progressFunction=self.progressFunction)
                else:
                    link_down = self.uploadSKD.uploadFile(file, progressFunction=self.progressFunction,
                                                          username=username, password=password)
                if link_down:
                    return link_down
                    # return link_down
            except Exception as e:
                print e
                time.sleep(1)
            else:
                break
            i += 1
        if i >= 5:
            self.uploadController.updateStatus(file, status='Cant login to your account',
                                               host=site_upload, username=username)

    def checkAndWait(self):
        while True:
            if self.pausenow:
                time.sleep(0.5)
            else:
                break

    def progressFunction(self, file_id, download_d, file_size, upload_d, speed=None, link=None):
        """
        :param file_id: uploaded file_id
        :param download_d: 0
        :param upload_t: file_size
        :param upload_d: uploaded size
        :return:
        """
        # self.threadID, self.site_upload, self.username
        self.checkAndWait()

        if file_id == 0 and download_d == 0 and file_size > 0 and upload_d == 0:
            self.uploadController.updateStatus(self.threadID, status='Uploading', file_size=file_size,
                                               host=self.site_upload, username=self.username, type_status="Upload")
            self.file_size = float(file_size)
        elif file_id == 0 and download_d == 0 and file_size > 0 and upload_d > 0:
            percent = upload_d / self.file_size
            kbspeed = ''
            if speed:
                kbspeed = str(speed / 1024) + 'Kb/s'
            self.uploadController.updateStatus(self.threadID, status='Uploading %.2f%%' % (percent * 100),
                                               speed=kbspeed, host=self.site_upload, username=self.username,
                                               type_status="Upload")
        elif link and download_d == 0 and file_size == self.file_size and upload_d == self.file_size:
            self.uploadController.updateStatus(self.threadID, status='Completed', host=self.site_upload,
                                               username=self.username, link=link, type_status="Upload")
        else:
            self.uploadController.updateStatus(self.threadID, status='Unknow state', host=self.site_upload,
                                               username=self.username, type_status="Upload")

    def stop_uploadthread(self):
        if self.site_upload == 'Storagon':
            self.uploadSKD.stop_thread()
Exemplo n.º 4
0
class tabDownload(QtWidgets.QWidget):
    trigger = pyqtSignal(int, str)
    input_link_signal = pyqtSignal(str)

    def __init__(self, mainWindow, download_complete_signal):
        super(tabDownload, self).__init__(mainWindow)
        self.mainWindow = mainWindow
        global serverURL
        self.storagonSDK = StoragonSDK(serverURL)
        self.download_complete_signal = download_complete_signal
        self.Ui_tabDownloadForm = Ui_tabDownloadForm()
        self.Ui_tabDownloadForm.setupUi(self)
        self.table = self.Ui_tabDownloadForm.tableWidget
        self.table.hideColumn(3)
        self.table.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.table.customContextMenuRequested.connect(self.openDownloadMenu)
        self.setTableWidth()
        self.show()
        self.mainWindow = mainWindow
        self.trigger.connect(self.updateStatus)
        self.input_link_signal.connect(self.chooseFileToDownload)

        self.Ui_tabDownloadForm.btn_addfile.setIcon(QtGui.QIcon(resource_path('add.png')))
        self.Ui_tabDownloadForm.btn_addfile.setIconSize(QtCore.QSize(24, 24))

        self.Ui_tabDownloadForm.btn_addfile.clicked.connect(self.showinputbox)
        self.inputbox = None
        self.fileToDownload = []
        self.threaddownload = {}
        self.maxthreaddownload = 4
        self.running_thread = 0
        self.fileSavePath = ''

    def download(self):
        # global storagon_username, storagon_password
        # print storagon_username, storagon_password
        login = self.storagonSDK.login(self.mainWindow.username, self.mainWindow.password)
        if login:
            print("Login Success !")
            while len(self.fileToDownload):
                if self.running_thread == self.maxthreaddownload:
                    break
                filex = self.fileToDownload.pop(0)
                link = filex
                if link in self.threaddownload.keys():
                    th = self.threaddownload[link]
                    # thread bi pause, gio cho download tiep
                    if th.downloadFolder == self.fileSavePath and th.pausenow:
                        th.pausenow = False
                else:
                    self.th = DownloadThread(link, self, link, self.fileSavePath)
                    self.threaddownload[link] = self.th  # add to thread list
                    self.th.start()
                    self.th.finished.connect(
                        lambda: self.download_complete_signal.emit(str(link),
                                                                   str(self.fileSavePath),
                                                                   self.th.start_time,
                                                                   self.th.finish_time)
                    )
                self.running_thread += 1
        else:
            print('Login Fail')
            # sys.exit(1)

    def updateStatus(self, threadID, status=None, file_size=None, up_to_path=None,
                     speed=None, types='download', error=None):
        num_row = self.table.rowCount()
        for i in range(num_row):
            try:
                link = self.table.item(i, 0).text()
            except:
                continue
            threadID = threadID
            if link == threadID:
                if file_size:
                    size_item = self.table.item(i, 1)
                    size_item.setText(str(file_size) + ' byte')
                if speed:
                    status_item = self.table.item(i, 2)
                    message = status + " " + speed
                    status_item.setText(str(message))
                elif error:
                    error_item = self.table.item(i, 2)
                    error_item.setText("Error: " + str(error))
                    self.running_thread -= 1
                elif status:
                    status_item = self.table.item(i, 2)
                    status_item.setText(status)
                    # self.table.update(self.table.indexFromItem(status_item))
                    if status == 'Download done!':

                        self.running_thread -= 1
                        if len(self.fileToDownload):
                            self.download()
                        else:
                            return True
                return True

    def setTableWidth(self):
        width = QtWidgets.QDesktopWidget().width() / 8
        for i in range(self.table.columnCount()):
            self.table.setColumnWidth(i, width)

    def openDownloadMenu(self, position):
        menu = QtWidgets.QMenu(self)
        # upload action
        Actionpause = menu.addAction('&Pause')
        Actionpause.triggered.connect(self.run_pause_selected)
        Actionpause = menu.addAction('&Resume')
        Actionpause.triggered.connect(self.run_resume_selected)
        Actionpause = menu.addAction('&Stop And Remove')
        Actionpause.triggered.connect(self.run_remove_selected)
        Actionpause = menu.addAction('&Open Folder Location')
        Actionpause.triggered.connect(self.open_downloaded_location_selected)
        menu.exec_(self.table.viewport().mapToGlobal(position))

    def openFolderPath(self, path):
        if sys.platform == 'darwin':
            subprocess.call(['open', '--', path])
        elif sys.platform == 'linux2':
            subprocess.call(['gnome-open', '--', path])
        elif sys.platform == 'win32':
            # subprocess.call(['explorer', path])
            ctypes.windll.shell32.ShellExecuteW(None, u'open', u'explorer.exe',
                                                u'/n,/select, ' + path, None, 1)

    def open_downloaded_location_selected(self):
        rows = self.Ui_tabDownloadForm.tableWidget.selectionModel().selectedRows()
        filesavepathList = []
        for row in rows:
            r = row.row()
            status = str(self.Ui_tabDownloadForm.tableWidget.item(r, 2).text())
            if status.find('Download done!') != -1:
                filesavepath = self.Ui_tabDownloadForm.tableWidget.item(r, 3).text()
                filesavepathList.append(filesavepath)
        filesavepathSet = set(filesavepathList)
        # print filesavepathSet
        for path in filesavepathSet:
            if os.path.isdir(path):
                self.openFolderPath(path)
            else:
                QtWidgets.QMessageBox.warning(self, "Notification Box", "Folder " +
                                              path + " Doesn't Exist", QtWidgets.QMessageBox.Ok)

    def run_pause_selected(self):
        row_selected = list(set([item.row() for item in self.table.selectedItems()]))

        if not row_selected:
            QtWidgets.QMessageBox.warning(self, 'Warning', "Please Choose File(s) To Pause", QtWidgets.QMessageBox.Ok)
        else:
            for row in row_selected:
                link = self.table.item(row, 0).text()

                for threadID in self.threaddownload.keys():
                    if threadID == link:
                        th = self.threaddownload[threadID]
                        if not th.pausenow:
                            th.pausenow = True
                            self.running_thread -= 1
                            # update status
                            status_item = self.table.item(row, 2)
                            message = "Paused"
                            status_item.setText(str(message))

    def run_resume_selected(self):
        row_selected = list(set([item.row() for item in self.table.selectedItems()]))
        if not row_selected:
            row_selected = range(self.table.rowCount())
        error = 0
        list_remove_thread = []
        for row in row_selected:
            file_path = self.table.item(row, 0).text()

            for threadID in self.threaddownload.keys():

                if threadID == file_path:
                    th = self.threaddownload[threadID]
                    if th.pausenow:
                        if self.running_thread < self.maxthreaddownload:
                            th.pausenow = False
                            self.running_thread += 1
                        else:
                            error = 1
                    else:
                        # check xem thread co bi loi khong
                        status = self.table.item(row, 2).text()
                        # Thread bi loi va gio nguoi dung resume
                        if status.find('Error') != -1:
                            if self.running_thread < self.maxthreaddownload:
                                self.fileToDownload.append(file_path)
                                list_remove_thread.append(file_path)
                                self.running_thread -= 1
                            else:
                                error = 1
            # remove thread cua file down bi loi de down lai
            if list_remove_thread:
                for th in list_remove_thread:
                    del self.threaddownload[th]
                self.download()
        if error == 1:
            QtWidgets.QMessageBox.warning(self, 'Warning', "Number Of Running Thread Is Maximum. Please" +
                                      "Wait Until Other Files Completed", QtWidgets.QMessageBox.Ok)

    def run_remove_selected(self):
        row_selected = list(set([item.row() for item in self.table.selectedItems()]))
        while len(row_selected):
            row = row_selected.pop(0)
            if len(row_selected):
                row_selected = [row - 1 for row in row_selected]
            file_path = self.table.item(row, 0).text()
            status = self.table.item(row, 2).text()
            self.table.removeRow(row)
            for threadID in self.threaddownload.keys():
                if threadID == file_path:
                    th = self.threaddownload[threadID]
                    th.pausenow = True
                    self.running_thread -= 1
                    if status.find('Download done!') != -1:
                        del self.threaddownload[threadID]
        self.download()

    def showinputbox(self):
        if self.inputbox is None:
            self.inputbox = tabDownload_InputBox(tab_download=self)

    def chooseFileToDownload(self, links):
        text_links = links
        links = text_links.split('\n')
        links = filter(lambda x: x != '', links)

        numrow = self.table.rowCount()
        row_links = []
        for i in range(numrow):
            row_links.append(self.table.item(i, 0).text())

        download_links = []
        for link in links:
            if link not in row_links:
                download_links.append(link)

        if download_links:
            self.fileSavePath = QtWidgets.QFileDialog.getExistingDirectory(self, "Select path to Save!", '')
            if self.fileSavePath:
                for link in download_links:
                    curRow = self.table.rowCount()
                    self.table.insertRow(curRow)
                    self.table.setItem(curRow, 0, QtWidgets.QTableWidgetItem(link))
                    self.table.setItem(curRow, 1, QtWidgets.QTableWidgetItem(u'Updating...'))
                    self.table.setItem(curRow, 2, QtWidgets.QTableWidgetItem(u'Ready'))
                    self.table.setItem(curRow, 3, QtWidgets.QTableWidgetItem(self.fileSavePath))
                    self.fileToDownload.append(link)
                self.download()