Skip to content

Instantly share code, notes, and snippets.

@qmega
Created March 6, 2015 04:05
Show Gist options
  • Save qmega/5825ce96abb06b8cf567 to your computer and use it in GitHub Desktop.
Save qmega/5825ce96abb06b8cf567 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from PyKDE4.kdeui import KWallet
from PyQt4 import QtGui
import re
from sys import argv
from os import execvp
# Path to the real mpv. You can just use 'mpv' if it's in your path,
# but then you'll get an infinite loop if this script is the first mpv
# in your path.
MPV = '/usr/bin/mpv'
SUPPORTED_SCHEMES = ['smb']
app = QtGui.QApplication([])
wallet = KWallet.Wallet.openWallet(KWallet.Wallet.LocalWallet(), 0)
def get_login_info(scheme, domain):
if not wallet.setFolder('Passwords'):
raise RuntimeError('no Passwords folder')
error, result = wallet.readMap('{}-{}:-1'.format(scheme, domain))
if error:
raise RuntimeError('readMap returned {}'.format(error))
return result
def add_login_info(urls):
logins = {}
for url in urls:
match = re.match(r'([^:]+)://([^/]+)(/.*)', url)
if match and match.group(1) in SUPPORTED_SCHEMES:
scheme, domain, path = match.groups()
try:
result = logins[scheme, domain]
except KeyError:
result = get_login_info(scheme, domain)
logins[scheme, domain] = result
if result:
url = '{0}://{1[login]}:{1[password]}@{2}{3}'.format(
scheme, result, domain, path
)
yield url
execvp(MPV, [MPV] + list(add_login_info(argv[1:])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment