mirror of
https://github.com/jellyfin/jellyfin-mpv-shim.git
synced 2025-02-21 23:21:02 +00:00
Fix more code quality issues.
This commit is contained in:
parent
9925f945a6
commit
293ab3c959
@ -1,15 +1,13 @@
|
||||
import cgi
|
||||
import datetime
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import posixpath
|
||||
import threading
|
||||
import requests
|
||||
import urllib.request, urllib.parse, urllib.error
|
||||
import urllib.parse
|
||||
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
from http.server import HTTPServer
|
||||
from http.server import SimpleHTTPRequestHandler
|
||||
from socketserver import ThreadingMixIn
|
||||
from .utils import upd_token
|
||||
@ -83,12 +81,12 @@ class HttpHandler(SimpleHTTPRequestHandler):
|
||||
name = arguments.get("X-Plex-Device-Name")
|
||||
|
||||
if not uuid:
|
||||
log.warn("HttpHandler::getSubFromRequest subscriber didn't set X-Plex-Client-Identifier")
|
||||
log.warning("HttpHandler::getSubFromRequest subscriber didn't set X-Plex-Client-Identifier")
|
||||
self.setStandardResponse(500, "subscriber didn't set X-Plex-Client-Identifier")
|
||||
return
|
||||
|
||||
if not name:
|
||||
log.warn("HttpHandler::getSubFromRequest subscriber didn't set X-Plex-Device-Name")
|
||||
log.warning("HttpHandler::getSubFromRequest subscriber didn't set X-Plex-Device-Name")
|
||||
self.setStandardResponse(500, "subscriber didn't set X-Plex-Device-Name")
|
||||
return
|
||||
|
||||
@ -108,7 +106,7 @@ class HttpHandler(SimpleHTTPRequestHandler):
|
||||
def updateCommandID(self, arguments):
|
||||
if "commandID" not in arguments:
|
||||
if self.path.find("unsubscribe") < 0:
|
||||
log.warn("HttpHandler::updateCommandID no commandID sent to this request!")
|
||||
log.warning("HttpHandler::updateCommandID no commandID sent to this request!")
|
||||
return
|
||||
|
||||
commandID = -1
|
||||
@ -120,7 +118,7 @@ class HttpHandler(SimpleHTTPRequestHandler):
|
||||
|
||||
uuid = self.headers.get("X-Plex-Client-Identifier", None)
|
||||
if not uuid:
|
||||
log.warn("HttpHandler::updateCommandID subscriber didn't set X-Plex-Client-Identifier")
|
||||
log.warning("HttpHandler::updateCommandID subscriber didn't set X-Plex-Client-Identifier")
|
||||
self.setStandardResponse(500, "When commandID is set you also need to specify X-Plex-Client-Identifier")
|
||||
return
|
||||
|
||||
@ -252,7 +250,7 @@ class HttpHandler(SimpleHTTPRequestHandler):
|
||||
pass
|
||||
|
||||
if commandID == -1 or not uuid:
|
||||
log.warn("HttpHandler::poll the poller needs to set both X-Plex-Client-Identifier header and commandID arguments.")
|
||||
log.warning("HttpHandler::poll the poller needs to set both X-Plex-Client-Identifier header and commandID arguments.")
|
||||
self.setStandardResponse(500, "You need to specify both x-Plex-Client-Identifier as a header and commandID as a argument")
|
||||
return
|
||||
|
||||
|
@ -1,8 +1,6 @@
|
||||
import logging
|
||||
import os
|
||||
import requests
|
||||
import uuid
|
||||
import xml.etree.ElementTree as ET
|
||||
import pickle as pickle
|
||||
import socket
|
||||
import json
|
||||
|
@ -26,8 +26,6 @@ __author__ = 'DHJ (hippojay) <plex@h-jay.com>'
|
||||
|
||||
import socket
|
||||
import struct
|
||||
import sys
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
import urllib.request, urllib.error, urllib.parse
|
||||
@ -96,52 +94,54 @@ class PlexGDM:
|
||||
def client_update (self):
|
||||
update_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||||
|
||||
#Set socket reuse, may not work on all OSs.
|
||||
# Set socket reuse, may not work on all OSs.
|
||||
try:
|
||||
update_sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
|
||||
except:
|
||||
pass
|
||||
|
||||
#Attempt to bind to the socket to recieve and send data. If we can;t do this, then we cannot send registration
|
||||
# Attempt to bind to the socket to receive and send data. If we can;t do this, then we cannot send registration
|
||||
try:
|
||||
update_sock.bind(('0.0.0.0',self.client_update_port))
|
||||
update_sock.bind(('0.0.0.0', self.client_update_port))
|
||||
except:
|
||||
self.__printDebug( "Error: Unable to bind to port [%s] - client will not be registered" % self.client_update_port, 0)
|
||||
self.__printDebug("Error: Unable to bind to port [%s] -"
|
||||
" client will not be registered" % self.client_update_port, 0)
|
||||
return
|
||||
|
||||
update_sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255)
|
||||
status = update_sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(self._multicast_address) + socket.inet_aton('0.0.0.0'))
|
||||
update_sock.setblocking(0)
|
||||
update_sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP,
|
||||
socket.inet_aton(self._multicast_address) + socket.inet_aton('0.0.0.0'))
|
||||
update_sock.setblocking(False)
|
||||
self.__printDebug("Sending registration data: HELLO %s\n%s" % (self.client_header, self.client_data), 3)
|
||||
|
||||
#Send initial client registration
|
||||
# Send initial client registration
|
||||
try:
|
||||
update_sock.sendto(b"HELLO %s\n%s" % (self.client_header, self.client_data), self.client_register_group)
|
||||
except:
|
||||
self.__printDebug( "Error: Unable to send registeration message" , 0)
|
||||
self.__printDebug("Error: Unable to send registration message", 0)
|
||||
|
||||
#Now, listen for client discovery reguests and respond.
|
||||
# Now, listen for client discovery reguests and respond.
|
||||
while self._registration_is_running:
|
||||
try:
|
||||
data, addr = update_sock.recvfrom(1024)
|
||||
self.__printDebug("Recieved UDP packet from [%s] containing [%s]" % (addr, data.strip()), 3)
|
||||
except socket.error as e:
|
||||
self.__printDebug("Received UDP packet from [%s] containing [%s]" % (addr, data.strip()), 3)
|
||||
except socket.error:
|
||||
pass
|
||||
else:
|
||||
if b"M-SEARCH * HTTP/1." in data:
|
||||
self.__printDebug("Detected client discovery request from %s. Replying" % ( addr ,) , 2)
|
||||
self.__printDebug("Detected client discovery request from %s. Replying" % (addr,), 2)
|
||||
try:
|
||||
update_sock.sendto(b"HTTP/1.0 200 OK\n%s" % self.client_data, addr)
|
||||
except:
|
||||
self.__printDebug( "Error: Unable to send client update message",0)
|
||||
self.__printDebug("Error: Unable to send client update message", 0)
|
||||
|
||||
self.__printDebug("Sending registration data: HTTP/1.0 200 OK\n%s" % (self.client_data), 3)
|
||||
self.__printDebug("Sending registration data: HTTP/1.0 200 OK\n%s" % self.client_data, 3)
|
||||
self.client_registered = True
|
||||
time.sleep(0.5)
|
||||
|
||||
self.__printDebug("Client Update loop stopped",1)
|
||||
self.__printDebug("Client Update loop stopped", 1)
|
||||
|
||||
#When we are finished, then send a final goodbye message to deregister cleanly.
|
||||
# When we are finished, then send a final goodbye message to deregister cleanly.
|
||||
self.__printDebug("Sending registration data: BYE %s\n%s" % (self.client_header, self.client_data), 3)
|
||||
try:
|
||||
update_sock.sendto(b"BYE %s\n%s" % (self.client_header, self.client_data), self.client_register_group)
|
||||
@ -202,7 +202,7 @@ class PlexGDM:
|
||||
while True:
|
||||
try:
|
||||
data, server = sock.recvfrom(1024)
|
||||
self.__printDebug("Received data from %s, %s" % server, 3)
|
||||
self.__printDebug("Received data from %s" % server, 3)
|
||||
self.__printDebug("Data received is:\n %s" % data, 3)
|
||||
returnData.append( { 'from' : server,
|
||||
'data' : data } )
|
||||
@ -221,9 +221,9 @@ class PlexGDM:
|
||||
update = { 'server' : response.get('from')[0] }
|
||||
|
||||
#Check if we had a positive HTTP response
|
||||
if bytes("200 OK",'utf-8') in response.get('data'):
|
||||
if b"200 OK" in response.get('data'):
|
||||
|
||||
for each in str(response.get('data')).split('\\r\\n'):
|
||||
for each in response.get('data').decode('utf-8').splitlines():
|
||||
|
||||
update['discovery'] = "auto"
|
||||
update['owned']='1'
|
||||
|
@ -204,6 +204,7 @@ class Video(object):
|
||||
return
|
||||
|
||||
match = False
|
||||
index = None
|
||||
for index, stream in enumerate(self._part_node.findall("./Stream[@streamType='2']") or []):
|
||||
if stream.get('selected') == "1":
|
||||
match = True
|
||||
@ -217,6 +218,7 @@ class Video(object):
|
||||
return
|
||||
|
||||
match = False
|
||||
index = None
|
||||
for index, sub in enumerate(self._part_node.findall("./Stream[@streamType='3']") or []):
|
||||
if sub.get('selected') == "1":
|
||||
match = True
|
||||
|
@ -1,6 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import getpass
|
||||
import queue
|
||||
import logging
|
||||
import sys
|
||||
|
@ -1,15 +1,10 @@
|
||||
import logging
|
||||
import math
|
||||
import os
|
||||
import re
|
||||
import mpv
|
||||
|
||||
from threading import Thread, RLock
|
||||
from time import sleep
|
||||
from threading import RLock
|
||||
from queue import Queue
|
||||
|
||||
from . import conffile
|
||||
from .conf import settings
|
||||
from .utils import synchronous, Timer
|
||||
|
||||
# Scrobble progress to Plex server at most every 5 seconds
|
||||
@ -37,7 +32,7 @@ class PlayerManager(object):
|
||||
if hasattr(self._player, 'osc'):
|
||||
self._player.osc = True
|
||||
else:
|
||||
log.warn("This mpv version doesn't support on-screen controller.")
|
||||
log.warning("This mpv version doesn't support on-screen controller.")
|
||||
|
||||
self.url = None
|
||||
self.evt_queue = Queue()
|
||||
|
@ -31,7 +31,7 @@ class RemoteSubscriberManager(object):
|
||||
self.subscribers[subscriber.uuid].commandID = subscriber.commandID
|
||||
|
||||
def removeSubscriber(self, subscriber):
|
||||
if subscriber != None and subscriber.uuid in self.subscribers:
|
||||
if subscriber is not None and subscriber.uuid in self.subscribers:
|
||||
log.debug("RemoteSubscriberManager::removeSubscriber removing subscriber %s [%s]" % (subscriber.url, subscriber.uuid))
|
||||
self.subscribers.pop(subscriber.uuid)
|
||||
|
||||
@ -72,10 +72,13 @@ class RemoteSubscriber(object):
|
||||
|
||||
def shouldRemove(self):
|
||||
if self.lastUpdated.elapsed() > SUBSCRIBER_REMOVE_INTERVAL:
|
||||
log.debug("RemoteSubscriber::shouldRemove removing %s because elapsed: %lld" % (self.uuid, self.lastUpdated.elapsed()))
|
||||
log.debug("RemoteSubscriber::shouldRemove "
|
||||
"removing %s because elapsed: %d" % (self.uuid, self.lastUpdated.elapsed()))
|
||||
return True
|
||||
|
||||
log.debug("RemoteSubscriber::shouldRemove will not remove %s because elapsed: %lld" % (self.uuid, self.lastUpdated.elapsed()))
|
||||
log.debug("RemoteSubscriber::shouldRemove will not "
|
||||
"remove %s because elapsed: %d" % (self.uuid, self.lastUpdated.elapsed()))
|
||||
return False
|
||||
|
||||
|
||||
remoteSubscriberManager = RemoteSubscriberManager()
|
||||
|
@ -43,7 +43,10 @@ def synchronous(tlockname):
|
||||
def upd_token(domain, token):
|
||||
plex_eph_tokens[domain] = token
|
||||
|
||||
def get_plex_url(url, data={}):
|
||||
def get_plex_url(url, data=None):
|
||||
if not data:
|
||||
data = {}
|
||||
|
||||
parsed_url = urllib.parse.urlsplit(url)
|
||||
domain = parsed_url.hostname
|
||||
|
||||
@ -82,11 +85,14 @@ def get_plex_url(url, data={}):
|
||||
|
||||
return url
|
||||
|
||||
def safe_urlopen(url, data={}):
|
||||
def safe_urlopen(url, data=None):
|
||||
"""
|
||||
Opens a url and returns True if an HTTP 200 code is returned,
|
||||
otherwise returns False.
|
||||
"""
|
||||
if not data:
|
||||
data = {}
|
||||
|
||||
url = get_plex_url(url, data)
|
||||
|
||||
try:
|
||||
@ -110,6 +116,7 @@ def find_exe(filename, search_path=None):
|
||||
if search_path is None:
|
||||
search_path = os.environ.get("PATH", "")
|
||||
|
||||
path = None
|
||||
paths = search_path.split(os.pathsep)
|
||||
for path in paths:
|
||||
if os.access(os.path.join(path, filename), os.X_OK):
|
||||
|
9
setup.py
9
setup.py
@ -1,21 +1,20 @@
|
||||
from setuptools import setup, Extension
|
||||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name='Plex MPV Shim',
|
||||
version='0.2',
|
||||
packages=['plex_mpv_shim',],
|
||||
packages=['plex_mpv_shim'],
|
||||
license='MIT',
|
||||
long_description=open('README.md').read(),
|
||||
author="Ian Walton",
|
||||
author_email="iwalton3@github",
|
||||
url="https://github.com/iwalton3/plex-mpv-shim",
|
||||
zip_safe=False,
|
||||
entry_points = {
|
||||
entry_points={
|
||||
'console_scripts': [
|
||||
'plex-mpv-shim=plex_mpv_shim.mpv_shim:main',
|
||||
]
|
||||
},
|
||||
include_package_data=True,
|
||||
install_requires = ['python-mpv', 'requests']
|
||||
install_requires=['python-mpv', 'requests']
|
||||
)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user