Skip to content

Instantly share code, notes, and snippets.

@targi
Created March 15, 2014 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save targi/9575081 to your computer and use it in GitHub Desktop.
Save targi/9575081 to your computer and use it in GitHub Desktop.
An Open In handler for Pythonista that uploads the file which name was passed to the configured server via SFTP, then runs the configured command on that server (e.g., transmission add).
import console
import paramiko
import keychain
import traceback
from os.path import basename
# TransmissionClient
#
# An Open In handler that uploads the file which name was passed
# to the configured server via SFTP, then runs the configured
# command on that server (e.g., transmission add).
#
# To start using, configure the constants below, then run
# the script once to save the password in the keychain.
# Then, in the Open In script, run
# TransmissionClient.upload(torrent_file_path)
# Configuration
HOST = 'server'
USERNAME = 'oleg' # $1 below is replaced with torrent file basename
ADD_CMD = './transmission-remote-cli.py -f .trclirc -- -a "$1"'
CLEAN_CMD = 'rm "$1"'
def open_connection():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
password = keychain.get_password('ssh:' + HOST, USERNAME)
console.set_color(0, 0, 1)
print 'connecting to %s@%s...' % (USERNAME, HOST)
console.set_color(0, 0, 0)
ssh.connect(HOST, username=USERNAME, password=password)
return ssh
def run_cmd(ssh, cmd):
console.set_color(0, 0, 1)
print '[remote] $ %s' % cmd
console.set_color(0, 0, 0)
stdin, stdout, stderr = ssh.exec_command(cmd)
data = stdout.read()
print data
def do_upload(ssh, torrent_filename):
bn = basename(torrent_filename)
ftp = ssh.open_sftp()
console.set_color(0, 0, 1)
print 'uploading ' + bn
console.set_color(0, 0, 0)
ftp.put(torrent_filename, bn)
ftp.close()
return bn
def upload(torrent_filename):
ssh = None
try:
console.show_activity()
ssh = open_connection()
bn = do_upload(ssh, torrent_filename)
run_cmd(ssh, ADD_CMD.replace('$1', bn))
run_cmd(ssh, CLEAN_CMD.replace('$1', bn))
console.hud_alert(bn + ' uploaded and started')
except Exception as ex:
console.set_color(1, 0, 0)
traceback.print_exc()
console.set_color(0, 0, 0)
console.alert('Torrent Upload Failed', str(ex))
finally:
if ssh:
ssh.close()
console.hide_activity()
def add_to_keychain():
try:
password = console.password_alert(
'Configure Connection Password',
'Please enter the password for %s@%s.\n\n' % (USERNAME, HOST) +
'The password will be saved in the keychain and used to ' +
'connect to this server via SSH.')
keychain.set_password('ssh:' + HOST, USERNAME, password)
console.show_activity()
console.hud_alert('Saved, now verifying')
ssh = open_connection()
ssh.close()
console.hud_alert('Password is correct')
except KeyboardInterrupt:
# user cancelled the operation
pass
except (paramiko.AuthenticationException, paramiko.SSHException) as ex:
console.alert(
'Password Incorrect',
'Cannot connect to the server using the provided password.\n' + str(ex))
finally:
console.hide_activity()
if __name__ == '__main__':
add_to_keychain()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment