如何在X时间过后才打印

时间:2015-01-08 22:56:28

标签: python itunes elapsedtime

我目前有一个脚本可以解析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`

1 个答案:

答案 0 :(得分:0)

如何添加时间计数器?

import time

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

print '60 seconds have passed'

或@Undeterminant指出只使用

time.sleep(60)