Browse Source

Added PySide support to --authbrowser and updated webkit_qt for Python3

Aaron Gokaslan 9 years ago
parent
commit
6fbc22e3a9
1 changed files with 16 additions and 6 deletions
  1. 16 6
      youtube_upload/auth/webkit_qt.py

+ 16 - 6
youtube_upload/auth/webkit_qt.py

@@ -1,5 +1,8 @@
-from PyQt4 import QtCore, QtGui, QtWebKit
-
+try:
+    from PyQt4 import QtCore, QtGui, QtWebKit
+except ImportError:
+    from PySide import QtCore, QtGui, QtWebKit
+    
 CHECK_AUTH_JS = """
     var code = document.getElementById("code");
     var access_denied = document.getElementById("access_denied");
@@ -16,12 +19,19 @@ CHECK_AUTH_JS = """
 """
 
 def _on_qt_page_load_finished(dialog, webview):
-    to_s = lambda x: (str(x.toUtf8()) if isinstance(x, QtCore.QString) else x)
+    to_s = lambda x: (str(x.toUtf8()) if hasattr(x,'toUtf8') else x)
     frame = webview.page().currentFrame()
-    jscode = QtCore.QString(CHECK_AUTH_JS)
+    try: #PySide does not QStrings
+        from QtCore import QString
+        jscode = QString(CHECK_AUTH_JS)
+    except ImportError:
+        jscode = CHECK_AUTH_JS
     res = frame.evaluateJavaScript(jscode)
-    authorization = dict((to_s(k), to_s(v)) for (k, v) in res.toPyObject().items())
-    if authorization.has_key("authorized"):
+    try:
+        authorization = dict((to_s(k), to_s(v)) for (k, v) in res.toPyObject().items())
+    except AttributeError: #PySide returns the result in pure Python
+        authorization = dict((to_s(k), to_s(v)) for (k, v) in res.items())
+    if "authorized" in authorization:
         dialog.authorization_code = authorization.get("code")
         dialog.close()