Skip to content

Instantly share code, notes, and snippets.

@FloPinguin
Created January 27, 2020 17:28
Show Gist options
  • Save FloPinguin/f58e1d2ed1de5d94a191a520c3055ab7 to your computer and use it in GitHub Desktop.
Save FloPinguin/f58e1d2ed1de5d94a191a520c3055ab7 to your computer and use it in GitHub Desktop.
In a directory structure full of .info.json files (Generated by youtube-dl), check which youtube videos are no longer online (And write them to a file).
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment