mCodingLLC / VideosSampleCode

Code from the mCoding sample videos
MIT License
952 stars 198 forks source link

Add code links to README #2

Closed beastmatser closed 2 years ago

beastmatser commented 2 years ago

Code I used:

import requests
from bs4 import BeautifulSoup
import lxml
import os

response = requests.get("https://github.com/mCodingLLC/VideosSampleCode/tree/master/videos")
soup = BeautifulSoup(response.text, "lxml")
links = {int(obj["href"].split("/")[6][:3]): obj["href"] for obj in soup.find_all("a", class_="js-navigation-open Link--primary")}

with open("README.md") as f:
    lines = f.readlines()

# with open("new_readme.txt", "w") as f:
#     new_lines: list[str] = []
#     n = 0
#     for line in lines:
#         if not line.startswith("|"):
#             new_lines.append(line)

#         elif line == "| N | Video | \n":
#             new_lines.append("| N | Video | Code |\n")

#         elif line == "| --- | --- |\n":
#             new_lines.append("| --- | --- | --- |\n")

#         if line.startswith("|") and line[2].isnumeric():
#             try:
#                 new_lines.append(f"{line.strip()} [here]({links[int(line.split(' ')[1])]}) |\n")
#             except KeyError:
#                 new_lines.append(line)
#             n += 1

#     f.writelines(new_lines)

with open("new_readme.txt", "w") as f:
    new_lines: list[str] = []
    n = 0
    for line in lines:
        if line.startswith("|") and line[2].isnumeric():
            splitted_lines = line.split(str(n))
            try:
                splitted_lines[1] = f"({links[int(line.split(' ')[1])]})" + splitted_lines[1]
                new_lines.append(str(f"[{n}]").join(splitted_lines))
            except KeyError:
                new_lines.append(str(f"{n}").join(splitted_lines))
            n += 1

        else:
            new_lines.append(line)

    f.writelines(new_lines)

os.remove("README.md")
os.rename("new_readme.txt", "README.md")

The commented code creates a separate column and adds here with a link to the code.

mCodingLLC commented 2 years ago

Instead of linking the number, could you add a new column that just says "src" all the way down and put the links on that column. Also could you make the links relative instead of absolute: https://github.blog/2013-01-31-relative-links-in-markup-files/

mCodingLLC commented 2 years ago

I went ahead and fixed it up the way I wanted, thanks for your help!