Skip to content

Instantly share code, notes, and snippets.

@FloPinguin
Created January 27, 2020 17:28

Revisions

  1. FloPinguin created this gist Jan 27, 2020.
    50 changes: 50 additions & 0 deletions check-youtube-online.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    import os
    import sys
    import json
    import subprocess
    import requests
    import datetime

    def log(msg):
    sys.stdout.buffer.write((msg + "\n").encode('utf8'))

    def loadjson(file_path):
    if os.path.getsize(file_path) > 0:
    file = open(file_path, "r")
    jsonx = json.loads(file.read())
    file.close()
    return jsonx
    else:
    return dict()

    def writejson(file_path, jsonx):
    file = open(file_path, "w")
    file.write(json.dumps(jsonx, indent=4, sort_keys=True))
    file.close()

    youtubedir_path = 'U:/Hoard'
    checkdatefile_path = "C:/Users/Flo/Documents/check-youtube-online/checkdate.txt"
    notonlinefile_path = "C:/Users/Flo/Documents/check-youtube-online/notonline.txt"

    checkdate = loadjson(checkdatefile_path)
    notonline = loadjson(notonlinefile_path)

    for subdir, dirs, files in os.walk(youtubedir_path):
    for file in files:
    if ".info.json" in file:
    f = open(os.path.join(subdir, file), "r")
    j = json.loads(f.read())

    if j['extractor'] == "youtube" and (j['id'] not in checkdate or (datetime.datetime.today() - datetime.datetime.strptime(checkdate[j['id']], '%Y%m%d')).days > 30):
    log("Checking " + os.path.join(subdir, file))
    r = requests.get("https://www.youtube.com/watch?v=" + j['id'])

    checkdate[j['id']] = datetime.datetime.strftime(datetime.datetime.today(), "%Y%m%d")
    writejson(checkdatefile_path, checkdate)

    if not "<meta name=\"twitter:title" in r.text:
    log("OFFLINE")
    notonline[j['id']] = datetime.datetime.strftime(datetime.datetime.today(), "%Y-%m-%d") + ": " + os.path.join(subdir, file).replace(".info.json", "")
    writejson(notonlinefile_path, notonline)
    else:
    log("Skipping " + os.path.join(subdir, file))