Created
June 2, 2023 19:18
-
-
Save renegarcia/9e80f1c84c57753b5aa4e19f0b8198db to your computer and use it in GitHub Desktop.
This program reads an url and prints the last part of the path as a title. Suitable for creating hyperlinks in markdown
This file contains 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
""" | |
# URL to title | |
Simple program that reads an url and prints the last part of the path as a title. | |
## Case of use | |
I use this to format references in my mardown documents. For example, given the url | |
``` | |
https://stackoverflow.com/questions/76392847/flutter-firebase-to-python-firebase | |
``` | |
The program outputs | |
``` | |
Flutter Firebas To Python Firebase | |
``` | |
which I use to link to the question, like so: | |
```markdown | |
[Flutter Firebas To Python Firebase](https://stackoverflow.com/questions/76392847/flutter-firebase-to-python-firebase) | |
``` | |
""" | |
from urllib.parse import urlparse | |
def get_last_path(url:str) -> str: | |
# Parse the URL | |
parsed_url = urlparse(url) | |
# Get the path from the parsed URL | |
path = parsed_url.path | |
# Split the path by slashes and get the last part | |
path_parts = path.split('/') | |
last_part = path_parts[-1] | |
return last_part | |
def path_to_title(path:str) -> str: | |
return path.replace("-", " ").title() | |
def main(): | |
url = input("Enter the URL: ") | |
path = get_last_path(url) | |
title = path_to_title(path) | |
print(title) | |
if __name__ == "__main__": | |
while True: | |
try: | |
main() | |
except KeyboardInterrupt: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment