如何仅在X时间已过时打印

2024-04-16 10:57:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在有一个脚本,可以解析iTunes中关于当前歌曲播放的信息并打印出来。它只在歌曲更改时打印。现在我想让它打印只有当一分钟的歌曲已经过去,但不知道如何做到这一点。你知道吗

这是我目前拥有的代码:

def giveData():
    last_title, last_artist, last_album = None, None, None
    while True:
       template = '"%s" by %s (%d)\n from %s'
       info = ''

iTunesCount = app('System Events').processes[its.name == 'iTunes'].count()
if iTunesCount > 0:
  iTunes = app('iTunes')
  if iTunes.player_state.get() == k.playing:
    track = iTunes.current_track.get()
    artist = track.artist.get()
    title = track.name.get()
    album = track.album.get()
    stars = track.rating.get()/20

     if title != last_title or artist != last_artist or album != last_album:

      last_title, last_artist, last_album = title, artist, album
      info = template % (title, artist, stars, album)

    # Trying new solution, still printing songs that haven't been playing
    # for 60s, just delaying printing them by 60s:

      now = time.time()
      future = now + 60
      while time.time() < future:
        pass

      song_info = title + " - " + artist
      print song_info`

Tags: infononealbumgetbyiftimetitle