0

I'm trying to check if a list of 4 YouTube videos still online. I know one of them is offline, and from time to time I want to see if any action was made upon the others. Searching through the web and with a little coding (I'm a beginner) I made this:

import requests
from pprint import pprint

videos = ["id1","id2","id3","id4"]

for i in videos:
    id_of_video = i
    your_api_key = 'myapi' 
    url = 'https://www.googleapis.com/youtube/v3/videos?id={}&key={}&part=status'.format(id_of_video, your_api_key)
    url_get = requests.get(url)
    pprint(url_get.json())

The code runs well and I receive something like this (I copied just two results):

{'etag': 'somenumbersandcharactershere',
 'items': [{'etag': 'somenumbersandcharactershere',
            'id': 'videoid',
            'kind': 'youtube#video',
            'status': {'embeddable': True,
                       'license': 'youtube',
                       'madeForKids': False,
                       'privacyStatus': 'public',
                       'publicStatsViewable': True,
                       'uploadStatus': 'processed'}}],
 'kind': 'youtube#videoListResponse',
 'pageInfo': {'resultsPerPage': 1, 'totalResults': 1}}

{'etag': 'somenumbersandcharactershere',
 'items': [],
 'kind': 'youtube#videoListResponse',
 'pageInfo': {'resultsPerPage': 0, 'totalResults': 0}}

One of them is online and the other one is not. Great.

But I want to create a code that check it for me from time to time and says: "This video ID is not available anymore". But I don't know how.

My main task right now is to print a message for each video ID! So, let's say: "ID1 is on-line" and "ID2 is offline".

I thought about creating an if statement to check the results of the "url_get.json()" tag but it only holds the last video ID...

If I run

url_get.json()

I get only the last ID from the list.

2 Answers2

1

You could use a dictionary for this. At first, I set the values to None. Then, when looping through the dictionary, I set the values to the returned JSON. At the end of the program, I print IDx is offline or online. You can do whatever you want with the data in the dictionary though.

import requests
from pprint import pprint

videos = {"id1": None, "id2": None, "id3": None, "id4": None}
for i in videos:
    id_of_video = i
    your_api_key = 'myapi' 
    url = 'https://www.googleapis.com/youtube/v3/videos?id={}&key={}&part=status'.format(id_of_video, your_api_key)
    url_get = requests.get(url)
    pprint(url_get.json())
    videos[i] = url_get.json()

for video in videos:
    if videos[video]["pageInfo"]["resultsPerPage"] == 0:
        print(f"{video.upper()} is offline")
    else:
        print(f"{video.upper()} is online")
KetZoomer
  • 2,701
  • 3
  • 15
  • 43
  • @interferemadly, if this answered your question, please click the checkmark – KetZoomer Apr 21 '21 at 23:47
  • Tried this, but received this error: `TypeError Traceback (most recent call last) in 12 13 for video in videos: ---> 14 if video[videos]["pageInfo"]["resultsPerPage"] == 0: 15 print(f"{video.upper()} is offline") 16 else: TypeError: string indices must be integers` – interferemadly Apr 21 '21 at 23:54
  • @interferemadly fixed :) – KetZoomer Apr 21 '21 at 23:57
  • hey man, I don't know if I'm missing something – as I said, I'm a beginner. Still getting and error: `TypeError Traceback (most recent call last) in 11 12 for video in videos: ---> 13 if videos[video]["pageInfo"]["resultsPerPage"] == 0: 14 print(f"{video.upper()} is offline") 15 else: TypeError: 'Response' object is not subscriptable` – interferemadly Apr 22 '21 at 00:18
  • now it should be fixed, I forgot to turn it into a json – KetZoomer Apr 22 '21 at 00:26
  • Thanks, @KetZoomer. It worked. I will read the core carefully to understand better :) – interferemadly Apr 22 '21 at 01:13
0

So you want to write a program that periodically pings Youtube. The easiest way to achieve it would be something like

from time import sleep

delay : int = 20

while True:
# ping YT here
  sleep(delay)

This will run it every 20 seconds. There are a number of problems here.

  • It runs forever
  • The only way to stop program is by clicking Ctrl+C
  • It also won't be exactly 20 seconds

But it will do the trick at first. I found great question (thread???) here at stackOverflow The Answer proposes to use some more advanced concept of "event loop". It's definitely worth getting into if you have time.

  • 1
    if server fails or system is shutdown then this won't work, program need to run again. better to create a cron job for this job – sahasrara62 Apr 21 '21 at 23:27
  • Thanks, this is part of the problem. But my main question is: how to check each ID and print a message for each one! So, let's say: "ID1 is on-line" and "ID2 is offline". – interferemadly Apr 21 '21 at 23:28
  • @CartesianCoordianateSystem, please read the question fully before answering – KetZoomer Apr 21 '21 at 23:58