Skip to content

Instantly share code, notes, and snippets.

@krashanoff
Last active May 2, 2021 07:09
Show Gist options
  • Save krashanoff/85bc51e6ba08312e85d526b367fe824f to your computer and use it in GitHub Desktop.
Save krashanoff/85bc51e6ba08312e85d526b367fe824f to your computer and use it in GitHub Desktop.
Simple program to pretty-print a request to YouTube's `get_video_info` endpoint.
#!/usr/bin/python3
# Leo Krashanoff
# (c) 2021
# Subject to contents of MIT License
#
# Originally written for maguro: https://github.com/krashanoff/maguro
from urllib.parse import parse_qs, unquote
from requests import get
from argparse import ArgumentParser
from json import dumps, loads
ENDPOINT = "https://www.youtube.com/get_video_info?video_id="
FIELD_NAME = "player_response="
if __name__ == "__main__":
parser = ArgumentParser(
"getinfo",
description="Write the output of a request to `/get_video_info?video_id=...` to stdout.",
)
parser.add_argument(
"ID", type=str, help="ID of the video to reference."
)
args = parser.parse_args()
res = get(f"{ENDPOINT}{args.ID}")
if res.status_code != 200:
exit(1)
readable = parse_qs(str(res.content, 'utf-8'))["player_response"][0].replace("\"", "\"")
print(dumps(loads(readable), indent=2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment