def updateWindowHandle(self, callback=None):
def getSpotifyWindowHandle(handle, extra):
pid = GetWindowThreadProcessId(handle)[1]
processName = psutil.Process(pid).name().lower()
songMatch = SONG_DATA_RE.match(GetWindowText(handle))
if(SPOTIFY in processName and songMatch):
self.windowHandle = handle
## Should really be a return False here to kill off the
## enumeration when a suitable handle is found, but that
## produces a weird 'Things have gone VERY wrong' error.
## See: http://docs.activestate.com/activepython/3.1/pywin32/win32gui__EnumWindows_meth.html
EnumWindows(getSpotifyWindowHandle, None)
## Can't know which window will display the currently playing song
## information unless it's playing music.
if(not self.windowHandle):
self._findWindowHandleAttempts += 1
if(self._findWindowHandleAttempts > ATTEMPT_LIMIT):
self.stopScraping()
raise RuntimeError("No valid " + SPOTIFY + " windows available. Is it currently open and running (and not playing any ads)?")
self.playSong()
time.sleep(WAIT_TIME) ## Give Spotify a moment to start playing.
self.updateWindowHandle()
if(callback):
callback()
评论列表
文章目录