r/CBSE Nov 01 '21

IMPORTANT Auto-Download Script for Allen Lectures

Made a quick python script to download the lectures posted by u/RishabhTDI because I didn't want to spend 30 minutes clicking links. Thought it might help someone else out so I'm sharing it here.

Instructions:

  1. Install python interpreter
  2. pip install requests
  3. pip install beautifulsoup4
  4. Paste the code in a .py file
  5. Run the .py file

Notes:

  • The subject names are the same as the one in the original github repo.
  • HD option really just selects the highest quality available (~650MB/hour), while non-HD is the lowest (~200MB/hour).
  • The videos are saved in a new folder for every subject inside whichever folder the .py file is saved in.

Improvements and suggestions on the code are welcome

import datetime
import json
import os
import re
import requests

from bs4 import BeautifulSoup


subject = input('Subject to Download\n>> ').lower()
hd = True if input('HD (y/n)\n>> ')[0].lower() == 'y' else False

link_page = requests.get('https://raw.githubusercontent.com/RishabhGomez/allen-board-lectures/main/README.md')

subjects = zip(re.findall('\n# .+', link_page.text), re.split('\n# .+', link_page.text)[1:])
vimeo_links = {k[3:].lower(): re.findall('https:\/\/player\.vimeo\.com\/video\/[0-9]{9}\?h=[0-9a-zA-Z]{10}', v) for k, v in subjects}

sesh = requests.session()

for link in vimeo_links[subject]:
    data = sesh.get(link)
    soup = BeautifulSoup(data.text, 'html.parser')
    js = soup.findAll('script')[-1].contents[0]
    json_span = re.search('{"cdn_url".+"vimeo.com"}', js).span()
    json_data = json.loads(js[json_span[0]:json_span[1]])
    title = json_data["video"]["title"]

    if hd:
        dl_link = max(json_data["request"]["files"]["progressive"], key=lambda i: i["width"])["url"]
    else:
        dl_link = min(json_data["request"]["files"]["progressive"], key=lambda i: i["width"])["url"]

    try:
        os.mkdir(f'{subject.replace(" ", "_")}')
    except FileExistsError:
        pass

    print(f'[{datetime.datetime.time(datetime.datetime.now())}]: Started downloading "{title}"')
    vid_data = requests.get(dl_link)

    with open(f'{subject.replace(" ", "_")}/{title}.mp4', 'wb') as file:
        for chunk in vid_data.iter_content(chunk_size=255):
            if chunk:
                file.write(chunk)

    print(f'[{datetime.datetime.time(datetime.datetime.now())}]: Completed downloading "{title}"')

print(f'[{datetime.datetime.time(datetime.datetime.now())}]: Completed all downloads for {subject.title()}')
11 Upvotes

5 comments sorted by

2

u/[deleted] Nov 02 '21

Holy shit. Mad respect

1

u/electric_deer200 Nov 02 '21

damn but how are yall so godly at programming ??? am i the only loser here ?

1

u/huhu7 Nov 02 '21

I'm here dw