Created
July 20, 2014 08:14
-
-
Save tdfischer/e9708c5316094a22cd5a to your computer and use it in GitHub Desktop.
self browsing webbrowser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from PyQt4 import QtCore, QtWebKit, QtGui, QtDeclarative | |
import sys | |
import random | |
app = QtGui.QApplication(sys.argv) | |
view = QtDeclarative.QDeclarativeView(QtCore.QUrl("browser.qml")) | |
view.show() | |
addressBar = QtGui.QLineEdit(window) | |
addressWidget = QtGui.QGraphicsProxyWidget(addressBar) | |
webview = QtWebKit.QWebView(window) | |
layout.addWidget(addressBar) | |
layout.addWidget(webview) | |
QtWebKit.QWebSettings.globalSettings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled, False) | |
window.show() | |
resetTimer = QtCore.QTimer() | |
nextLinkTimer = QtCore.QTimer() | |
startPages = [ | |
"http://en.wikipedia.org/", | |
"http://everything2.com/", | |
"http://reddit.com/r/all", | |
"http://flickr.com", | |
"http://pintrest.com", | |
"http://twitter.com", | |
"http://amazon.com", | |
"http://noisebridge.net", | |
"http://synhak.org", | |
] | |
lasthost = None | |
hostPages = 0 | |
def restartBrowser(): | |
print "GOT BORED. RESTARTING." | |
webview.setUrl(QtCore.QUrl(random.choice(startPages))) | |
def nextPage(): | |
global hostPages | |
links = webview.page().mainFrame().findAllElements("a[href]") | |
hostPages += 1 | |
if len(links): | |
print "HEY THIS LOOKS COOL *click*" | |
link = random.choice(links) | |
link.evaluateJavaScript("this.click()") | |
nextLinkTimer.start(3000) | |
else: | |
nextLinkTimer.start(1000) | |
def updateUrl(): | |
global hostPages, lasthost | |
uri = webview.url() | |
addressBar.setText(uri.toString()) | |
print "browsing", uri.toString(), hostPages | |
resetTimer.start(10000) | |
if hostPages >= 100: | |
print "I SAW", hostPages, "PAGES FROM THIS HOST." | |
hostPages = 0 | |
QtCore.QTimer.singleShot(0, restartBrowser) | |
if uri.host() == lasthost: | |
hostPages += 1 | |
else: | |
lasthost = uri.host() | |
hostPages = 0 | |
webview.page().loadFinished.connect(nextPage) | |
webview.urlChanged.connect(updateUrl) | |
resetTimer.timeout.connect(restartBrowser) | |
nextLinkTimer.timeout.connect(nextPage) | |
restartBrowser() | |
app.exec_() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment