Skip to content

Commit

Permalink
Handle null Country TvMaze field
Browse files Browse the repository at this point in the history
A recording of a broadcast of the first episode in
season 16 of "Criminal Minds" on CBS lead to a metadata
lookup failure. The general syntax for this metadata
lookup was:
    tvmaze.py -N "Criminal Minds" "2022-11-24 11:00:00"
Investigation revealed that for recent years this show is
normally only available on a streaming service called
Paramount+.
  https://api.tvmaze.com/shows/81
    ...
    "webChannel":{"id":107,"name":"Paramount+",
    "country":null,"officialSite":
    "https://www.paramountplus.com/"}
    ...

The null value for Country caused the Timezone
retrieval code in tvmaze.py to fail. The code has
been updated to handle a null Country field.

While regression testing, another crash appeared.
    tvmaze.py -N "The Masked Singer" "2022-11-24 19:00:00"
This show has distinct versions in many countries and
timezones.

country='United States',  timezone='America/New_York'
country='United Kingdom', timezone='Europe/London'
country='Australia',      timezone='Australia/Sydney'
country='Belgium',        timezone='Europe/Brussels'
country=None, show_tz =None  (Japanese version)
country='Finland',        timezone='Europe/Helsinki'
country='Germany',        timezone='Europe/Busingen'
country='Israel',         timezone='Asia/Jerusalem'
country='Mexico',         timezone='America/Monterrey'

The null Country field for the Japanese version is
handled fine, but passing 'Asia/Jerusalem' to
astimezone() caused it to throw an exception.

  UnboundLocalError: local variable 'ttmfmt'
  referenced before assignment

This appears to be a bug in python3. To prevent
it from crashing our script, UnboundLocalError
has been added to the exception catcher.

Some of the debug print messages have also been
updated to improve their format.

Refs: #654
  • Loading branch information
SteveErl authored and linuxdude42 committed Jan 22, 2023
1 parent 0e5dab4 commit e4c55b5
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions mythtv/programs/scripts/metadata/Television/tvmaze.py
Expand Up @@ -226,10 +226,19 @@ def buildNumbers(args, opts):
if show_network is None:
show_network = show_info.streaming_service
show_country = show_network.get('country')
show_tz = show_country.get('timezone')
# Some webChannels don't specify country or timezone
if show_country:
show_tz = show_country.get('timezone')
else:
show_tz = None
dtInTgtZone = dtInLocalZone.astimezone(posixtzinfo(show_tz))
# For at least one timezone, the following exception may be thrown.
# UnboundLocalError: local variable 'ttmfmt' referenced before assignment
# Example: tvmaze.py --debug -N "The Masked Singer" "2022-11-24 19:00:00"

except (ValueError, AttributeError) as e:
except (ValueError, AttributeError, UnboundLocalError) as e:
if opts.debug:
print('show_tz =%s, except = %s' % (show_tz, e))
dtInTgtZone = None

if dtInTgtZone:
Expand All @@ -252,18 +261,16 @@ def buildNumbers(args, opts):

if epInTgtZone == dtInTgtZone:
if opts.debug:
print('Recording matches inetref %d, season %d, episode %d at %s' \
% (inetref, ep.season, ep.number, epInTgtZone))
print('Recording matches \'%s\' at %s' % (ep, epInTgtZone))
time_match_list.append(i)
minTimeDelta = timedelta(minutes=0)

# Consider it a match if the recording starts late,
# but within the duration of the show.
elif epInTgtZone < dtInTgtZone < epInTgtZone+durationDelta:
# Recording start time is within the range of this episode
if opts.debug:
print('Recording in range of inetref %d, season %d, episode %d (%s ... %s)' \
% (inetref, ep.season, ep.number, epInTgtZone, epInTgtZone+durationDelta))
print('Recording in range of \'%s\' (%s ... %s)' \
% (ep, epInTgtZone, epInTgtZone+durationDelta))
time_match_list.append(i)
minTimeDelta = timedelta(minutes=0)
# Consider it a match if the recording is a little bit early. This helps cases
Expand All @@ -273,11 +280,13 @@ def buildNumbers(args, opts):
# Recording started earlier than this episode, so see if it's the closest match
if epInTgtZone - dtInTgtZone == minTimeDelta:
if opts.debug:
print('adding episode to closest list', epInTgtZone - dtInTgtZone, '\n')
print('Adding episode \'%s\' to closest list. Offset = %s' \
% (ep, epInTgtZone - dtInTgtZone))
early_match_list.append(i)
elif epInTgtZone - dtInTgtZone < minTimeDelta:
if opts.debug:
print('this episode is new closest', epInTgtZone - dtInTgtZone, '\n')
print('Episode \'%s\' is new closest. Offset = %s' \
% (ep, epInTgtZone - dtInTgtZone))
minTimeDelta = epInTgtZone - dtInTgtZone
early_match_list = [i]

Expand Down

0 comments on commit e4c55b5

Please sign in to comment.