Advertisement
jarekmor

YouTube

Jan 31st, 2022
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. # Wygenerowanie listy z YouTube
  2.  
  3. import googleapiclient.discovery
  4. import googleapiclient.errors
  5. from googleapiclient.discovery import build
  6.  
  7. import datetime
  8. import json
  9. import os
  10.  
  11. today_date = datetime.datetime.now()
  12. up_to_date = today_date.strftime("%Y-%m-%dT%H:%M:%S%z+12:00")
  13.  
  14. tomorrow_date = datetime.datetime.now() + datetime.timedelta(days=1)
  15. tomorrow_day = tomorrow_date.strftime("%Y-%m-%dT%H:%M:%S%z+00:00")
  16.  
  17. api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'  <=== YOUTUBE API KEY
  18.  
  19. youtube = build('youtube', 'v3', developerKey = api_key)
  20.  
  21. request = youtube.activities().list(
  22.     part="snippet, contentDetails",
  23.     channelId="UCCT_JGIn9I9FS6OTzzqWEew",
  24. #    channelId="UCbX3YkedQunLt7EQAdVxh7w",
  25.     publishedBefore=tomorrow_day
  26.     )
  27.  
  28. response = request.execute()
  29.  
  30. base_youtube_url = 'https://www.youtube.com/watch?v='
  31.  
  32. table = []
  33. videos_id_table = []
  34.  
  35. pages_num = int(response['pageInfo']['resultsPerPage'])
  36.  
  37. for x in range(pages_num):
  38.  
  39.     pub_data = response["items"][x]["snippet"]["publishedAt"]
  40.     film_addr = base_youtube_url + response["items"][x]["contentDetails"]["upload"]["videoId"]
  41.     film_title = response["items"][x]["snippet"]["title"]
  42.     table.append(film_addr)
  43.     videos_id_table.append(response["items"][x]["contentDetails"]["upload"]["videoId"])
  44.  
  45.     #print(pub_data[0:10] , film_addr, " ", film_title)
  46.  
  47.  
  48. with open('/usr/share/hassio/homeassistant/www/data2.txt', 'w') as f:
  49.     json.dump(table, f)
  50.  
  51. with open('data.txt', 'w') as f:
  52.     json.dump(table, f)
  53.  
  54. #print(table[0])
  55.  
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement