Skip to content

Instantly share code, notes, and snippets.

@gwanryo
Created April 4, 2022 01:42
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 gwanryo/0c5071041409804cbc7d23d7d510cf37 to your computer and use it in GitHub Desktop.
Save gwanryo/0c5071041409804cbc7d23d7d510cf37 to your computer and use it in GitHub Desktop.
import subprocess
import json
def get_subprocess_output(cmd):
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
process.wait()
data, _ = process.communicate()
if process.returncode != 0:
return None
return data.decode('utf-8')
with open('last_video.id', 'r') as f:
last_video_id = f.readline().rstrip()
if last_video_id:
last_video_id = int(last_video_id)
else:
last_video_id = 0
result = get_subprocess_output("./twitch-dl videos --game \"Super Mario Maker 2\" nokduro --json")
if not result:
exit()
twitch_info = json.loads(result.split('\n')[-2])
videos = sorted(twitch_info['videos'], key=lambda x: int(x['id']))
max_video_id = last_video_id
print(f"Found {twitch_info['count']} videos!")
for i, video in enumerate(videos):
if int(video['id']) > last_video_id:
print(f"Video {i + 1} ({video['title']}) has greater id than previous last downloaded video id. ({video['id']} > {last_video_id})")
subprocess.call(["./twitch-dl", "download", video['id'], "--overwrite", "-q", "source", "-f", "mp4", "-o", "{channel}_{title_slug}_{date}_{time}.{format}"])
# Update last downloaded video id
with open('last_video.id', 'w') as f:
f.write(video['id'])
else:
print(f"Video {i + 1} ({video['title']}) has lesser id than previous last downloaded video id. ({video['id']} <= {last_video_id})")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment