Created
October 6, 2022 21:31
-
-
Save capjamesg/486ab9e189d2ea12b1013598dd4a2445 to your computer and use it in GitHub Desktop.
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
# Adapting https://gist.github.com/angelogladding/c61488d2dd82fb73b9078541a4e2ea13 for library use | |
import re | |
from urllib.parse import urlparse | |
REST = r"(?P<rest>.*)" | |
DOMAIN_PATTERN = r"^(?P<domain>\w.[^/]+)/" + REST | |
url_summary_templates = { | |
"github.com": [ | |
( | |
r"github.com/(?P<user>\w+)/(?P<project>[\w-]+)/", | |
"A comment in the {project} GitHub repository", | |
), | |
( | |
r"github.com/(?P<user>\w+)/(?P<project>[\w-]+)/issues/(?P<issue>\d+)", | |
"A comment on an issue in the {project} GitHub repository", | |
), | |
( | |
r"github.com/(?P<user>\w+)/(?P<project>[\w-]+)/pulls/(?P<pull>\d+)", | |
"A comment on a pull request in the {project} GitHub repository", | |
), | |
], | |
"twitter.com": [ | |
( | |
r"twitter.com/(?P<user>\w+)" + REST, | |
"A tweet by @{user}", | |
), | |
], | |
"upcoming.com": [ | |
( | |
"", | |
"An event on Upcoming", | |
) | |
], | |
"calagator.com": [ | |
( | |
"", | |
"An event on Calagator", | |
) | |
], | |
"events.indieweb.org": [ | |
( | |
"", | |
"An event on Calagator", | |
) | |
], | |
"indieweb.org": [ | |
( | |
r"^indieweb.org/(?P<page>[\w-]+)", | |
"The {page} page on the IndieWeb wiki", | |
) | |
] | |
} | |
def get_summary(url: str, custom_properties: dict = {}): | |
""" | |
Return a text summary for given `url`. | |
>>> get_summary("https://github.com/capjamesg/indieweb-utils/pulls/1") | |
'A comment on a pull request in the indieweb-utils GitHub repository' | |
""" | |
url_summary_templates.update(custom_properties) | |
results = url_summary_templates.get(urlparse(url).netloc, []) | |
url = url.replace("https://", "").replace("http://", "") | |
for pattern, summary in results: | |
if pattern == "": | |
return summary | |
if match := re.match(pattern, url): | |
return summary.format(**match.groupdict()) | |
domain = re.match(DOMAIN_PATTERN, url) | |
if domain: | |
return "A post on " + domain.groupdict()["domain"] | |
return "" | |
print(get_summary("https://github.com/capjamesg/indieweb-utils/pulls/1")) | |
print(get_summary("https://twitter.com/indieweb")) | |
print(get_summary("https://eventbrite.co.uk/test")) | |
print(get_summary("http://upcoming.com/test")) | |
print(get_summary("https://indieweb.org/coffee")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment