Created
August 20, 2023 12:53
-
-
Save jmcph4/c9c0c4754ca161dd573d707f966d1769 to your computer and use it in GitHub Desktop.
Grab URLs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/python3 | |
| from sys import argv | |
| from typing import Optional | |
| from dataclasses import dataclass | |
| from datetime import datetime | |
| from bs4 import BeautifulSoup | |
| from urllib import request | |
| @dataclass | |
| class Entry(object): | |
| timestamp: datetime | |
| title: str | |
| url: str | |
| def __repr__(self) -> str: | |
| return f" - [{self.timestamp}] [{self.title}]({self.url})\n\n---\n" | |
| def fetch_title(url: str) -> Optional[str]: | |
| return BeautifulSoup( | |
| request.urlopen(url).read().decode('utf8'), | |
| "html.parser" | |
| ).find("title").text | |
| def main() -> None: | |
| if len(argv) != 2: | |
| print("usage: wiki_urls.py infile") | |
| exit(1) | |
| with open(argv[1], "r") as f: | |
| entries = [ | |
| Entry( | |
| datetime.now().isoformat(), | |
| fetch_title(line.strip()), | |
| line.strip() | |
| ) for line in f.readlines() | |
| ] | |
| print("---") | |
| for entry in entries: | |
| print(entry) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment