Skip to content

Instantly share code, notes, and snippets.

@dreness
Last active December 17, 2017 00:44
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 dreness/675ae3b8a752df3b0b4dfd4c25ffe172 to your computer and use it in GitHub Desktop.
Save dreness/675ae3b8a752df3b0b4dfd4c25ffe172 to your computer and use it in GitHub Desktop.
Somebody asked what their second-longest video is :)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import isodate
import time
import math
import json
import sys
import os
from ytdata import YTData
from datetime import datetime
from subprocess import PIPE, Popen
chan = ("schmoyoho", "UCNYrK4tc5i1-eL8TXesH2pg")
freq = 0.10
spread = 4
seed = 2
# be nice to servers; look for a cached json file first
try:
f = open('{}.json'.format(chan[0]))
j = json.loads(f.read())
vids = j.get('items')
except IOError:
print("Didn't find a saved {}.json, making a new one...".format(chan[0]))
schmoyoho = YTData(channel_id=chan[1],
fields=['videoId', 'title', 'duration', 'publishedAt'],
max_results=999, verbose=False)
schmoyoho.fetch()
schmoyoho.dump('{}.json'.format(chan[0]))
vids = schmoyoho.items
if vids is None or len(vids) < 1:
print("Couldn't load any info :/ Is your google API key ok?")
print(" https://support.google.com/googleapi/answer/6158862")
os.remove("{}.json".format(chan[0]))
sys.exit(1)
for _,v in enumerate(vids):
v['duration'] = isodate.parse_duration(v['duration'])
v['publishedAt'] = datetime.strptime(
v['publishedAt'], "%Y-%m-%dT%H:%M:%S.%fZ")
sorted_vids = sorted(vids, key=lambda k: k['duration'])
# suspense printer
def easeUp(t,b,c,d):
return c * math.pow( 2, 25 * (t/d - 1) ) + b;
child = Popen(["lolcat", "-f", "-t", "--seed={}".format(seed),
"--freq={}".format(freq), "--spread={}".format(spread), "-"],
stdin=PIPE, stdout=PIPE)
lines = []
num = len(sorted_vids) + 1
for i in range(1,num,1):
v = sorted_vids.pop(0)
s = '{} {} {} {}\n'.format(
v['duration'], v['publishedAt'], v['videoId'], v['title'])
child.stdin.write(s.encode("utf-8"))
child.stdin.flush()
child.stdin.close()
lines = child.stdout.read().decode("utf-8").splitlines()
for z in range(1,num,1):
interval = easeUp(z,.02,1.8,num)
print(lines.pop(0))
time.sleep(interval)
@dreness
Copy link
Author

dreness commented Dec 17, 2017

It looks kinda like this: https://youtu.be/afnDnWcOsII

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment