commit 2: 0f075381c6e9
parent 1: 9e896eaa1beb
branch: default
Better error handling for fetching the current song
Derek Payton / dmpayton
6 months ago

Changed (Δ544 bytes):

raw changeset »

kusc.py (23 lines added, 5 lines removed)

Up to file-list kusc.py:

@@ -121,19 +121,37 @@ class SysTrayIcon(QtGui.QSystemTrayIcon)
121
121
        ## Parse out the currently playing song...
122
122
        soup = BeautifulSoup(response)
123
123
        onnowtext = soup.findAll('p', {'id': 'onnowtext'})[0]
124
        program = unescape(onnowtext.findAll('span')[1].contents[0])
125
        song = unescape(onnowtext.findAll('span')[2].contents[0])
124
        now_playing = []
125
126
        ## Try to get the program name...
127
        try:
128
            now_playing.append(unescape(onnowtext.findAll('span')[1].contents[0]))
129
        except Exception:
130
            pass
131
132
        ## Try to get the song name...
133
        try:
134
            now_playing.append(unescape(onnowtext.findAll('span')[2].contents[0]))
135
        except Exception:
136
            pass
137
138
        ## Couldn't get either? Bummer... Try again in a few minutes
139
        if not now_playing:
140
            self.showMessage('Error', 'Could not get currently playing song from KUSC.org', self.Warning)
141
            QtCore.QTimer.singleShot(180000, self.now_playing)
142
            return
143
144
        now_playing = '\n'.join(now_playing)
126
145
127
146
        ## If it's a new song, set it
128
        if song == self.current_song:
147
        if now_playing == self.current_song:
129
148
            ## Check again in 1 minute
130
149
            print '\told song, try again in 1 minute'
131
150
            QtCore.QTimer.singleShot(60000, self.now_playing)
132
151
        else:
133
152
            ## Set the new song, and dont check again for 5 minutes
134
153
            print '\tnew song, check again in 5 minutes'
135
            self.current_song = song
136
            now_playing = '\n'.join((program, song))
154
            self.current_song = now_playing
137
155
            self.setToolTip(now_playing)
138
156
            self.showMessage('Now Playing on KUSC:', now_playing)
139
157
            QtCore.QTimer.singleShot(300000, self.now_playing)