Skip to content

Instantly share code, notes, and snippets.

@tdfischer
Last active August 29, 2015 13:56
Show Gist options
  • Save tdfischer/8875480 to your computer and use it in GitHub Desktop.
Save tdfischer/8875480 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from PyQt4 import QtCore, QtWebKit, QtGui
import sys
import random
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
layout = QtGui.QVBoxLayout(window)
addressBar = QtGui.QLineEdit(window)
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."
nextLinkTimer.stop()
webview.setUrl(QtCore.QUrl(random.choice(startPages)))
updateUrl()
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