From 548e0a9821e44ef08458cbed2f2150f0c9967c7e Mon Sep 17 00:00:00 2001 From: Bas Stottelaar Date: Wed, 12 Nov 2014 18:49:57 +0100 Subject: [PATCH] Use urlparse to parse transmission URL --- headphones/notifiers.py | 7 +---- headphones/transmission.py | 56 ++++++++++++++++++-------------------- 2 files changed, 27 insertions(+), 36 deletions(-) diff --git a/headphones/notifiers.py b/headphones/notifiers.py index 953b7d5f..18f5227a 100644 --- a/headphones/notifiers.py +++ b/headphones/notifiers.py @@ -17,6 +17,7 @@ from headphones import logger, helpers, common, request from xml.dom import minidom from httplib import HTTPSConnection +from urlparse import parse_qsl from urllib import urlencode from pynma import pynma @@ -33,12 +34,6 @@ import json import oauth2 as oauth import pythontwitter as twitter -try: - from urlparse import parse_qsl -except ImportError: - from cgi import parse_qsl - - class GROWL(object): """ Growl notifications, for OS X. diff --git a/headphones/transmission.py b/headphones/transmission.py index 73a6ea3b..2529a6a5 100644 --- a/headphones/transmission.py +++ b/headphones/transmission.py @@ -18,6 +18,7 @@ from headphones import logger, request import time import json import base64 +import urlparse import headphones # This is just a simple script to send torrents to transmission. The @@ -126,7 +127,6 @@ def torrentAction(method, arguments): host = headphones.CONFIG.TRANSMISSION_HOST username = headphones.CONFIG.TRANSMISSION_USERNAME password = headphones.CONFIG.TRANSMISSION_PASSWORD - sessionid = None if not host.startswith('http'): host = 'http://' + host @@ -134,30 +134,22 @@ def torrentAction(method, arguments): if host.endswith('/'): host = host[:-1] - # Either the host ends with a port, or some directory, or rpc - # If it ends with /rpc we don't have to do anything - # If it ends with a port we add /transmission/rpc - # anything else we just add rpc - if not host.endswith('/rpc'): - # Check if it ends in a port number - i = host.rfind(':') - if i >= 0: - possible_port = host[i + 1:] - try: - port = int(possible_port) - if port: - host = host + "/transmission/rpc" - except ValueError: - host = host + "/rpc" - logger.debug('No port, assuming not transmission') - else: - logger.error('Transmission port missing') - return + # Fix the URL. We assume that the user does not point to the RPC endpoint, + # so add it if it is missing. + parts = list(urlparse.urlparse(host)) + + if not parts[0] in ("http", "https"): + parts[0] = "http" + + if not parts[2].endswith("/rpc"): + parts[2] += "/transmission/rpc" + + host = urlparse.urlunparse(parts) # Retrieve session id auth = (username, password) if username and password else None - - response = request.request_response(host, auth=auth, whitelist_status_code=[401, 409]) + response = request.request_response(host, auth=auth, + whitelist_status_code=[401, 409]) if response is None: logger.error("Error gettings Transmission session ID") @@ -166,26 +158,30 @@ def torrentAction(method, arguments): # Parse response if response.status_code == 401: if auth: - logger.error("Username and/or password not accepted by Transmission") + logger.error("Username and/or password not accepted by " \ + "Transmission") else: logger.error("Transmission authorization required") return elif response.status_code == 409: - sessionid = response.headers['x-transmission-session-id'] + session_id = response.headers['x-transmission-session-id'] - if not sessionid: - logger.error("Error getting Session ID from Transmission") - return + if not session_id: + logger.error("Expected a Session ID from Transmission") + return # Prepare next request - headers = {'x-transmission-session-id': sessionid} + headers = {'x-transmission-session-id': session_id} data = {'method': method, 'arguments': arguments} - response = request.request_json(host, method="post", data=json.dumps(data), headers=headers, auth=auth) + response = request.request_json(host, method="POST", data=json.dumps(data), + headers=headers, auth=auth) + + print response if not response: logger.error("Error sending torrent to Transmission") return - return response + return response \ No newline at end of file