yt_video_collect.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import requests
  2. import json
  3. import redis
  4. api_key = 'AIzaSyDzDM2u5Ru6ld1Imz0pUrTFL1n-gll9q1U'
  5. def collect_video():
  6. all_video_id = []
  7. video_list = f"https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails,status&playlistId=UU2Qda9PhJWuiMTCZ-j7K_hg&key={api_key}&maxResults=50"
  8. r = requests.get(video_list)
  9. res = json.loads(r.content)
  10. next_page = res['nextPageToken']
  11. for i in res['items']:
  12. all_video_id.append(i['snippet']['resourceId']['videoId'])
  13. while True:
  14. next_video_list = f"https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails,status&playlistId=UU2Qda9PhJWuiMTCZ-j7K_hg&key={api_key}&maxResults=50&pageToken={next_page}"
  15. r = requests.get(next_video_list)
  16. res = json.loads(r.content)
  17. for i in res['items']:
  18. all_video_id.append(i['snippet']['resourceId']['videoId'])
  19. try:
  20. next_page = res['nextPageToken']
  21. except:
  22. break
  23. print(len(all_video_id))
  24. r = redis.Redis(host='db.ptt.cx', port=6379, db=0,password='choozmo9')
  25. js = json.dumps(all_video_id)
  26. r.set('yt_video',js)
  27. if __name__ == '__main__':
  28. collect_video()
  29. "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails,status&playlistId=UU2Qda9PhJWuiMTCZ-j7K_hg&key=AIzaSyDzDM2u5Ru6ld1Imz0pUrTFL1n-gll9q1U&maxResults=50&pageToken=EAAaBlBUOkNESQ"
  30. "https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id=UC2Qda9PhJWuiMTCZ-j7K_hg&key=AIzaSyDzDM2u5Ru6ld1Imz0pUrTFL1n-gll9q1U"
  31. "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails,status&playlistId=UU2Qda9PhJWuiMTCZ-j7K_hg&key=AIzaSyDzDM2u5Ru6ld1Imz0pUrTFL1n-gll9q1U&maxResults=50"
  32. '''觀看率'''"https://www.googleapis.com/youtube/v3/videos?part=statistics&id=eFZckOTi24c&key=AIzaSyDzDM2u5Ru6ld1Imz0pUrTFL1n-gll9q1U"