Beispiel #1
0
    def __init__(self, parent=None):
        super(Text, self).__init__(parent)

        # Sentence view
        self.view, self.page = QWebView(), QWebPage()
        self.view.setPage(self.page)
        # TODO: update dynamically on parent widget resize events (somehow?)
        self.view.setMinimumSize(parent.width(), parent.height() / 2)

        # Info view
        self.infoView, self.infoPage = QWebView(), QWebPage()
        self.infoView.setPage(self.infoPage)
        self.infoView.setMaximumSize(parent.width() / 2, parent.height())

        # Active terms and sentence
        self.terms, self.example = {}, u''
        # Default style
        self.css = 'solarized-light'

        # Composition
        self.compose({QHBoxLayout(): [self.infoView, self.view]})

        # Initialize events
        self.page.linkHovered.connect(self.showInfo)
        self.page.setLinkDelegationPolicy(QWebPage.DelegateExternalLinks)
        self.page.linkClicked.connect(self.queryServer)

        # Initialize style
        self.clearPages()
Beispiel #2
0
 def __init__(self, url):
   self.app = QApplication(sys.argv)         
        
   QWebPage.__init__(self)
   self.loadFinished.connect(self._loadFinished)  
   self.mainFrame().load(QUrl(url))  
   self.app.exec_()
Beispiel #3
0
class Browser:
    def __init__(self):
        self.network_manager = QNetworkAccessManager()
        self.network_manager.createRequest = self._create_request

        self.web_page = QWebPage()
        self.web_page.setNetworkAccessManager(self.network_manager)

        self.web_view = QWebView()
        self.web_view.setPage(self.web_page)

        self.html_data = None
        self.resultList = []
        self.finished = threading.Event()

    def _create_request(self, operation, request, data):
        reply = QNetworkAccessManager.createRequest(self.network_manager,
                                                    operation, request, data)
        self.resultList.append(request.url().toString())
        return reply

    def myLoadFinish(self):
        #print "finished"
        self.finished.set()

    def getResourceUrlList(self, url):
        self.web_view.load(url)
        self.web_view.loadFinished.connect(self.myLoadFinish)
        self.finished.wait()
        return self.resultList
Beispiel #4
0
class TestFindText(TimedQApplication):
    '''Test cases for finding text'''

    def setUp(self):
        TimedQApplication.setUp(self, timeout=TIMEOUT)
        self.page = QWebPage()
        QObject.connect(self.page, SIGNAL('loadFinished(bool)'),
                        self.load_finished)
        self.called = False

    def tearDown(self):
        #Release resources
        del self.page
        self.called = False
        TimedQApplication.tearDown(self)

    def testFindSelectText(self):
        url = QUrl.fromLocalFile(adjust_filename('fox.html', __file__))
        self.page.mainFrame().load(url)
        self.app.exec_()
        self.assert_(self.called)

    def load_finished(self, ok):
        #Callback to check if load was successful
        if ok:
            self.called = True
            self.assert_(self.page.findText('fox'))
            self.assertEqual(self.page.selectedText(), 'fox')
        self.app.quit()
 def __init__(self, url, image_path):
     QObject.__init__(self)
     self.url = QUrl(url)
     self.image_path = image_path
     self.webpage = QWebPage(self)
     self.webpage.loadFinished.connect(self.render)
     self.webpage.mainFrame().load(self.url)
    def __init__(self, app):
        self.app = app
        QWebPage.__init__(self)

        self.setForwardUnsupportedContent(True)
        self.setUserStylesheet()

        self._loading = False
        self._navigationRequest = False
        self._loading_ev = Event()
        self._userAgent = None
        self._consoleMessages = []
        self._capybaraJavascript = self.loadCapybaraJavascript()
        self._error = None
        self._lastStatus = None
        self._pageHeaders = {}
        self._errorTolerant = False
        self._ignoreSslErrors = False

        self.setCustomNetworkAccessManager()

        self.frameCreated.connect(self.frameCreatedCallback)
        self.unsupportedContent.connect(self.unsupportedContentCallback)

        #self.setLinkDelegationPolicy(WebPage.DelegateAllLinks)
        #self.linkClicked.connect(self.linkClickedCallback)

        self.loadProgress.connect(self.pageLoadProgressCallback)
        self.windowCloseRequested.connect(self.windowCloseRequestedCallback)

        self.setViewportSize(QSize(1680, 1050))
Beispiel #7
0
    def __init__(self, edit):
        QWebPage.__init__(self)
        self.current = None
        self.edit = edit
        self.active_image = None
        self.active_link = None
        settings = self.settings()
        family = self.edit.app.settings.value(
            'note-font-family',
            DEFAULT_FONT,
        )
        size = int(
            self.edit.app.settings.value(
                'note-font-size',
                DEFAULT_FONT_SIZE,
            ))
        settings.setFontFamily(
            QWebSettings.StandardFont,
            family,
        )
        settings.setFontSize(
            QWebSettings.DefaultFontSize,
            size,
        )
        settings.setFontSize(
            QWebSettings.DefaultFixedFontSize,
            size,
        )

        # This allows JavaScript to call back to Slots, connect to Signals
        # and access/modify Qt props
        self.mainFrame().addToJavaScriptWindowObject("qpage", self)
class TestFindText(TimedQApplication):
    '''Test cases for finding text'''
    def setUp(self):
        TimedQApplication.setUp(self, timeout=TIMEOUT)
        self.page = QWebPage()
        QObject.connect(self.page, SIGNAL('loadFinished(bool)'),
                        self.load_finished)
        self.called = False

    def tearDown(self):
        #Release resources
        del self.page
        self.called = False
        TimedQApplication.tearDown(self)

    def testFindSelectText(self):
        url = QUrl.fromLocalFile(adjust_filename('fox.html', __file__))
        self.page.mainFrame().load(url)
        self.app.exec_()
        self.assert_(self.called)

    def load_finished(self, ok):
        #Callback to check if load was successful
        if ok:
            self.called = True
            self.assert_(self.page.findText('fox'))
            self.assertEqual(self.page.selectedText(), 'fox')
        self.app.quit()
Beispiel #9
0
    def __init__(self, edit):
        QWebPage.__init__(self)
        self.current = None
        self.edit = edit
        self.active_image = None
        self.active_link = None
        self.active_table = None
        settings = self.settings()
        family = self.edit.app.settings.value(
            'note-font-family', DEFAULT_FONT,
        )
        size = int(self.edit.app.settings.value(
            'note-font-size', DEFAULT_FONT_SIZE,
        ))
        settings.setFontFamily(
            QWebSettings.StandardFont, family,
        )
        settings.setFontSize(
            QWebSettings.DefaultFontSize, size,
        )
        settings.setFontSize(
            QWebSettings.DefaultFixedFontSize, size,
        )

        # This allows JavaScript to call back to Slots, connect to Signals
        # and access/modify Qt props
        self.mainFrame().addToJavaScriptWindowObject("qpage", self)
    def __init__(self):
        super().__init__()

        self.app = QApplication(sys.argv)

        QNetworkProxyFactory.setUseSystemConfiguration(True)

        self.web_page = QWebPage(self)
        self.web_page.settings().setAttribute(QWebSettings.AutoLoadImages,
                                              False)
        self.web_page.loadFinished.connect(self.load_finished)

        # Адрес страницы объявления
        self.url = None

        # Переменная нужна для замера времени выполнения
        self.t = None

        self.phone_img_parser = AvitoPhoneImgParser()

        # Строка с номером телефона
        self.phone = None

        self.proxy_url = None
        self.proxy_type = None
        self.proxy_enabled = None
Beispiel #11
0
 def __init__(self, token, secret, parent, *args, **kwargs):
     QWebPage.__init__(self, *args, **kwargs)
     self.token = token
     self.secret = secret
     self.parent = parent
     manager = TLSNetworkAccessManager(self)
     manager.sslErrors.connect(self.ssl)
     self.setNetworkAccessManager(manager)
Beispiel #12
0
 def __init__(self, app):
     self.network_manager = QNetworkAccessManager()
     self.network_manager.createRequest = self._create_request
     self.web_page = QWebPage()
     self.web_page.setNetworkAccessManager(self.network_manager)
     self.web_view = QWebView()
     self.web_view.setPage(self.web_page)
     self.html_data = None
     self.app = app
     self.finished = threading.Event()
Beispiel #13
0
    def __init__(self):
        self.network_manager = QNetworkAccessManager()
        self.network_manager.createRequest = self._create_request
        self.network_manager.finished.connect(self._request_finished)

        self.web_page = QWebPage()
        self.web_page.setNetworkAccessManager(self.network_manager)

        self.web_view = QWebView()
        self.web_view.setPage(self.web_page)
Beispiel #14
0
 def __init__(self, window_width, window_height):
     super(WebThumbnailer, self).__init__()
     self.ok = None
     self.page = QWebPage(self)
     self.page.mainFrame().setScrollBarPolicy(
             Qt.Horizontal, Qt.ScrollBarAlwaysOff)
     self.page.mainFrame().setScrollBarPolicy(
             Qt.Vertical, Qt.ScrollBarAlwaysOff)
     self.page.loadStarted.connect(self.on_page_started)
     self.page.loadFinished.connect(self.on_page_finished)
     self.page.networkAccessManager().finished.connect(
             self.on_network_finished)
     self.page.setViewportSize(QSize(window_width, window_height))
class ImageRenderer(QObject):
    def __init__(self, url, image_path):
        QObject.__init__(self)
        self.url = QUrl(url)
        self.image_path = image_path
        self.webpage = QWebPage(self)
        self.webpage.loadFinished.connect(self.render)
        self.webpage.mainFrame().load(self.url)

    def render(self, ok):
        if ok:
            print('Loaded {0}'.format(self.url.toString()))
            self.webpage.setViewportSize(
                self.webpage.mainFrame().contentsSize())
            image = QImage(self.webpage.viewportSize(), QImage.Format_ARGB32)
            painter = QPainter(image)

            self.webpage.mainFrame().render(painter)
            painter.end()

            if image.save(self.image_path):
                print('Saved image to {0}'.format(self.image_path))
            else:
                print('Could not save to {0}'.format(self.image_path))
        else:
            print('Could not load {0.toString()}'.format(self.url))

        QApplication.instance().quit()
Beispiel #16
0
class MyBrowser:
    global result
    global DEBUG
    global DOMXSSTAG
    global XSSSIGN

    def __init__(self):

        self.debug = DEBUG
        self.xsssign = XSSSIGN
        self.tagsign = DOMXSSTAG
        #一些全局的东西
        self.headers = {'User-Agent' : 'Mozilla/5.0 (MSIE 9.0; Windows NT 6.1; Trident/5.0)'}
        self.timeout = 3
        #记录respones的数据
        self._load_status = None
        self.http_code = None
        #初始化webkit
        self.application = QApplication([])

    def load_url(self, url='', method='get', body='', headers={}):
        self.webpage = QWebPage()
        self.webframe = self.webpage.currentFrame()
        #document = self.webframe.documentElement().tagName()
        #print '--------------'
        #print self.webframe.documentElement().findFirstElement('script').tagName ()
        #重写alert confirm事件
        self.webpage.javaScriptAlert = self._on_javascript_alert
        self.webpage.javaScriptConfirm = self._on_javascript_confirm
        self.webpage.javaScriptConsoleMessage = self._on_javascript_consolemessage
        self.netmanager = self.webpage.networkAccessManager()
        #绑定事件
        self.netmanager.finished.connect(self._request_ended)
        self.webpage.loadFinished.connect(self._on_load_finished)
        self.webpage.loadStarted.connect(self._on_load_started)

        try:
            method = getattr(QNetworkAccessManager, "%sOperation" % method.capitalize())
        except AttributeError, e:
            if self.debug:
                my_print("[-] Error: %s" % str(e), 'yellow')

        request = QNetworkRequest(QUrl(url))
        if headers == {}:
            headers = self.headers
        for header in headers:
            request.setRawHeader(header, headers[header])

        self.webframe.load(request, method, body)
        self.wait_for()
class ImageRenderer(QObject):

    def __init__(self, url, image_path):
        QObject.__init__(self)
        self.url = QUrl(url)
        self.image_path = image_path
        self.webpage = QWebPage(self)
        self.webpage.loadFinished.connect(self.render)
        self.webpage.mainFrame().load(self.url)

    def render(self, ok):
        if ok:
            print('Loaded {0}'.format(self.url.toString()))
            self.webpage.setViewportSize(
                self.webpage.mainFrame().contentsSize())
            image = QImage(self.webpage.viewportSize(),
                           QImage.Format_ARGB32)
            painter = QPainter(image)

            self.webpage.mainFrame().render(painter)
            painter.end()

            if image.save(self.image_path):
                print('Saved image to {0}'.format(self.image_path))
            else:
                print('Could not save to {0}'.format(self.image_path))
        else:
            print('Could not load {0.toString()}'.format(self.url))

        QApplication.instance().quit()
Beispiel #18
0
class Browser(object):
    def __init__(self):
        self.network_manager = QNetworkAccessManager()
        self.network_manager.createRequest = self._create_request
        self.network_manager.finished.connect(self._request_finished)

        self.web_page = QWebPage()
        self.web_page.setNetworkAccessManager(self.network_manager)

        self.web_view = QWebView()
        self.web_view.setPage(self.web_page)

    def _create_request(self, operation, request, data):
        print(data.readAll())
        reply = QNetworkAccessManager.createRequest(self.network_manager,
                                                    operation, request, data)
        return reply

    def _request_finished(self, reply):
        if not reply.error() == QNetworkReply.NoError:
            # request probably failed
            print(reply.error())
            print(reply.errorString())

    def _make_request(self, url):
        request = QNetworkRequest()
        request.setUrl(QUrl(url))
        return request

    def _urlencode_post_data(self, post_data):
        post_params = QUrl()
        for (key, value) in post_data.items():
            post_params.addQueryItem(key, unicode(value))

        return post_params.encodedQuery()

    def perform(self, url, method='GET', post_data=dict()):
        request = self._make_request(url)

        if method == 'GET':
            self.web_view.load(request)
        else:
            encoded_data = self._urlencode_post_data(post_data)
            request.setRawHeader(
                'Content-Type',
                QByteArray('application/x-www-form-urlencoded'))
            self.web_view.load(request, QNetworkAccessManager.PostOperation,
                               encoded_data)
 def __init__(self, url, image_path):
     QObject.__init__(self)
     self.url = QUrl(url)
     self.image_path = image_path
     self.webpage = QWebPage(self)
     self.webpage.loadFinished.connect(self.render)
     self.webpage.mainFrame().load(self.url)
Beispiel #20
0
 def __init__(self):
     if Jaime.instance:
         raise Exception("Can't call to constructor with another instance created")
     
     self.tabs_widget = QTabWidget()
     self.view = QWebView()
     self.page = QWebPage()
     self.config = SafeConfigParser()        
     Logger.getLoggerFor(self.__class__)
     
     self.tabs_widget.insertTab(0,self.view,'label')
     self.tabs = {}        
     
     self.graph_file = None
     
     self.close_tab_timer = QTimer()
     self.close_tab_timer.setSingleShot(False)
     #cada 30 segundos se cierra un tab
     self.close_tab_timer.setInterval(10000)
     self.view.setPage(self.page)
     self.tabs['mainTab'] = self.view
     
     self.network_manager = CustomNetworkAccessManager.getInstance()        
     self.navigator = Navigator.getInstance()
     self.route_node = YahooRouteNode.getInstance()
     self.graph_parser = GraphParser.getInstance()        
     self.page.setNetworkAccessManager(self.network_manager)
    def __init__(self):
        super().__init__()

        self.app = QApplication(sys.argv)

        QNetworkProxyFactory.setUseSystemConfiguration(True)

        self.web_page = QWebPage(self)
        self.web_page.settings().setAttribute(QWebSettings.AutoLoadImages, False)
        self.web_page.loadFinished.connect(self.load_finished)

        # Адрес страницы объявления
        self.url = None

        # Переменная нужна для замера времени выполнения
        self.t = None

        self.phone_img_parser = AvitoPhoneImgParser()

        # Строка с номером телефона
        self.phone = None

        self.proxy_url = None
        self.proxy_type = None
        self.proxy_enabled = None
Beispiel #22
0
 def acceptNavigationRequest(self, frame, request, type):
     if(type == QWebPage.NavigationTypeLinkClicked):
         if(frame == self.mainFrame()):
             self.view().load(request.url())
         else:
             webbrowser.open(request.url().toString())
             return False
     return QWebPage.acceptNavigationRequest(self, frame, request, type)
Beispiel #23
0
class Browser:
    def __init__(self, app):
        self.network_manager = QNetworkAccessManager()
        self.network_manager.createRequest = self._create_request
        self.web_page = QWebPage()
        self.web_page.setNetworkAccessManager(self.network_manager)
        self.web_view = QWebView()
        self.web_view.setPage(self.web_page)
        self.html_data = None
        self.app = app
        self.finished = threading.Event()
        self.resultList = []

    def _create_request(self, operation, request, data):
        reply = QNetworkAccessManager.createRequest(self.network_manager,
                                                    operation, request, data)
        self.resultList.append(request.url().toString())
        return reply

    def myLoadFinish(self):
        app.quit()
Beispiel #24
0
    def load_url(self, url='', method='get', body='', headers={}):
        self.webpage = QWebPage()
        self.webframe = self.webpage.currentFrame()
        #document = self.webframe.documentElement().tagName()
        #print '--------------'
        #print self.webframe.documentElement().findFirstElement('script').tagName ()
        #重写alert confirm事件
        self.webpage.javaScriptAlert = self._on_javascript_alert
        self.webpage.javaScriptConfirm = self._on_javascript_confirm
        self.webpage.javaScriptConsoleMessage = self._on_javascript_consolemessage
        self.netmanager = self.webpage.networkAccessManager()
        #绑定事件
        self.netmanager.finished.connect(self._request_ended)
        self.webpage.loadFinished.connect(self._on_load_finished)
        self.webpage.loadStarted.connect(self._on_load_started)

        try:
            method = getattr(QNetworkAccessManager, "%sOperation" % method.capitalize())
        except AttributeError, e:
            if self.debug:
                my_print("[-] Error: %s" % str(e), 'yellow')
Beispiel #25
0
    def __init__(self, parent=None):
        super(InvoiceForm, self).__init__(parent)
        self.setupUi(self)
        self.progressBar.setHidden(True)

        self.invoice = Invoice(self)
        self.tableView.setModel(self.invoice)
        self.tableView.horizontalHeader().setResizeMode(QHeaderView.Stretch)
        self.tableView.setAlternatingRowColors(True)
        self.tableView.setSelectionBehavior(QAbstractItemView.SelectRows)

        self.updateButton.clicked.connect(self.update_invoices)
        self.tableView.selectionModel().selectionChanged.connect(self.invoice_selected)

        self.webpage = QWebPage()
        self.webpage.loadFinished.connect(self.save_invoice)
        self.webpage.networkAccessManager().sslErrors.connect(self.allow_connection)

        self.network_access_manager = QNetworkAccessManager()
        self.network_access_manager.finished.connect(self.fetched_invoices)
        self.network_access_manager.sslErrors.connect(self.allow_connection)
Beispiel #26
0
    def __init__(self, callback, logger, options=None):
        self.logger = logger

        self._request_ops = {'head': QNetworkAccessManager.HeadOperation,
                             'get': QNetworkAccessManager.GetOperation,
                             'put': QNetworkAccessManager.PutOperation,
                             'post': QNetworkAccessManager.PostOperation,
                             'delete': QNetworkAccessManager.DeleteOperation}

        self._network_manager = NetworkManager(logger)

        self._web_page = QWebPage()
        self._web_page.setNetworkAccessManager(self._network_manager)

        self._web_view = QWebView()
        self._web_view.setPage(self._web_page)

        # connect the loadFinished signal to a method defined by us.
        # loadFinished is the signal which is triggered when a page is loaded
        self._web_view.loadFinished.connect(self._load_finished)

        options = options or dict()
        settings = self._web_view.settings()
        settings.setAttribute(QWebSettings.AutoLoadImages,
                              options.pop('images', False))
        settings.setAttribute(QWebSettings.JavascriptEnabled,
                              options.pop('javascript', False))
        settings.setAttribute(QWebSettings.JavascriptCanOpenWindows,
                              options.pop('popups', False))
        settings.setAttribute(QWebSettings.PrivateBrowsingEnabled,
                              options.pop('private_browsing', False))
        settings.setAttribute(QWebSettings.JavaEnabled, False)
        settings.setAttribute(QWebSettings.PluginsEnabled, False)
        settings.setAttribute(QWebSettings.DnsPrefetchEnabled, True)

        # store the callback function which will be called when a request is
        # finished
        self._result_callback = callback
Beispiel #27
0
 def __init__ (self, user_agent, parent=None):
     QWebPage.__init__(self, parent)
     self._user_agent = user_agent
Beispiel #28
0
 def acceptNavigationRequest(self, frame, request, type):
     self._navigationRequest = True
     logger.debug('navigate %s' % WebkitConnection.toPyObject(request.url()).encode('utf-8'))
     return QWebPage.acceptNavigationRequest(self, frame, request, type)
Beispiel #29
0
class WebThumbnailer(QObject):
    finished = Signal(bool)

    def __init__(self, window_width, window_height):
        super(WebThumbnailer, self).__init__()
        self.ok = None
        self.page = QWebPage(self)
        self.page.mainFrame().setScrollBarPolicy(
                Qt.Horizontal, Qt.ScrollBarAlwaysOff)
        self.page.mainFrame().setScrollBarPolicy(
                Qt.Vertical, Qt.ScrollBarAlwaysOff)
        self.page.loadStarted.connect(self.on_page_started)
        self.page.loadFinished.connect(self.on_page_finished)
        self.page.networkAccessManager().finished.connect(
                self.on_network_finished)
        self.page.setViewportSize(QSize(window_width, window_height))

    def on_page_started(self):
        logging.debug('on_page_started')
        self.ok = None

    def on_page_finished(self, ok):
        logging.debug('on_page_finished: ok=%s', ok)
        self.ok = ok
        self.finished.emit(ok)

    def on_network_finished(self, reply):
        logging.debug('on_network_finished: %s', reply.url().toEncoded())

    def load(self, url):
        self.page.mainFrame().load(url)

    def save(self, out, width=None, height=None):
        image = self.render()
        scaled = self.scale(image, width, height)
        scaled.save(out)
        logging.debug('imagesize: %s', scaled.size())

    def render(self):
        image = QImage(self.page.viewportSize(), QImage.Format_RGB32)
        painter = QPainter(image)
        self.page.mainFrame().render(painter)
        painter.end()
        return image

    def scale(self, image, width=None, height=None):
        if width is None and height is None:
            scaled = image
        elif width is None:
            scaled = image.scaledToHeight(height, Qt.SmoothTransformation)
        elif height is None:
            scaled = image.scaledToWidth(width, Qt.SmoothTransformation)
        else:
            scaled = image.scaled(width, height,
                    Qt.KeepAspectRatioByExpanding,
                    Qt.SmoothTransformation)
            scaled = scaled.copy(0, 0, width, height)
        return scaled
Beispiel #30
0
 def userAgentForUrl(self, url):
     if self._userAgent:
         agent = self._userAgent
     else:
         agent = QWebPage.userAgentForUrl(self,url)
     return agent
Beispiel #31
0
 def testSetNetworkAccessManager(self):
     page = QWebPage()
     manager = QNetworkAccessManager()
     page.setNetworkAccessManager(manager)
Beispiel #32
0
class Jaime(QObject,Singleton):
    
    instance = None
    logger = None
    
    def __init__(self):
        if Jaime.instance:
            raise Exception("Can't call to constructor with another instance created")
        
        self.tabs_widget = QTabWidget()
        self.view = QWebView()
        self.page = QWebPage()
        self.config = SafeConfigParser()        
        Logger.getLoggerFor(self.__class__)
        
        self.tabs_widget.insertTab(0,self.view,'label')
        self.tabs = {}        
        
        self.graph_file = None
        
        self.close_tab_timer = QTimer()
        self.close_tab_timer.setSingleShot(False)
        #cada 30 segundos se cierra un tab
        self.close_tab_timer.setInterval(10000)
        self.view.setPage(self.page)
        self.tabs['mainTab'] = self.view
        
        self.network_manager = CustomNetworkAccessManager.getInstance()        
        self.navigator = Navigator.getInstance()
        self.route_node = YahooRouteNode.getInstance()
        self.graph_parser = GraphParser.getInstance()        
        self.page.setNetworkAccessManager(self.network_manager)
        
    def loadConfig(self,config_file):
        self.config.read(config_file)
    
    def loadParam(self,name,value):
        name = name.strip()
#         print 'get param [%s]' % name
        if not self.config.has_section('PARAMS'):
#             print 'cree la seccion'
            self.config.add_section('PARAMS')
            
        self.config.set('PARAMS',name.strip(),value)        
#         print 'seteo %s a %s ' %  (name,value)
    
    def getParam(self,name,default=None):
        
        name = name.strip()
#         print 'get param [%s]' % name
        if self.config.has_section('PARAMS') and \
                self.config.has_option('PARAMS',name):
#             print 'get param 1 %s' % name
            return self.config.get('PARAMS',name)
        if default != None:
            return default
        return None
    
    def toggleDelegationPolicy(self, delegate=None):
        if self.page.linkDelegationPolicy() == QWebPage.DontDelegateLinks or \
                ( isinstance(delegate,bool) and delegate ):
            self.logger.info('cambio a delegate links')            
            self.page.setLinkDelegationPolicy(QWebPage.DelegateAllLinks) 
        
        elif self.page.linkDelegationPolicy() == QWebPage.DelegateAllLinks or \
                ( isinstance(delegate,bool) and not delegate ):
            self.logger.info('cambio a dont delegate links')
            self.page.setLinkDelegationPolicy(QWebPage.DontDelegateLinks)
        
        else:
            self.logger.warn("Can't set delegation policy")

    def setGraph(self,filename):
        
        self.graph_file = filename
    
    def start(self):
        self.logger.info('---------------------------- Jaime start work ---------------------------------')                    
        self.logger.info('Graph file = %s' % self.graph_file)                    
        if self.config.has_section('PARAMS') :
            self.logger.info('[PARAMS]')                    
            for name,value in self.config.items('PARAMS'):
                self.logger.info('        %s = %s' % (name,value))
        
        self.page.setNetworkAccessManager(self.network_manager)
        self.page.loadFinished.connect(self.navigator.processPageLoadFinished)
        self.page.loadStarted.connect(self.navigator.processLoadStarted)
        self.page.linkClicked.connect(self.openLinkOnTab)
        
        self.close_tab_timer.timeout.connect(self.closeOpenTab)
        
        self.graph_parser.loadGraph(self.graph_file)
        
        if not self.navigator.takeEntryPoint():
            self.finishWork()           
            
        self.tabs_widget.show()
#         self.tabs_widget.showMaximized()
        
    def finishWork(self):
        self.logger.info('Jaime termina su funcionamiento')                    
        QApplication.closeAllWindows()
    
    def openLinkOnTab(self,link):        
        l = len(self.tabs)
        new_tab_key = 'newTab_%s' % time.time()
        self.tabs[new_tab_key] =  QWebView()
        self.tabs[new_tab_key].load(link)
        self.tabs_widget.insertTab(self.tabs_widget.count(),self.tabs[new_tab_key],new_tab_key)        
        if self.close_tab_timer.timerId() == -1  :
            self.logger.info('starteo el close_tab_timer')
            self.close_tab_timer.start()
        
    def closeOpenTab(self):
        if len(self.tabs) == 1  and self.close_tab_timer.timerId() != -1  :
            self.logger.info('stopeo el close_tab_timer')
            self.close_tab_timer.stop()
            return
        
        ks = self.tabs.keys()
        ks.remove('mainTab')
        ks.sort()
        last_key = ks[0]
        index = None
        for i in range(len(self.tabs)):
            if self.tabs_widget.tabText(i) == last_key:
                index = i
                break
        if index: 
            del self.tabs[last_key]
            self.tabs_widget.removeTab(index)
        else:
#             print 'stopeo el close_tab_timer'
            self.logger.error('no se encontro tab para remover con nombre %s' % last_key)
 def testNetWorkAccessManager(self):
     page = QWebPage()
     a = page.networkAccessManager()
Beispiel #34
0
class Browser(object):

    def __init__(self, callback, logger, options=None):
        self.logger = logger

        self._request_ops = {'head': QNetworkAccessManager.HeadOperation,
                             'get': QNetworkAccessManager.GetOperation,
                             'put': QNetworkAccessManager.PutOperation,
                             'post': QNetworkAccessManager.PostOperation,
                             'delete': QNetworkAccessManager.DeleteOperation}

        self._network_manager = NetworkManager(logger)

        self._web_page = QWebPage()
        self._web_page.setNetworkAccessManager(self._network_manager)

        self._web_view = QWebView()
        self._web_view.setPage(self._web_page)

        # connect the loadFinished signal to a method defined by us.
        # loadFinished is the signal which is triggered when a page is loaded
        self._web_view.loadFinished.connect(self._load_finished)

        options = options or dict()
        settings = self._web_view.settings()
        settings.setAttribute(QWebSettings.AutoLoadImages,
                              options.pop('images', False))
        settings.setAttribute(QWebSettings.JavascriptEnabled,
                              options.pop('javascript', False))
        settings.setAttribute(QWebSettings.JavascriptCanOpenWindows,
                              options.pop('popups', False))
        settings.setAttribute(QWebSettings.PrivateBrowsingEnabled,
                              options.pop('private_browsing', False))
        settings.setAttribute(QWebSettings.JavaEnabled, False)
        settings.setAttribute(QWebSettings.PluginsEnabled, False)
        settings.setAttribute(QWebSettings.DnsPrefetchEnabled, True)

        # store the callback function which will be called when a request is
        # finished
        self._result_callback = callback

    def _prepare_request(self, url, headers):
        # create an empty request
        request = QNetworkRequest()
        # assign a url to it
        request.setUrl(QUrl(url))

        # add some custom headers to the request
        for (header_name, header_value) in headers.items():
            request.setRawHeader(header_name, QByteArray(header_value))

        return request

    def _urlencode_request_data(self, raw_data):
        # the data which we want to send to the server must be urlencoded
        request_data = QUrl()
        for (name, value) in raw_data.items():
            request_data.addQueryItem(name, unicode(value))

        return request_data.encodedQuery()

    def _load_finished(self, ok):
        """
        Called when the page is fully loaded. It will get the html file of
        the loaded page and call the callback function with that result.
        """
        frame = self._web_view.page().mainFrame()
        url = smart_str(frame.url().toString())
        html = frame.toHtml()

        result = {'html': html,
                  'url': url,
                  'successful': ok}

        if self._network_manager.errors:
            result['errors'] = self._network_manager.errors

        # calling the callback function which we passed upon instantiation to
        # report the results there
        self._result_callback(result)

    def make(self, method, url, headers, raw_data=None):
        request = self._prepare_request(url, headers)
        operation = self._request_ops[method.lower()]
        request_data = self._urlencode_request_data(raw_data or dict())
        self._web_view.load(request, operation, request_data)
Beispiel #35
0
 def __init__(self, url, parent=None):
     QWebPage.__init__(self, parent)
     self.loadFinished.connect(self.download)
     self.urlDL = url
     # self.mainFrame().load(QUrl(url))
     self.complete = False
Beispiel #36
0
class InvoiceForm(QDialog, Ui_InvoiceDialog):

    def __init__(self, parent=None):
        super(InvoiceForm, self).__init__(parent)
        self.setupUi(self)
        self.progressBar.setHidden(True)

        self.invoice = Invoice(self)
        self.tableView.setModel(self.invoice)
        self.tableView.horizontalHeader().setResizeMode(QHeaderView.Stretch)
        self.tableView.setAlternatingRowColors(True)
        self.tableView.setSelectionBehavior(QAbstractItemView.SelectRows)

        self.updateButton.clicked.connect(self.update_invoices)
        self.tableView.selectionModel().selectionChanged.connect(self.invoice_selected)

        self.webpage = QWebPage()
        self.webpage.loadFinished.connect(self.save_invoice)
        self.webpage.networkAccessManager().sslErrors.connect(self.allow_connection)

        self.network_access_manager = QNetworkAccessManager()
        self.network_access_manager.finished.connect(self.fetched_invoices)
        self.network_access_manager.sslErrors.connect(self.allow_connection)

    def invoice_selected(self, new, old):
        try:
            row = self.invoice.invoices[new.indexes()[0].row()]
        except IndexError:
            return
        if row[1].startswith('Invoice'):
            self.invoice_filename = os.path.expanduser('~/.blusage/%s.png' % row[1].lower())
            if os.path.exists(self.invoice_filename):
                self.load_invoice()
            else:
                usage = self.parentWidget().usage_model
                url = '%s%s' % (usage.user_endpoint[:-5], self.invoice.invoices[new.indexes()[0].row()][0])
                self.webpage.mainFrame().load(QUrl(url))
                self.webpage.mainFrame().setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAlwaysOff)
                self.webpage.mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff)
                self.webpage.setViewportSize(QSize(800, 600))
                self.enable_ui(False)
        else:
            self.invoiceLabel.setText("""
<center>
    <table>
        <tr>
            <th>Payment #</th>
            <td>{number}</td>
        </tr>
        <tr>
            <th>Date</th>
            <td>{date}</td>
        </tr>
        <tr>
            <th>Credit</th>
            <td>{credit}</td>
        </tr>
        <tr>
            <th>Balance</th>
            <td>{balance}</td>
        </tr>
    </table>
</center>
            """.format(number=row[1][8:], date=row[2], credit=row[3], balance=row[5]))

    def load_invoice(self):
        pixmap = QPixmap(self.invoice_filename)
        self.invoiceLabel.setPixmap(pixmap)

    def update_invoices(self):
        usage = self.parentWidget().usage_model
        url = '%s?%s' % (usage.user_endpoint, self.invoice.post_data)
        request = QNetworkRequest(QUrl(url))
        self.network_access_manager.get(request)
        self.enable_ui(False)

    def enable_ui(self, value=True):
        self.updateButton.setEnabled(value)
        self.progressBar.setHidden(value)
        self.tableView.setEnabled(value)

    def save_invoice(self, ok):
        self.enable_ui()
        if ok:
            frame = self.webpage.mainFrame()
            image = QImage(frame.contentsSize(), QImage.Format_ARGB32_Premultiplied)
            image.fill(Qt.transparent);

            painter = QPainter(image)
            painter.setRenderHint(QPainter.Antialiasing, True);
            painter.setRenderHint(QPainter.TextAntialiasing, True);
            painter.setRenderHint(QPainter.SmoothPixmapTransform, True);
            frame.documentElement().render(painter);
            painter.end();

            image.save(self.invoice_filename)
            self.load_invoice()
        else:
            title = "An error occured"
            message = "Could not load invoice." \
                    + "\nPlease check your internet connection."

            QMessageBox.critical(self, title, message)

    def fetched_invoices(self, reply):
        self.enable_ui()
        if reply.error() == QNetworkReply.NoError:
            html = unicode(reply.readAll())
            self.invoice.parse(html)
            self.invoice.reset()
        else:
            title = "An error occured"
            message = reply.errorString() \
                    + ".\nPlease check your internet connection."

            QMessageBox.critical(self, title, message)
        reply.deleteLater()

    def allow_connection(self, reply):
        reply.ignoreSslErrors()
class AvitoAdParser(QObject):
    """Парсер страницы объявления сайта avito.ru"""

    def __init__(self):
        super().__init__()

        self.app = QApplication(sys.argv)

        QNetworkProxyFactory.setUseSystemConfiguration(True)

        self.web_page = QWebPage(self)
        self.web_page.settings().setAttribute(QWebSettings.AutoLoadImages, False)
        self.web_page.loadFinished.connect(self.load_finished)

        # Адрес страницы объявления
        self.url = None

        # Переменная нужна для замера времени выполнения
        self.t = None

        self.phone_img_parser = AvitoPhoneImgParser()

        # Строка с номером телефона
        self.phone = None

        self.proxy_url = None
        self.proxy_type = None
        self.proxy_enabled = None

    # Сигнал вызывается, когда закончен парсинг сотового из объявления
    parse_phone_finished = Signal(str)

    def run(self, url):
        """Функция выполняет парсинг страницы объявления"""

        self.url = url
        self.phone = None

        self.t = time.clock()

        # Загружаем страницу объявления
        self.web_page.mainFrame().load(url)

        logger.debug('Начало выполнения загрузки "{}" {:.3f} секунд'.format(url, time.clock() - self.t))

        # Ждем тут, пока закончится парсинг объявления -- все из-за ассинхронности webpage и моей лени -- хочется
        # просто в цикле запустить обработку и url, но из-за асинхронности с сигналами это не сработает -- какая-то
        # лажа происходит -- падает приложение с ошибкой где-то в QtCore.dll
        loop = QEventLoop()
        self.parse_phone_finished.connect(loop.quit)
        loop.exec_()

        logger.debug('Время выполнения парсера {:.3f} секунд'.format(time.clock() - self.t))
        self.t = None

    def get_phone(self):
        """Функция выполняет поиск картинки с номером телефона, получает его адрес, скачивает, а после
        парсит картинку с номером и записывает в self.phone номер телефона. После парсинга вызывает сигнал
        parse_phone_finished, в котором передается номер телефона.

        """

        # Ищем элемент с картинкой телефона
        el = self.web_page.mainFrame().findFirstElement("img[class='description__phone-img']")

        logger.debug('Поиск изображения телефона {:.3f} секунд'.format(time.clock() - self.t))

        ok = not el.isNull()
        if ok:
            src = el.attribute('src')
            logger.debug('Атрибут src картинки с телефоном: "%s".', src)
            src = src.replace('data:image/png;base64,', '')
            logger.debug('Данные картинки с телефоном: "%s".', src)
            data = base64.b64decode(src)

            # TODO: вызывать исключение, если номер не найден
            # TODO: вызывать исключение, если номер не 11 символов (нормальный номер: 89615750404)
            phone_number = self.phone_img_parser.parse_from_data(data)
            logger.debug('Телефон получен: %s', phone_number)
            logger.debug('Парсинг номера телефона из картинки {:.3f} секунд'.format(time.clock() - self.t))

            self.phone = phone_number
            self.parse_phone_finished.emit(phone_number)

    def find_and_click_element(self):
        """Функция ищет на странице объявления элемент 'Показать телефон' и кликает его, чтобы
        выполнились страшные скрипты и загрузилась картинка с номером телефона

        """

        code = """
        span_phone = $("span[class='description__phone-insert js-phone-show__insert'] span[class='btn__text']")
        span_phone.click()
        """

        logger.debug('Выполняю программный клик по кнопке "Получить телефон"')
        ok = self.web_page.mainFrame().evaluateJavaScript(code)
        if ok is None:
            logger.warn('Выполнение js скрипта неудачно. Code:\n%s', code)
            return

        logger.debug('Время выполнения js скрипта {:.3f} секунд'.format(time.clock() - self.t))
        self.get_phone()

    def load_finished(self, x):
        """Функция вызывается, когда QWebView отсылает сигнал loadFinished"""

        if x and self.phone is None:
            logger.info('Загрузка завершена {:.3f} секунд'.format(time.clock() - self.t))
            self.find_and_click_element()
Beispiel #38
0
 def testNetWorkAccessManager(self):
     page = QWebPage()
     a = page.networkAccessManager()
 def setUp(self):
     TimedQApplication.setUp(self, timeout=TIMEOUT)
     self.page = QWebPage()
     QObject.connect(self.page, SIGNAL('loadFinished(bool)'),
                     self.load_finished)
     self.called = False
class AvitoAdParser(QObject):
    """Парсер страницы объявления сайта avito.ru"""
    def __init__(self):
        super().__init__()

        self.app = QApplication(sys.argv)

        QNetworkProxyFactory.setUseSystemConfiguration(True)

        self.web_page = QWebPage(self)
        self.web_page.settings().setAttribute(QWebSettings.AutoLoadImages,
                                              False)
        self.web_page.loadFinished.connect(self.load_finished)

        # Адрес страницы объявления
        self.url = None

        # Переменная нужна для замера времени выполнения
        self.t = None

        self.phone_img_parser = AvitoPhoneImgParser()

        # Строка с номером телефона
        self.phone = None

        self.proxy_url = None
        self.proxy_type = None
        self.proxy_enabled = None

    # Сигнал вызывается, когда закончен парсинг сотового из объявления
    parse_phone_finished = Signal(str)

    def run(self, url):
        """Функция выполняет парсинг страницы объявления"""

        self.url = url
        self.phone = None

        self.t = time.clock()

        # Загружаем страницу объявления
        self.web_page.mainFrame().load(url)

        logger.debug('Начало выполнения загрузки "{}" {:.3f} секунд'.format(
            url,
            time.clock() - self.t))

        # Ждем тут, пока закончится парсинг объявления -- все из-за ассинхронности webpage и моей лени -- хочется
        # просто в цикле запустить обработку и url, но из-за асинхронности с сигналами это не сработает -- какая-то
        # лажа происходит -- падает приложение с ошибкой где-то в QtCore.dll
        loop = QEventLoop()
        self.parse_phone_finished.connect(loop.quit)
        loop.exec_()

        logger.debug(
            'Время выполнения парсера {:.3f} секунд'.format(time.clock() -
                                                            self.t))
        self.t = None

    def get_phone(self):
        """Функция выполняет поиск картинки с номером телефона, получает его адрес, скачивает, а после
        парсит картинку с номером и записывает в self.phone номер телефона. После парсинга вызывает сигнал
        parse_phone_finished, в котором передается номер телефона.

        """

        # Ищем элемент с картинкой телефона
        el = self.web_page.mainFrame().findFirstElement(
            "img[class='description__phone-img']")

        logger.debug(
            'Поиск изображения телефона {:.3f} секунд'.format(time.clock() -
                                                              self.t))

        ok = not el.isNull()
        if ok:
            src = el.attribute('src')
            logger.debug('Атрибут src картинки с телефоном: "%s".', src)
            src = src.replace('data:image/png;base64,', '')
            logger.debug('Данные картинки с телефоном: "%s".', src)
            data = base64.b64decode(src)

            # TODO: вызывать исключение, если номер не найден
            # TODO: вызывать исключение, если номер не 11 символов (нормальный номер: 89615750404)
            phone_number = self.phone_img_parser.parse_from_data(data)
            logger.debug('Телефон получен: %s', phone_number)
            logger.debug(
                'Парсинг номера телефона из картинки {:.3f} секунд'.format(
                    time.clock() - self.t))

            self.phone = phone_number
            self.parse_phone_finished.emit(phone_number)

    def find_and_click_element(self):
        """Функция ищет на странице объявления элемент 'Показать телефон' и кликает его, чтобы
        выполнились страшные скрипты и загрузилась картинка с номером телефона

        """

        code = """
        span_phone = $("span[class='description__phone-insert js-phone-show__insert'] span[class='btn__text']")
        span_phone.click()
        """

        logger.debug('Выполняю программный клик по кнопке "Получить телефон"')
        ok = self.web_page.mainFrame().evaluateJavaScript(code)
        if ok is None:
            logger.warn('Выполнение js скрипта неудачно. Code:\n%s', code)
            return

        logger.debug(
            'Время выполнения js скрипта {:.3f} секунд'.format(time.clock() -
                                                               self.t))
        self.get_phone()

    def load_finished(self, x):
        """Функция вызывается, когда QWebView отсылает сигнал loadFinished"""

        if x and self.phone is None:
            logger.info(
                'Загрузка завершена {:.3f} секунд'.format(time.clock() -
                                                          self.t))
            self.find_and_click_element()
 def testSetNetworkAccessManager(self):
     page = QWebPage()
     manager = QNetworkAccessManager()
     page.setNetworkAccessManager(manager)
Beispiel #42
0
 def __init__(self, parent = None):
     QWebPage.__init__(self, parent)
     self.connect(self, SIGNAL("printRequested(QWebFrame *)"), self.customPrintRequested)
Beispiel #43
0
 def __init__(self, parent):
     QWebPage.__init__(self, parent)  
     manager = PtNetworkAccessManager(self)
     manager.set_proxy("127.0.0.1:8888")
     self.setNetworkAccessManager(manager)
Beispiel #44
0
 def __init__(self, edit):
     QWebPage.__init__(self)
     self.current = None
     self.edit = edit
Beispiel #45
0
 def __init__(self):
     QWebPage.__init__(self)
Beispiel #46
0
 def setUp(self):
     TimedQApplication.setUp(self, timeout=TIMEOUT)
     self.page = QWebPage()
     QObject.connect(self.page, SIGNAL('loadFinished(bool)'),
                     self.load_finished)
     self.called = False