model.py 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. import youtube_model
  2. import db_model
  3. def get_video_infos (video_ids, youtube_user, db_path):
  4. """
  5. Get YouTube video info from the cache DB if we have it, fetching and storing anything we don't already have.
  6. Fetches up to 50 videos due to API limitations and the fact we don't code to handle more.
  7. Is auth aware; returns videos fetched with the app key or auth_user_id.
  8. """
  9. auth_user_id = None
  10. if youtube_user:
  11. auth_user_id = youtube_user['id']
  12. vid_infos = db_model.load_video_infos(video_ids, auth_user_id, db_path=db_path)
  13. loaded_video_ids = set(map(lambda vid_info: vid_info['id'], vid_infos))
  14. fetch_video_ids = list(set(video_ids) - loaded_video_ids)
  15. if fetch_video_ids:
  16. print(f'get_video_infos: fetching videos: {fetch_video_ids}')
  17. fetched_vid_infos = youtube_model.fetch_video_infos(fetch_video_ids, youtube_user=youtube_user)
  18. print(f'get_video_infos: fetched videos, length={len(fetched_vid_infos)}')
  19. db_model.store_video_infos(fetched_vid_infos, auth_user_id=auth_user_id, db_path=db_path)
  20. vid_infos += fetched_vid_infos
  21. return vid_infos