Skip to content

Conversation

@beastmatser
Copy link

@beastmatser beastmatser commented Oct 13, 2021

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
Copy link
Collaborator

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
Copy link
Collaborator

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

@mCodingLLC mCodingLLC closed this Oct 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants