Ejemplo n.º 1
0
def initProperty():
    AppProperty.AppTitle = "OSChina"
    AppProperty.AppIcon = QIcon("imgs/icon.png")
    AppProperty.HomeDir = getcwd()
    AppProperty._service_ = Service()
    AppProperty._notifications_ = NotificationPresenter()
    AppProperty.CookieJar = QNetworkCookieJar()
Ejemplo n.º 2
0
def initProperty():
    manifest = json.load(file('./view/app.json'))
    app.AppTitle = manifest['name'] if manifest['name'] else app.AppTitle
    app.AppUser = manifest['user'] if manifest['user'] else app.AppUser
    app.Debug = manifest['debug'] if manifest['debug'] else app.Debug
    app.HomeDir = getcwd()
    app.Template = manifest['win'] if manifest['win'] else app.Template
    app.App = manifest['app'] if manifest['app'] else app.App
    app.ViewDir = app.HomeDir+'/view/'+app.Template+'/'
    app.AppDir  = app.HomeDir+'/view/'+app.App+'/'
    app.AppIcon = QIcon(app.ViewDir + (manifest['ico'] if manifest['ico'] else app.AppIcon))
    #主页面配置
    app.MainSrc = manifest['main']['url'] if manifest['main']['url'] else app.MainSrc
    app.WinWidth = manifest['main']['width'] if manifest['main']['width'] else app.WinWidth
    app.WinHeight = manifest['main']['height'] if manifest['main']['height'] else app.WinHeight
    app.Opacity = manifest['opacity'] if manifest['opacity'] else app.Opacity
    #对话框配置
    app.DialogSrc = manifest['dialog']['url'] if manifest['dialog']['url'] else app.DialogSrc
    app.DialogCon = manifest['dialog']['content'] if manifest['dialog']['content'] else app.DialogCon
    app.DialogWidth = manifest['dialog']['width'] if manifest['dialog']['width'] else app.DialogWidth
    app.DialogHeight = manifest['dialog']['height'] if manifest['dialog']['height'] else app.DialogHeight
    app.DialogOpacity = manifest['dialog']['opacity'] if manifest['dialog']['opacity'] else app.DialogOpacity

    app.Music = mp3play.load('./view/winView/assets/notify.mp3')

    # 各类初始化
    app._notifications_ = NotificationPresenter()
    app.CookieJar = QNetworkCookieJar()
    app.MainWin = Window(app.MainSrc, app.WinWidth, app.WinHeight)  # 打开窗口
    app._tool_ = toolKit.ToolKit(app.MainWin)
    app._robots_ = robots()
Ejemplo n.º 3
0
    def create_page(self,
                    wait_timeout=20,
                    wait_callback=None,
                    is_popup=False,
                    max_resource_queued=None,
                    download_images=True,
                    prevent_download=[]):
        """Create a new GhostWebPage
        :param wait_timeout: The timeout used when we want to load a new url.
        :param wait_callback: An optional callable that is periodically
        executed until Ghost stops waiting.
        :param is_popup: Indicates if the QWebPage it's a popup
        :param max_resource_queued: Indicates witch it's the max number of
        resources that can be saved in memory. If None then no limits
        are applied. If 0 then no resources are kept. If the number
        it's > 0 then the number of resources won't be more than
        max_resource_queued
        :param download_images: Indicate if the browser download or not the images
        :param prevent_download: A List of extensions of the files that you want
        to prevent from downloading
        """
        cache_name = self.cache_dir if self.share_cache else self.cache_dir + str(
            random.randint(0, 100000000))
        network_manager = NetworkAccessManager(
            cache_dir=cache_name,
            cache_size=self.cache_size,
            prevent_download=prevent_download)
        if not self.share_cookies or len(self.network_managers) == 0:
            cookie_jar = QNetworkCookieJar()
            network_manager.setCookieJar(cookie_jar)
            self.cookie_jar = cookie_jar
        else:
            network_manager.setCookieJar(self.cookie_jar)

        if self.share_cookies:
            self.cookie_jar.setParent(None)
        self.network_managers.append(network_manager)

        page = GhostWebPage(app=Ghost._app,
                            network_manager=network_manager,
                            wait_timeout=wait_timeout,
                            wait_callback=wait_callback,
                            viewport_size=self.viewport_size,
                            user_agent=self.user_agent,
                            log_level=self.log_level,
                            create_page_callback=self.create_page,
                            is_popup=is_popup,
                            max_resource_queued=max_resource_queued,
                            download_images=download_images)

        page.removeWindowFromList.connect(self._remove_page)

        self._pages.append(page)

        if self.display:
            self.webview = QtWebKit.QWebView()
            self.webview.setPage(page)
            self.webview.show()

        return page, (len(self._pages) - 1)
Ejemplo n.º 4
0
    def __init__(self,
                 user_agent=default_user_agent,
                 wait_timeout=8,
                 wait_callback=None,
                 log_level=logging.WARNING,
                 display=False,
                 viewport_size=(800, 600)):
        self.http_resources = []

        self.user_agent = user_agent
        self.wait_timeout = wait_timeout
        self.wait_callback = wait_callback

        self.loaded = True

        if not sys.platform.startswith('win') and not 'DISPLAY' in os.environ\
                and not hasattr(Ghost, 'xvfb'):
            try:
                os.environ['DISPLAY'] = ':99'
                Ghost.xvfb = subprocess.Popen(['Xvfb', ':99'])
            except OSError:
                raise Exception('Xvfb is required to a ghost run oustside ' +\
                    'an X instance')

        self.display = display

        if not Ghost._app:
            Ghost._app = QApplication.instance() or QApplication(['ghost'])

        self.page = GhostWebPage(Ghost._app)
        QtWebKit.QWebSettings.setMaximumPagesInCache(0)
        QtWebKit.QWebSettings.setObjectCacheCapacities(0, 0, 0)

        self.set_viewport_size(*viewport_size)

        self.page.loadFinished.connect(self._page_loaded)
        self.page.loadStarted.connect(self._page_load_started)

        self.manager = self.page.networkAccessManager()
        self.manager.finished.connect(self._request_ended)

        self.cookie_jar = QNetworkCookieJar()
        self.manager.setCookieJar(self.cookie_jar)

        self.page.networkAccessManager().authenticationRequired\
            .connect(self._authenticate)
        self.page.networkAccessManager().proxyAuthenticationRequired\
            .connect(self._authenticate)

        self.main_frame = self.page.mainFrame()

        logger.setLevel(log_level)

        if self.display:
            self.webview = QtWebKit.QWebView()
            self.webview.setPage(self.page)
            self.webview.show()
Ejemplo n.º 5
0
 def __init__(self, supportUrl, parent=None):
     super(GeniusBarReserser, self).__init__(parent)
     self.ui = Ui_Dialog()
     self.ui.setupUi(self)
     self.supportUrl = supportUrl
     # self.ui.webView.load(QUrl('http://www.apple.com/cn/retail/shanghaiiapm/'))
     self.netWorkmgr = QNetworkAccessManager(self)
     cookieJar = QNetworkCookieJar()
     self.netWrokMgr.setCookieJar(cookieJar)
Ejemplo n.º 6
0
    def __init__(self, my_cookie_dict):
        super(QWebView, self).__init__()
        # 将字典转化成QNetworkCookieJar的格式
        self.cookie_jar = QNetworkCookieJar()
        cookies = []
        for key, values in my_cookie_dict.items():
            my_cookie = QNetworkCookie(QByteArray(key), QByteArray(values))
            #my_cookie.setDomain('.baidu.com')
            cookies.append(my_cookie)
        self.cookie_jar.setAllCookies(cookies)
        # 如果没有在前面设置domain,那么可以在这里指定一个url作为domain
        # self.cookie_jar.setCookiesFromUrl(cookies, QUrl('https://www.baidu.com/'))

        # 最后cookiejar替换完成
        self.page().networkAccessManager().setCookieJar(self.cookie_jar)
Ejemplo n.º 7
0
    def load_cookies(self, cookie_storage, keep_old=False):
        """load from cookielib's CookieJar or Set-Cookie3 format text file.

        :param cookie_storage: file location string on disk or CookieJar
            instance.
        :param keep_old: Don't reset, keep cookies not overridden.
        """
        cookie_jar = QNetworkCookieJar()
        if cookie_storage.__class__.__name__ == 'str':
            cj = LWPCookieJar(cookie_storage)
            cj.load()
            self.toQtCookieJar(cj, cookie_jar)
        elif cookie_storage.__class__.__name__.endswith('CookieJar'):
            self.toQtCookieJar(cookie_storage, cookie_jar, keep_old)
        else:
            raise ValueError('unsupported cookie_storage type.')
        return cookie_jar
Ejemplo n.º 8
0
    def __init__(self, gui=False):
        if not Kit._app:
            Kit._app = QApplication([])

        manager = KitNetworkAccessManager()
        manager.finished.connect(self.network_reply_handler)

        self.cookie_jar = QNetworkCookieJar()
        manager.setCookieJar(self.cookie_jar)

        self.page = KitPage()
        self.page.setNetworkAccessManager(manager)

        self.view = KitWebView()
        self.view.setPage(self.page)
        self.view.setApplication(Kit._app)

        if gui:
            self.view.show()
Ejemplo n.º 9
0
    def _load(self):
        self.cookie = QNetworkCookie('Settings',
                                     'Player~others|Bandwidth~ALL|Codec~ALL')
        self.cookie.setDomain('.shoutcast.com')
        self.cookie.setExpirationDate(QDateTime())
        self.cookie.setPath('/')

        self.webView = QWebView(self)
        self.webPage = self.webView.page()
        self.cookiejar = QNetworkCookieJar()
        self.cookiejar.setAllCookies([self.cookie])
        self.webPage.networkAccessManager().setCookieJar(self.cookiejar)

        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.webView)
        self.webView.load(QUrl(HOMEURL))
        self.webPage.setLinkDelegationPolicy(QWebPage.DelegateExternalLinks)
        self.connect(self.webPage, SIGNAL('linkClicked(const QUrl&)'),
                     self._processLink)
Ejemplo n.º 10
0
def initProperty():
    AppProperty.AppTitle = "QPA"
    AppProperty.AppIcon = QIcon("imgs/icon.png")
    AppProperty.HomeDir = getcwd()
    AppProperty.CookieJar = QNetworkCookieJar()
Ejemplo n.º 11
0
    def __init__(self, user_agent=default_user_agent, operate_timeout=10, loading_timeout=60, log_level=logging.WARNING,
                 display=False, viewport_size=(1024, 768), accept_language='en,*', ignore_ssl_errors=True,
                 cache_dir=os.path.join(tempfile.gettempdir(), "GRobot"),
                 image_enabled=True, plugins_enabled=False, java_enabled=False, javascript_enabled=True,
                 plugin_path=None, develop=False, proxy=None, sleep=0.5, jquery_namespace='GRobot'):
        """GRobot manages a QWebPage.
    
        @param user_agent: The default User-Agent header.
        @param operate_timeout: Operation timeout.
        @param loading_timeout: The page loading timeout.
        @param log_level: The optional logging level.
        @param display: A boolean that tells GRobot to displays UI.
        @param viewport_size: A tupple that sets initial viewport size.
        @param accept_language: Set the webkit accept language. 
        @param ignore_ssl_errors: A boolean that forces ignore ssl errors.
        @param cache_dir: A directory path where to store cache datas.
        @param image_enabled: Enable images.
        @param plugins_enabled: Enable plugins (like Flash).
        @param java_enabled: Enable Java JRE.
        @param javascript_enabled: Enable Javascript.
        @param plugin_path: Array with paths to plugin directories (default ['/usr/lib/mozilla/plugins'])
        @param develop: Enable the Webkit Inspector.
        @param proxy: Set a Socks5,HTTP{S} Proxy
        @param sleep: Sleep `sleep` second,after operate
        @param jquery_namespace: Set the jQuery namespace.
        """

        GRobot.exit_lock.acquire()

        if GRobot._kill_loop:
            gevent.kill(GRobot._kill_loop)
            GRobot._kill_loop=None

        logger.setLevel(log_level)

        plugin_path = plugin_path or ['/usr/lib/mozilla/plugins', ]

        GRobot._liveRobot += 1

        self.develop = develop
        self.inspector = None
        self.plugin = False
        self.exitLoop = False
        self._deleted = False

        self.set_proxy(proxy)

        self.sleep = sleep
        self.jquery_namespace = jquery_namespace
        self.popup_messages = None
        self.accept_language = accept_language

        self._loaded = True

        self._confirm_expected = None
        self._prompt_expected = None
        self._upload_file = None
        self._alert = None

        self.http_resources = []

        self.user_agent = user_agent

        self.loading_timeout = loading_timeout
        self.operate_timeout = operate_timeout

        self.ignore_ssl_errors = ignore_ssl_errors

        if not sys.platform.startswith('win') and not 'DISPLAY' in os.environ \
            and not hasattr(GRobot, 'xvfb'):
            try:
                os.environ['DISPLAY'] = ':99'
                GRobot.xvfb = subprocess.Popen(['Xvfb', ':99'])
            except OSError:
                raise Exception('Xvfb is required to a GRobot run oustside ' + \
                                'an X instance')

        self.display = display

        if not GRobot._app:
            GRobot._app = QApplication.instance() or QApplication(['GRobot'])
            if plugin_path:
                for p in plugin_path:
                    GRobot._app.addLibraryPath(p)

        self.page = GRobotWebPage(self, GRobot._app)

        QtWebKit.QWebSettings.setMaximumPagesInCache(0)
        QtWebKit.QWebSettings.setObjectCacheCapacities(0, 0, 0)
        QtWebKit.QWebSettings.globalSettings().setAttribute(QtWebKit.QWebSettings.LocalStorageEnabled, True)

        self.page.setForwardUnsupportedContent(True)

        # Page signals
        self.page.loadFinished.connect(self._page_loaded)
        self.page.loadStarted.connect(self._page_load_started)
        self.page.unsupportedContent.connect(self._unsupported_content)

        self.manager = self.page.networkAccessManager()

        #TODO:Think about how to handle the network accessible signal
        #self.manager.networkAccessibleChanged.connect()

        self.manager.finished.connect(self._request_ended)
        self.manager.sslErrors.connect(self._on_manager_ssl_errors)

        # Cache
        self.cache = QNetworkDiskCache()
        self.cache.setCacheDirectory(cache_dir)

        self.manager.setCache(self.cache)

        # Cookie jar
        self.cookie_jar = QNetworkCookieJar()
        self.manager.setCookieJar(self.cookie_jar)

        # User Agent
        self.page.setUserAgent(self.user_agent)

        self.page.networkAccessManager().authenticationRequired \
            .connect(self._authenticate)
        self.page.networkAccessManager().proxyAuthenticationRequired \
            .connect(self._authenticate)

        self.main_frame = self.page.mainFrame()

        self.webview = None

        self.viewport_size = viewport_size

        self.webview = QtWebKit.QWebView()
        self.webview.setPage(self.page)

        self.webview.show() if display else self.webview.hide()

        self.set_viewport_size(*viewport_size)

        self.page.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled, plugins_enabled)
        self.page.settings().setAttribute(QtWebKit.QWebSettings.JavaEnabled, java_enabled)
        self.page.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, self.develop)

        self.enable_image = image_enabled
        self.enable_javascript = javascript_enabled

        #always open link in current window instead of new window
        self.page.setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
        self.page.linkClicked.connect(self._link_clicked)

        #start the qt main loop
        GRobot._loop = QtMainLoop(GRobot._app)
        GRobot._loop.start()
        GRobot.exit_lock.release()
Ejemplo n.º 12
0
 def _handle_request_cookies(self, request):
     jar = QNetworkCookieJar()
     self.setCookieJar(jar)
     cookiejar = self._getWebPageAttribute(request, "cookiejar")
     if cookiejar is not None:
         cookiejar.update_cookie_header(request)
