Last active
October 6, 2022 21:09
-
-
Save capjamesg/b2a9e52bc55fc6756cc86fbb4b6e5e9f 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 | |
REST = r"(?P<rest>.*)" | |
DOMAIN_PATTERN = r"^http[s]?://(?P<domain>\w.[^/]+)/" + REST | |
url_summary_templates = [ | |
( | |
r"http[s]?://github.com/(?P<user>\w+)/(?P<project>[\w-]+)/", | |
"A comment in the {project} GitHub repository", | |
), | |
( | |
r"http[s]?://github.com/(?P<user>\w+)/(?P<project>[\w-]+)/issues/(?P<issue>\d+)", | |
"A comment on an issue in the {project} GitHub repository", | |
), | |
( | |
r"http[s]?://github.com/(?P<user>\w+)/(?P<project>[\w-]+)/pulls/(?P<pull>\d+)", | |
"A comment on a pull request in the {project} GitHub repository", | |
), | |
( | |
r"http[s]?://twitter.com/(?P<user>\w+)" + REST, | |
"A tweet by @{user}", | |
), | |
( | |
r"^http[s]?://eventbrite.[com|co.uk]", | |
"An event on Eventbrite", | |
), | |
( | |
r"^http[s]?://upcoming.com", | |
"An event on Upcoming", | |
), | |
( | |
r"^http[s]?://calagator.com", | |
"An event on Calagator", | |
), | |
( | |
r"^http[s]?://events.indieweb.org", | |
"An IndieWeb event" | |
), | |
( | |
r"^http[s]?://indieweb.org/(?P<page>[\w-]+)", | |
"The {page} page on the IndieWeb wiki", | |
), | |
] | |
def get_summary(url: str, custom_properties: list = []): | |
""" | |
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' | |
""" | |
for pattern, summary in url_summary_templates + custom_properties: | |
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