Skip to content

Instantly share code, notes, and snippets.

@carneeki
Last active August 11, 2018 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carneeki/68e4deb3a9b9f3de460b3fd9370cad0b to your computer and use it in GitHub Desktop.
Save carneeki/68e4deb3a9b9f3de460b3fd9370cad0b to your computer and use it in GitHub Desktop.
ytGhostBuster scans all your favourite edgey channels for vids that might get deleted, and saves them before they're taken down. Run frequently with a scheduler.
#!/usr/bin/zsh
SCRIPTDIR=~/.local/bin/
TIMERSDIR=~/.config/systemd/user/
sedstring="s/REPLACEME/$(whoami)/g"
mkdir -p $SCRIPTDIR
cp ytGhostBuster.py $SCRIPTDIR/ytGhostBuster.py
mkdir -p $TIMERSDIR
cp ytGhostBuster.service $TIMERSDIR/ytGhostBuster.service
sed -i $sedstring $TIMERSDIR/ytGhostBuster.service
cp ytGhostBuster.timer $TIMERSDIR/ytGhostBuster.timer
systemctl --user enable ytGhostBuster.service
systemctl --user enable ytGhostBuster.timer
systemctl --user start ytGhostBuster.service
systemctl --user start ytGhostBuster.timer
#!/usr/bin/python
import urllib.request
import json
import os
import itertools
import re
API_KEY="" # TODO: get from console.google.com
VIDSBASE='{}/Videos/YouTubes/'.format(os.path.expanduser("~")) # TODO: set where all vids will be saved
channels=[
"UC7SeFWZYFmsm1tqWxfuOTPQ", # dankula
"UCwW_vaMPlq8J4weKDsfeJzw", # Bearing
"UC-yewGHQbNFpDrGM0diZOLA", # Sargon
"UCpiCH7qvGVlzMOqy3dncA5Q", # The Thinkery
"UCx_SEanFxmYdkylVM1v3RDg", # The Incredible Salt Mine
"UCDc_MCu3ZstNXmAqqT36SNA", # Sugartits
"UC-EREEErQQqgYNyNB4YGQnQ", # Patrick
"UCG749Dj4V2fKa143f8sE60Q", # Tim Pool
]
################################################################################
# Probably no need to change anything below
################################################################################
COUNT="5" # TODO: tune me if desired
archive = '{}/archive.txt'.format(VIDSBASE)
youtubedlopts='{}/%(uploader)s/%(upload_date)s - %(id)s - %(title)s.%(ext)s'.format(VIDSBASE)
################################################################################
# Very little need to change anything below...
################################################################################
vidlist=[] # list of all video urls to fetch
def get_videos_in_channel(channel_id,maxResults):
base_video_url = 'https://www.youtube.com/watch?v='
base_search_url = 'https://www.googleapis.com/youtube/v3/search?'
first_url = base_search_url+'key={}&channelId={}&part=snippet,id&order=date&maxResults={}'.format(API_KEY, channel_id,maxResults)
video_links = []
url = first_url
with urllib.request.urlopen(url) as url:
inp = url.read()
resp = json.loads(inp.decode("utf-8"))
for i in resp['items']:
if i['id']['kind'] == "youtube#video":
video_links.append(base_video_url + i['id']['videoId'])
return video_links
def get_vid(url):
m = re.search('.*watch\?v=(.*)', url)
if m:
return m.group(1)
def check_archive(vid):
return open(archive,'r').read().find(vid) >= 0
for chan in channels:
vidlist.extend(get_videos_in_channel(chan,COUNT))
for url in vidlist:
if check_archive(get_vid(url)) == False :
cmdstring = 'youtube-dl --download-archive {} -f best --write-info-json -o \'{}\' "{}"'.format(archive,youtubedlopts,url)
#os.system(cmdstring)
print(cmdstring)
[Unit]
Description=ytGhostBuster
[Service]
Type=simple
ExecStart=/home/REPLACEME/.local/bin/ytGhostBuster.py
[Install]
WantedBy=default.target
[Unit]
Description=Run ytGhostBuster every hour
RefuseManualStart=no
RefuseManualStop=no
[Timer]
Persistent=false
OnCalendar=hourly
Unit=ytGhostBuster.service
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment