我正在使用 Plex 并尝试使用 Plex api 来获取并不断更新我的 Plex 服务器的状态。如果状态发生变化,我会做一些事情。这是我的代码(我已经编辑了不相关的部分):
import requests
import time
ip = '127.0.0.1'
port = '####'
def get_status(last_media_type, last_state):
data = requests.get('http://' + ip + ':' + port + '/status/sessions', verify=False)
if home: # Ignore this part. Redacted code
if '<MediaContainer size="0">' in data.text: # If nothing is playing
media_type = 'None'
state = 'STOPPED'
else: # Get what's playing and its status
media_type_start = data.text.find('type=\"')
media_type_end = data.text.find('updatedAt=')
media_type = data.text[media_type_start + 6:media_type_end - 2].capitalize()
print('Type: ' + str(media_type))
state_prefix_start = data.text.find('<Player address=')
state_prefix_end = data.text.find('<TranscodeSession key=')
state_prefix = data.text[state_prefix_start:state_prefix_end]
state_start = state_prefix.find('state=\"')
state_end = state_prefix.find('title=')
state = state_prefix[state_start + 7:state_end - 2].upper()
if last_media_type != media_type or last_state != state: # THIS IS IMPORTANT. I don't want to execute the next part if nothing has changed.
# DO STUFF, redacted
if state == 'PLAYING':
interval = 1
elif state == 'PAUSED':
interval = 10
elif state == 'STOPPED':
interval = 15
else:
interval = 60 * 3 # if nobody home, only check once every 3 minutes
time.sleep(interval)
get_status(media_type, state)
get_status('a', 'b') # filler arguments used to initiate script
问题
不幸的是,半小时左右后,我得到:
RecursionError: maximum recursion depth exceeded in comparison
这是回溯(尽管由于修订,行号不匹配):
Traceback (most recent call last):
File "H:/Users/micha/Documents/Scripts/Python/plexStatus.pyw", line 63, in <module>
p = psutil.Process(os.getpid()) # Get PID for this script
File "C:\Python351\lib\site-packages\psutil\__init__.py", line 349, in __init__
self._init(pid)
File "C:\Python351\lib\site-packages\psutil\__init__.py", line 375, in _init
self.create_time()
File "C:\Python351\lib\site-packages\psutil\__init__.py", line 636, in create_time
self._create_time = self._proc.create_time()
File "C:\Python351\lib\site-packages\psutil\_pswindows.py", line 282, in wrapper
return fun(self, *args, **kwargs)
File "C:\Python351\lib\site-packages\psutil\_pswindows.py", line 422, in create_time
if self.pid in (0, 4):
RecursionError: maximum recursion depth exceeded in comparison
问题
我怎样才能让它在没有递归错误的情况下工作,同时在执行之间仍然只等待 1 秒?
我的猜测是调用这个脚本有更好的方法,同时仍然能够使用上一次调用中的变量。我只是不知道怎么做。我想我可以使用pickle
我已经在另一个编辑部分使用的,但如果可能的话,我想避免大量的读/写。或者说真的,只是尽可能少地占用内存,同时仍然保留功能。
注意: 我确信有更好的解析方法。我是编程新手。在我解决这个问题的同时进行研究。
在尝试修复此问题时,我已多次编辑此代码,因此我不确定这是否会引发其他错误。这是我要修复的递归。
我知道有像 PlexPy 这样的工具。只是想自己写一个更小的,我可以连续运行而不会成为主要的资源消耗者。