Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes for Python 3 #16

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LMSTools/artworkresolver.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from urllib import urlencode
from urllib.parse import urlencode

class LMSArtworkResolver(object):
"""
Expand Down
6 changes: 3 additions & 3 deletions LMSTools/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ def update(self):
sock.bind(('', 0))

try:
sock.sendto(lms_msg, (lms_ip, lms_port))
sock.sendto(lms_msg.encode(), (lms_ip, lms_port))

while True:
try:
data, server = sock.recvfrom(1024)
host, _ = server
host, _ = server
if data.startswith(b'E'):
port = data.split("\x04")[1]
port = data.split("\x04".encode())[1]
entries.append({'port': int(port),
'data': data,
'from': server,
Expand Down
2 changes: 1 addition & 1 deletion LMSTools/menu.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

from menuitems import (NextMenuItem,
from .menuitems import (NextMenuItem,
PlaylistMenuItem,
SearchMenuItem,
AudioMenuItem)
Expand Down
6 changes: 3 additions & 3 deletions LMSTools/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#
# This set of tools was inspired by the PyLMS library.

from artworkresolver import LMSArtworkResolver
from tags import LMSTags as tags
from utils import LMSUtils
from .artworkresolver import LMSArtworkResolver
from .tags import LMSTags as tags
from .utils import LMSUtils


DETAILED_TAGS = [tags.ARTIST,
Expand Down
21 changes: 16 additions & 5 deletions LMSTools/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
Simple python class definitions for interacting with Logitech Media Server.
This code uses the JSON interface.
"""
import urllib2
import urllib.request, urllib.error
import json
import base64

from .player import LMSPlayer

Expand All @@ -18,19 +19,26 @@ class LMSServer(object):
:param host: address of LMS server (default "localhost")
:type port: int
:param port: port for the web interface (default 9000)
:type username: str
:param username: username for accessing the server
:type password: str
:param password: password for accessing the server

Class for Logitech Media Server.
Provides access via JSON interface. As the class uses the JSON interface, no active connections are maintained.

"""

def __init__(self, host="localhost", port=9000):
def __init__(self, host="localhost", port=9000, username=None, password=None):
self.host = host
self.port = port
self._version = None
self.id = 1
self.web = "http://{h}:{p}/".format(h=host, p=port)
self.url = "http://{h}:{p}/jsonrpc.js".format(h=host, p=port)
if username and password:
self.auth = base64.b64encode(bytes('{u}:{p}'.format(u=username, p=password),'ascii'))
else: self.auth = None

def request(self, player="-", params=None):
"""
Expand All @@ -40,9 +48,12 @@ def request(self, player="-", params=None):
:param params: Request command

"""
req = urllib2.Request(self.url)
req = urllib.request.Request(self.url)
req.add_header('Content-Type', 'application/json')

if self.auth:
req.add_header("Authorization", "Basic {}".format(self.auth.decode('utf-8')))

if type(params) == str:
params = params.split()

Expand All @@ -53,11 +64,11 @@ def request(self, player="-", params=None):
"params": cmd}

try:
response = urllib2.urlopen(req, json.dumps(data))
response = urllib.request.urlopen(req, json.dumps(data).encode())
self.id += 1
return json.loads(response.read())["result"]

except urllib2.URLError:
except urllib.error.URLError:
raise LMSConnectionError("Could not connect to server.")

except:
Expand Down
10 changes: 10 additions & 0 deletions doc/examples/start.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,13 @@ Pretty unexciting though, isn't it?

That's because you know it's not the server that really matters, it's the \
players. So let's see how they work in the next section: :ref:`player-example`.

Authentication
--------------

If your server requires authentication, you can add that into your initial \
server request.

::

>>>server = LMSServer(SERVER_IP, username='username', password='password')
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[build-system]
requires = [
"setuptools>=42",
"wheel"
]
build-backend = "setuptools.build_meta"
22 changes: 22 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import setuptools

with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()

setuptools.setup(
name="LMSTools",
version="0.0.1",
author="Andreas Schamberger",
author_email="[email protected]",
description="A python library for iteracting with your Logitech Media Server",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/aschamberger/LMSTools",
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)",
"Operating System :: OS Independent",
],
packages=setuptools.find_packages(),
python_requires='>=3.6',
)