CCCPaste

YouTube

# Wygenerowanie listy z YouTube

import googleapiclient.discovery
import googleapiclient.errors
from googleapiclient.discovery import build

import datetime
import json
import os

today_date = datetime.datetime.now()
up_to_date = today_date.strftime("%Y-%m-%dT%H:%M:%S%z+12:00")

tomorrow_date = datetime.datetime.now() + datetime.timedelta(days=1)
tomorrow_day = tomorrow_date.strftime("%Y-%m-%dT%H:%M:%S%z+00:00")

api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'  <=== YOUTUBE API KEY

youtube = build('youtube', 'v3', developerKey = api_key)

request = youtube.activities().list(
    part="snippet, contentDetails",
    channelId="UCCT_JGIn9I9FS6OTzzqWEew",
#    channelId="UCbX3YkedQunLt7EQAdVxh7w",
    publishedBefore=tomorrow_day
    )

response = request.execute()

base_youtube_url = 'https://www.youtube.com/watch?v='

table = []
videos_id_table = []

pages_num = int(response['pageInfo']['resultsPerPage'])

for x in range(pages_num):

    pub_data = response["items"][x]["snippet"]["publishedAt"]
    film_addr = base_youtube_url + response["items"][x]["contentDetails"]["upload"]["videoId"]
    film_title = response["items"][x]["snippet"]["title"]
    table.append(film_addr)
    videos_id_table.append(response["items"][x]["contentDetails"]["upload"]["videoId"])

    #print(pub_data[0:10] , film_addr, " ", film_title) 


with open('/usr/share/hassio/homeassistant/www/data2.txt', 'w') as f:
    json.dump(table, f)

with open('data.txt', 'w') as f:
    json.dump(table, f)

#print(table[0])