Skip to content

Commit

Permalink
Subscriptions: Fix exceptions when videos are missing upload dates
Browse files Browse the repository at this point in the history
E.g. line 548, AttributeError: 'NoneType' object has no attribute 'lower'

When upload dates are unavailable, make ones up which give the
correct video order
  • Loading branch information
user234683 committed Jan 20, 2024
1 parent bfdae7f commit e7bd21e
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions youtube/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,14 +542,37 @@ def find_element(base, tag_name):
if video_item['id'] in times_published:
video_item['time_published'] = times_published[video_item['id']]
video_item['is_time_published_exact'] = True
else:
elif video_item.get('time_published'):
video_item['is_time_published_exact'] = False
try:
video_item['time_published'] = youtube_timestamp_to_posix(video_item['time_published']) - i # subtract a few seconds off the videos so they will be in the right order
except KeyError:
except Exception:
print(video_item)

else:
video_item['is_time_published_exact'] = False
video_item['time_published'] = None
video_item['channel_id'] = channel_id
if len(videos) > 1:
# Go back and fill in any videos that don't have a time published
# using the time published of the surrounding ones
for i in range(len(videos)-1):
if (videos[i+1]['time_published'] is None
and videos[i]['time_published'] is not None
):
videos[i+1]['time_published'] = videos[i]['time_published'] - 1
for i in reversed(range(1,len(videos))):
if (videos[i-1]['time_published'] is None
and videos[i]['time_published'] is not None
):
videos[i-1]['time_published'] = videos[i]['time_published'] + 1
# Special case: none of the videos have a time published.
# In this case, make something up
if videos and videos[0]['time_published'] is None:
assert all(v['time_published'] is None for v in videos)
now = time.time()
for i in range(len(videos)):
# 1 month between videos
videos[i]['time_published'] = now - i*3600*24*30


if len(videos) == 0:
Expand Down

0 comments on commit e7bd21e

Please sign in to comment.