Added headphones authorization, fixed a few default values in musicbrainzngs, changed user agent for headphones. Much thanks to diskir for doing all the heavy lifting on this

This commit is contained in:
rembo10
2012-05-28 23:03:27 +05:30
parent 2171355397
commit 4b72f9f6a8
2 changed files with 24 additions and 6 deletions
+8 -5
View File
@@ -8,6 +8,7 @@ from headphones import logger, db
from headphones.helpers import multikeysort, replace_all
import lib.musicbrainzngs as musicbrainzngs
from lib.musicbrainzngs import WebServiceError
mb_lock = threading.Lock()
@@ -42,17 +43,19 @@ def startmb(forcemb=False):
mbport = 5000
sleepytime = 0
musicbrainzngs.set_useragent("headphones","0.0","https://github.com/doskir/headphones")
musicbrainzngs.set_useragent("headphones","0.0","https://github.com/rembo10/headphones")
musicbrainzngs.set_hostname(mbhost + ":" + str(mbport))
if sleepytime == 0:
musicbrainzngs.set_rate_limit(False)
else:
musicbrainzngs.set_rate_limit(True)
#CHECK THIS
if mbuser and mbpass:#i have no idea if this will work or not
musicbrainzngs.auth(mbuser,mbpass)
#CHECK THIS
# Add headphones credentials
if headphones.MIRROR == "headphones":
if not mbuser and mbpass:
logger.warn("No username or password set for VIP server")
else:
musicbrainzngs.hpauth(mbuser,mbpass)
#q = musicbrainzngs
q = musicbrainzngs
+16 -1
View File
@@ -10,6 +10,7 @@ import logging
import socket
import xml.etree.ElementTree as etree
from xml.parsers import expat
import base64
from lib.musicbrainzngs import mbxml
from lib.musicbrainzngs import util
@@ -228,6 +229,14 @@ def auth(u, p):
global user, password
user = u
password = p
def hpauth(u, p):
"""Set the username and password to be used in subsequent queries to
the MusicBrainz XML API that require authentication.
"""
global hpuser, hppassword
hpuser = u
hppassword = p
def set_useragent(app, version, contact=None):
"""Set the User-Agent to be used for requests to the MusicBrainz webservice.
@@ -357,7 +366,7 @@ class _MusicbrainzHttpRequest(compat.Request):
# Core (internal) functions for calling the MB API.
def _safe_open(opener, req, body=None, max_retries=8, retry_delay_delta=2.0):
def _safe_open(opener, req, body=None, max_retries=3, retry_delay_delta=2.0):
"""Open an HTTP request with a given URL opener and (optionally) a
request body. Transient errors lead to retries. Permanent errors
and repeated errors are translated into a small set of handleable
@@ -477,6 +486,12 @@ def _mb_request(path, method='GET', auth_required=False, client_required=False,
# Make request.
req = _MusicbrainzHttpRequest(method, url, data)
req.add_header('User-Agent', _useragent)
# Add headphones credentials
if hostname == '178.63.142.150:8181':
base64string = base64.encodestring('%s:%s' % (hpuser, hppassword)).replace('\n', '')
req.add_header("Authorization", "Basic %s" % base64string)
_log.debug("requesting with UA %s" % _useragent)
if body:
req.add_header('Content-Type', 'application/xml; charset=UTF-8')