Ejemplo n.º 13
0
    def __init__(self,
                 user_agent=default_user_agent,
                 wait_timeout=8,
                 wait_callback=None,
                 log_level=logging.WARNING,
                 display=False,
                 viewport_size=(800, 600),
                 ignore_ssl_errors=True,
                 cache_dir=os.path.join(tempfile.gettempdir(), "ghost.py"),
                 plugins_enabled=False,
                 java_enabled=False,
                 plugin_path=[
                     '/usr/lib/mozilla/plugins',
                 ],
                 download_images=True,
                 qt_debug=False):
        self.http_resources = []

        self.user_agent = user_agent
        self.wait_timeout = wait_timeout
        self.wait_callback = wait_callback
        self.ignore_ssl_errors = ignore_ssl_errors
        self.loaded = True

        if not sys.platform.startswith('win') and not 'DISPLAY' in os.environ\
                and not hasattr(Ghost, 'xvfb'):
            try:
                os.environ['DISPLAY'] = ':99'
                Ghost.xvfb = subprocess.Popen(['Xvfb', ':99'])
            except OSError:
                raise Error('Xvfb is required to a ghost run outside ' +
                            'an X instance')

        self.display = display

        if not Ghost._app:
            Ghost._app = QApplication.instance() or QApplication(['ghost'])
            qInstallMsgHandler(QTMessageProxy(qt_debug))
            if plugin_path:
                for p in plugin_path:
                    Ghost._app.addLibraryPath(p)

        self.popup_messages = []
        self.page = GhostWebPage(Ghost._app, self)
        QtWebKit.QWebSettings.setMaximumPagesInCache(0)
        QtWebKit.QWebSettings.setObjectCacheCapacities(0, 0, 0)
        QtWebKit.QWebSettings.globalSettings().setAttribute(
            QtWebKit.QWebSettings.LocalStorageEnabled, True)

        self.page.setForwardUnsupportedContent(True)
        self.page.settings().setAttribute(QtWebKit.QWebSettings.AutoLoadImages,
                                          download_images)
        self.page.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled,
                                          plugins_enabled)
        self.page.settings().setAttribute(QtWebKit.QWebSettings.JavaEnabled,
                                          java_enabled)

        self.set_viewport_size(*viewport_size)

        # Page signals
        self.page.loadFinished.connect(self._page_loaded)
        self.page.loadStarted.connect(self._page_load_started)
        self.page.unsupportedContent.connect(self._unsupported_content)

        self.manager = self.page.networkAccessManager()
        self.manager.finished.connect(self._request_ended)
        self.manager.sslErrors.connect(self._on_manager_ssl_errors)
        # Cache
        self.cache = QNetworkDiskCache()
        self.cache.setCacheDirectory(cache_dir)
        self.manager.setCache(self.cache)
        # Cookie jar
        self.cookie_jar = QNetworkCookieJar()
        self.manager.setCookieJar(self.cookie_jar)
        # User Agent
        self.page.setUserAgent(self.user_agent)

        self.page.networkAccessManager().authenticationRequired\
            .connect(self._authenticate)
        self.page.networkAccessManager().proxyAuthenticationRequired\
            .connect(self._authenticate)

        self.main_frame = self.page.mainFrame()

        logger.setLevel(log_level)

        if self.display:

            class MyQWebView(QtWebKit.QWebView):
                def sizeHint(self):
                    return QSize(*viewport_size)

            self.webview = MyQWebView()
            if plugins_enabled:
                self.webview.settings().setAttribute(
                    QtWebKit.QWebSettings.PluginsEnabled, True)
            if java_enabled:
                self.webview.settings().setAttribute(
                    QtWebKit.QWebSettings.JavaEnabled, True)
            self.webview.setPage(self.page)
            self.webview.show()
        else:
            self.webview = None
Ejemplo n.º 14
0
#! /usr/bin/env python
# encoding: utf-8

import sys
import signal
import urllib
# from PyQt4.QtCore import *
# from PyQt4.QtGui import *
from PyQt4.QtCore import QUrl
from PyQt4.QtCore import SIGNAL
from PyQt4.QtGui import QApplication
from PyQt4.QtNetwork import QNetworkCookieJar, QNetworkCookie
from PyQt4.QtWebKit import QWebPage

_cookieJar = QNetworkCookieJar()
_cookie = 'a=m;\nb=1;\nc=test'


def user_agent(url=""):
    print url
    return "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) " \
           "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36"


def alert(frame, message):
    print message
    pass


def console_message(s1, i, s2):
    pass