Enable secure playback, Widevine DRM in python webbrowsers (Protected content) - python

I'm trying to use the spotify web player on a python browser windows.
I try it in two ways:
import webview
webview.create_window('spotify', 'https://open.spotify.com')
webview.start()
import sys
import time
from PyQt5.Qt import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
web = QWebEngineView()
web.load(QUrl("https://open.spotify.com/"))
web.showFullScreen()
sys.exit(app.exec_())
After I log in with my account I get:
Enable secure playback in your browser.
Is there way I can do this? In the spotify support page it tells me to enable protected content playback or play drm. I am not sure if it is possible.

By #metatoaster 's suggestion, I tried to set the QTWEBENGINE_CHROMIUM_FLAGS environment variable to --widevine-path="/home/<myusername>/.mozilla/firefox/237oaqbb.default-release/gmp-widevinecdm/4.10.2449.0/libwidevinecdm.so"
After I got the location from locate libwidevinecdm.so, and it worked!
I tried with the spotify's path for the library but that didn't work.

Related

QWebEngineView and ignoring cert errors

I am trying to understand how this works and am struggling to figure out how to use it. The only examples I can find are not in Python and apparently I'm not that good at translating.
I've been digging through the help() results for most of these modules but am still not able to figure out how they work. Now if I read it right this should be able to be used to ignore a certificate error when loading a page.
QWebEngineCertificateError.ignoreCertificateError()
But when I try to run this I get the following error. I am pretty sure I am using it wrong but I can't find a good example of how its supposed to work.
TypeError: ignoreCertificateError(self): first argument of unbound method must have type 'QWebEngineCertificateError'
In a normal browser when you run into a cert error like this "ERR_CERT_AUTHORITY_INVALID" you can choose to proceed anyway. That option doesn't seem to be a default feature of QWebEngineView. What I am trying to do is implement that or to have it automatically just ignore the error and proceed.
Does anyone out there understand how to do this and are you willing to give me a pointer in the right direction? I'm stumped. I tried to get around the problem by just embedding a Chrome or edge browser into the app but it won't let my type in the browser though I can click on things and right click.
Here is an example code opening a website that triggers the same error. Its not what I need to load but its a site that triggers the same error.
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
import sys
app = QApplication(sys.argv)
webview = QWebEngineView()
webview.load(QUrl("https://www.us.army.mil/"))
webview.show()
sys.exit(app.exec_())
screenshot of the error:
You have to override the certificateError() method of QWebEnginePage:
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineView
class WebEnginePage(QWebEnginePage):
def certificateError(self, error):
# If you want to ignore the certificates of certain pages
# then do something like
# if error.url() == QUrl("https://www.us.army.mil/"):
# error.ignoreCertificateError()
# return True
# return super().certificateError(error)
error.ignoreCertificateError()
return True
def main(args):
app = QApplication(args)
webview = QWebEngineView()
page = WebEnginePage()
webview.setPage(page)
webview.load(QUrl("https://www.us.army.mil/"))
webview.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main(sys.argv)
```

Pyside WebView example showing blank screen

I'm starting to play with QWebView in PySide.
I can load HTML into the web view and view it, however I can't seem to load online pages or set urls. Here is the example I've got:
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.setUrl(QUrl("https://www.google.com"))
web.show()
sys.exit(app.exec_())
All I get from this is a nice blank white window. Anyone know what's missing?
Maybe you are behind a proxy.
I've experienced the same behavior and setting a Proxy for the Application helped.
from PySide.QtNetwork import QNetworkProxy
proxy = QNetworkProxy()
proxy.setType(QNetworkProxy.Socks5Proxy);
proxy.setType(QNetworkProxy.HttpProxy);
proxy.setHostName("*********");
proxy.setPort(****);
QNetworkProxy.setApplicationProxy(proxy);

Is it possible to get QWebKit to display pdf files?

i have a link in my QWebkit, which points to a pdf file.
But when the link is clicked, it can't display the pdf file.
is there a way to make it happen?
If you enable plugins through QWebSettings and have a PDF viewer installed that provides a browser plugin such as Acrobat then you should see the PDF rendered using the plugin inside your QWebView:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.settings().setAttribute(QWebSettings.PluginsEnabled, True)
web.show()
web.load(QUrl('file:///C:/test/test.pdf')) # Change path to actual file.
sys.exit(app.exec_())
This code isn't working for me on Windows with the latest version of Acrobat X (it just shows a progress bar but no PDF - proof that the plugin is loading, just not working) but I'm sure this is how I've done it before. Give it a try and let me know.
Webkit doesn't include a PDF viewer. You'll need to have some way of rendering it - whether you pass it off to a different viewer (Adobe PDF viewer or something else), render it in the control in some way you devise (you could even try rendering it in JavaScript for fun).
This is Gary Hudges code for PyQt5:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtWebKitWidgets import *
from PyQt5.QtWebKit import *
from PyQt5.QtCore import *
app = QApplication(sys.argv)
web = QWebView()
web.settings().setAttribute(QWebSettings.PluginsEnabled, True)
web.show()
web.load(QUrl('file:///C:/data/progetti_miei/python/test.pdf')) # Change path to actual file.
sys.exit(app.exec_())

PyQt Web Browser: Focus on the webpage without a mouse click

I am writing an application for an embedded device that has no mouse input. The web browser is very very basic and does not have any buttons, address bar, file bar , etc. For now, it simply just loads a web page. This web page uses javascript to catch key press events for actions.
The problem I am having is that when the browser loads, the key presses are not caught. I have tracked this problem down to what I believe is a focus problem. When the browser loads, it does not have focus until a mouse click occurs on the application. Since I do not have a mouse, that initial click cannot happen.
How can I make sure that the browser application gets focused correctly such that when I launch it from a terminal or script it will gain immediate focus and the key events can happen accordingly?
My code is as follows:
#!/usr/bin/env python
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.showFullScreen()
web.load(QUrl("http://www.google.com"))
sys.exit(app.exec_())
The QWidget.setFocus() did not work, assuming I used it correctly. Any help is appreciated. Thanks
I tried your code on my laptop and the QWebView had focus already - once Google had loaded I could type and my text appeared in the text box.
If it is a focus problem then as QWebView inherits QWidget you can call its setFocus() method. Here is your code with an extra line calling the QWebView's setFocus method once a page has loaded:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.loadFinished.connect(web.setFocus)
web.showFullScreen()
web.load(QUrl("http://www.google.com/"))
sys.exit(app.exec_())
You can set focus on the main frame:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
if __name__ == '__main__':
app = QApplication(sys.argv)
web = QWebView()
frame = web.page().mainFrame()
frame.setFocus()
web.showFullScreen()
web.load(QUrl("http://www.google.com"))
sys.exit(app.exec_())

Website to image

I'm running Python 3.1 and you would call me an advanced novice :)
My question is simple: I'm trying to make a simple program which asks the users for a URL (or multiple URLs) and then goes to the website and takes a screenshot (of the whole page, not just what can be seen in the browser without scrolling all the way down).
It's simpler then it sounds, I want to use an existing platform on the web, similar to this:
import subprocess
MYFILENAME = "google_screen"
MYURL = "www.google.com"
subprocess.Popen(['wget', '-O', MYFILENAME+'.png', 'http://images.websnapr.com/?url='+MYURL+'&size=s&nocache=82']).wait()
Although this website does not work :(, I'm wondering is it possible to do it with this website and if so, how? If it is not possible, are there any alternatives?
There is a package called webkit2png that you can use for this, its located: here
More information on this blog post
Example from blog post(copied to SO for preservation, read the blog post to understand it if you have issues):
#!/usr/bin/env python
import sys
import signal
from PyQt4.QtCore import *
from PyQt 4.QtGui import *
from PyQt4.QtWebKit import QWebPage
def onLoadFinished(result):
if not result:
print "Request failed"
sys.exit(1)
# Set the size of the (virtual) browser window
webpage.setViewportSize(webpage.mainFrame().contentsSize())
# Paint this frame into an image
image = QImage(webpage.viewportSize(), QImage.Format_ARGB32)
painter = QPainter(image)
webpage.mainFrame().render(painter)
painter.end()
image.save("output2.png")
sys.exit(0)
app = QApplication(sys.argv)
signal.signal(signal.SIGINT, signal.SIG_DFL)
webpage = QWebPage()
webpage.connect(webpage, SIGNAL("loadFinished(bool)"), onLoadFinished)
webpage.mainFrame().load(QUrl("http://www.google.com"))
sys.exit(app.exec_())
Edit: Link to the pyqt4 download page
You can use Selenium to get a screenshot, but it'll only be what is viewed by the browser.

Categories

Resources