|
| 1 | +from typing import Optional |
| 2 | +from bs4 import BeautifulSoup |
| 3 | +import requests |
| 4 | +import feedparser |
| 5 | + |
| 6 | +from generate_docs.markdown import MarkdownPage |
| 7 | + |
| 8 | + |
| 9 | +def get_intro_text(): |
| 10 | + return """Welcome to a collection of Jupyter Notebooks from the |
| 11 | + [How to Python](https://therenegadecoder.com/series/how-to-python/) series on The Renegade Coder. For convenience, |
| 12 | + you can access all of the articles, videos, challenges, and source code below. Alternatively, I keep |
| 13 | + [an enormous article](https://therenegadecoder.com/code/python-code-snippets-for-everyday-problems/) up to date |
| 14 | + with all these snippets as well. |
| 15 | + """ |
| 16 | + |
| 17 | + |
| 18 | +def get_series_posts(): |
| 19 | + index = 1 |
| 20 | + base = "https://therenegadecoder.com/series/how-to-python/feed/?paged=" |
| 21 | + feed = [] |
| 22 | + while (rss := feedparser.parse(f"{base}{index}")).entries: |
| 23 | + feed.extend(rss.entries) |
| 24 | + index += 1 |
| 25 | + return feed |
| 26 | + |
| 27 | + |
| 28 | +def get_youtube_video(entry): |
| 29 | + content = entry.content[0].value |
| 30 | + soup = BeautifulSoup(content, "html.parser") |
| 31 | + target = soup.find("h2", text="Video Summary") |
| 32 | + if target: |
| 33 | + return target.find_next_sibling().find_all("a")[-1]["href"] |
| 34 | + |
| 35 | + |
| 36 | +def get_slug(title: str, sep: str): |
| 37 | + return title.split(":")[0][:-10].lower().replace(" ", sep) |
| 38 | + |
| 39 | + |
| 40 | +def get_challenge(title: str): |
| 41 | + slug = get_slug(title, "-") |
| 42 | + base = "https://github.com/TheRenegadeCoder/how-to-python-code/tree/master/challenges/" |
| 43 | + print(f"Trying {base}{slug}") |
| 44 | + request = requests.get(f"{base}{slug}") |
| 45 | + if request.status_code == 200: |
| 46 | + return f"{base}{slug}" |
| 47 | + |
| 48 | + |
| 49 | +def get_notebook(title: str): |
| 50 | + slug = get_slug(title, "_") |
| 51 | + base = "https://github.com/TheRenegadeCoder/how-to-python-code/tree/master/notebooks/" |
| 52 | + print(f"Trying {base}{slug}.ipynb") |
| 53 | + request = requests.get(f"{base}{slug}.ipynb") |
| 54 | + if request.status_code == 200: |
| 55 | + return f"{base}{slug}.ipynb" |
| 56 | + |
| 57 | + |
| 58 | +class HowTo: |
| 59 | + def __init__(self): |
| 60 | + self.page: Optional[MarkdownPage] = None |
| 61 | + self.feed: Optional[list] = None |
| 62 | + self._load_data() |
| 63 | + self._build_readme() |
| 64 | + |
| 65 | + def _load_data(self): |
| 66 | + self.feed = get_series_posts() |
| 67 | + |
| 68 | + def _build_readme(self): |
| 69 | + self.page = MarkdownPage("README") |
| 70 | + |
| 71 | + # Introduction |
| 72 | + self.page.add_content("# How to Python - Source Code") |
| 73 | + self.page.add_section_break() |
| 74 | + self.page.add_content(get_intro_text()) |
| 75 | + self.page.add_section_break() |
| 76 | + |
| 77 | + # Article List |
| 78 | + self.page.add_table_header("Index", "Title", "Publish Date", "Article", "Video", "Challenge", "Notebook") |
| 79 | + self.build_table() |
| 80 | + |
| 81 | + def build_table(self): |
| 82 | + index = 1 |
| 83 | + for entry in self.feed: |
| 84 | + if "Code Snippets" not in entry.title: |
| 85 | + article = f"[Article]({entry.link})" |
| 86 | + youtube_url = get_youtube_video(entry) |
| 87 | + youtube = f"[Video]({youtube_url})" if youtube_url else "" |
| 88 | + challenge_url = get_challenge(entry.title) |
| 89 | + challenge = f"[Challenge]({challenge_url})" if challenge_url else "" |
| 90 | + notebook_url = get_notebook(entry.title) |
| 91 | + notebook = f"[Notebook]({notebook_url})" if notebook_url else "" |
| 92 | + self.page.add_table_row(str(index), entry.title, entry.published, article, youtube, challenge, notebook) |
| 93 | + index += 1 |
0 commit comments